How do you rename a MySQL table in PHPMyAdmin? (SQL command?)
Are MySQL table names case-sensitive when working with PHP?
Hit the Operations tab when browsing a table to rename it. Names are case-sensitive in Linux, insensitive in Windows.
You can use the RENAME TABLE syntax:
RENAME TABLE old_name TO new_name
...but the caveat is that no one can be accessing the table when you execute the statement.
Case sensitivity is dependent on the collation - collations ending with "_ci" means "Case Insensitive". The client, in your case PHPMyAdmin, can have it's collation which is different from the MySQL instance's collation... and tables can have their own collation. See the MySQL Collation documentation for more info.
The table name case sensitivity is controlled by lower_case_table_names server variable:
http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_lower_case_table_names
In phpMyAdmin all you need to do is click on the table, and then click on the 'Operations' tab. Once there, you will see 'Rename Table To'. Change this field and your should be good to go.
Related
The site is written in PHP. Connection to the database on the server (debian 9) occurs as follows:
$sqlConnect = $wo['sqlConnect'] = mysqli_connect ($sql_db_host, $sql_db_user, $sql_db_pass, $sql_db_name, 3306);
On the server in the mySQL (1) database, table names have upper-cases (example: Wo_Langs). My localhost is on windows 7 and it does not support case sensetive. When I import a database from server (1) to localhost, all upper-cases in the table names are replaced with low-cases (example: table name Wo_Langs -> wo_langs). After making changes to the database on localhost when importing the database from localhost (2) to the server in the server database (2), all table names are obtained in low-cases. The technical support of the hosting assured me that they had set lower_case_table_names = 0 in the settings of mySQL server. But anyway, when renaming the table name Wo_Langs -> wo_langs, the data from the table is not displayed on the site.
You have to change the case in the php code as well where you are creating queries to manage CRUD operations. Change to case which matches your actual table column names from you environment Windows, Linux....
Debian (and linux in general) use filesystems that are case sensitive. MySQL database name is "mapped" to directory name where all the table files are stored on the server while table name is also name of table related files (usually stored in /var/lib/mysql/<DATABASE> or so). So to solve your problem you should stay away from using mixed case names (for both tables and databases). You may try to a) rename all the files and directories - as suggested to all lowercase names (and maybe restart mysqld) and update your code to use the exactly the same, lowercased references to your tables and databases.
I am having serious headache with MySQL database. Here is my problem.
I am developing a website in Php with SQL Server as database, everything working fine. database 1
Than I have to change database to MySQL, I've done that and create some stored procedures and function, everything working fine here too. database 2
Now I've have to shift MySQL database to another MySQL server, database 3. Problem occurs here because now my old stored procedure and query which I've created are not working. My DB3 structure is same as DB2.
If I fire this query in DB2 select * from Online_Patient; no error.
Same query in DB3 throws an error:
Error Code: 1146. Table 'Online_Patient' doesn't exist
But when I change my query to select * from online_patient; it gives me correct result.
So is there any way to solve this error or do I have to change all query and stored procedures, there are dynamic query in stored procedure so just can't convert upper case to lower case.
Locate your mysql configuration file. It should be named my.cnf. You can run mysqld --help --verbose in the beginning of the output the are the default locations where you can look for the file. My output is:
Default options are read from the following files in the given order:
C:\Windows\my.ini C:\Windows\my.cnf C:\my.ini C:\my.cnf C:\xampp\mysql\my.ini C:\xampp\mysql\my.cnf
Find the lower_case_table_names directive and set it to 1:
lower_case_table_names = 1
From MySQL documentation: http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_lower_case_table_names
If set to 0, table names are stored as specified and comparisons are case sensitive. If set to 1, table names are stored in lowercase on disk and comparisons are not case sensitive. If set to 2, table names are stored as given but compared in lowercase. This option also applies to database names and table aliases. For additional information, see Section 10.2.2, “Identifier Case Sensitivity”.
Then restart mysql server.
I have php code where first letter of database table names is in capital letter every where but table names in database are in lowercase.
What global setting should I use so that no need to change in every code file for making table names in same case as in database.
MySQL metadata is case sensitive by default on Linux. That is if you have a table my_table, in lower case, then
select * from my_table
will succeed while
select * from MY_TABLE
will fail with some sort of table doesn't exist message.
If you want both statements to succeed, you need to put the following line
lower_case_table_names = 1
in your /etc/my.cnf or wherever you keep your MySQL configuration. Be sure to add the system variable to [mysqld] section of the configuration file.
You need to check for the mysql identifier:
lower_case_table_names
You need to check your my.ini file located in your mysql directory. For more details on source and destination operating systems check:
http://dev.mysql.com/doc/refman/5.0/en/identifier-case-sensitivity.html
AS EXTRACTED FROM THE URL ABOVE:
If you are using MySQL on only one platform, you do not normally have
to change the lower_case_table_names variable from its default value.
However, you may encounter difficulties if you want to transfer tables
between platforms that differ in file system case sensitivity. For
example, on Unix, you can have two different tables named my_table and
MY_TABLE, but on Windows these two names are considered identical. To
avoid data transfer problems arising from lettercase of database or
table names, you have two options:
Use lower_case_table_names=1 on all systems. The main disadvantage with this is that when you use SHOW TABLES or SHOW DATABASES, you do
not see the names in their original lettercase.
Use lower_case_table_names=0 on Unix and lower_case_table_names=2 on Windows. This preserves the lettercase of database and table names.
The disadvantage of this is that you must ensure that your statements
always refer to your database and table names with the correct
lettercase on Windows. If you transfer your statements to Unix, where
lettercase is significant, they do not work if the lettercase is
incorrect.
I want to name my mysql table column names using camel case and create php classes from these mysql tables with the same camel case names. I will be generating these php classes automatically. I'm wondering if I can rely on column name case no matter what platform I run my application on. So for example, if I name one column name "FirstName", will I ever encounter a time where reading the column name from the database will product "firstname" or something like that?
Short answer is no.
The long answer is that case-sensitivity for some things in MySQL depend on the underlying operating system. (Unix being the sensitive one)
Here is the reference to the issue in the MySQL documentation.
Consequently, the case sensitivity of the underlying operating system plays a part in the case sensitivity of database and table names. This means database and table names are not case sensitive in Windows, and case sensitive in most varieties of Unix. One notable exception is Mac OS X, which is Unix-based but uses a default file system type (HFS+) that is not case sensitive.
Also from the documentation on column names specifically:
Column, index, and stored routine names are not case sensitive on any platform, nor are column aliases. Trigger names are case sensitive, which differs from standard SQL.
MySQL is case sensitive in table and column names, and case in-sensitive in keywords.
But note that Windows is only case preserving, and file names are table names. (If you work on table "SalesAccounts" when you meant "Salesaccounts" then it will read OK on Windows and fail on Linux.
You should be OK with what you want to do, but
1) Test on Linux,
2) Test the tools you are using.
I am working in three different servers which are unix/windows/unix.
case-1: In my loacl server which is unix I have one table name Country_master and its fields pk_CountryId, CountryName etc.
case-2:In my Demo server which is window table name changed automatically to country_master.
case-3:In my Live server which is unix table name country_master.
But in my code table name i taken is Country_master so it gives me error like this
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'markets.Country_master' doesn't exist'.
Can i have any way not to change table name in database and also in code but it takes lower case?
You can change the case sensitivity by setting the lower_case_table_names system variable. See here for instructions:
http://dev.mysql.com/doc/refman/5.6/en/identifier-case-sensitivity.html
From the fine manual:
In MySQL, databases correspond to directories within the data directory. Each table within a database corresponds to at least one file within the database directory (and possibly more, depending on the storage engine). Triggers also correspond to files. Consequently, the case sensitivity of the underlying operating system plays a part in the case sensitivity of database, table, and trigger names.
So the case sensitivity of your table names depends on the underlying file system: they will (usually) be case insensitive on Windows and OSX but case sensitive on Linux. This behavior is partially dependent on the lower_case_table_names setting:
If set to 0, table names are stored as specified and comparisons are case sensitive. If set to 1, table names are stored in lowercase on disk and comparisons are not case sensitive. If set to 2, table names are stored as given but compared in lowercase. This option also applies to database names and table aliases.
So you have various things that will affect the case sensitivity of your table names. The only sane solution (IMO) is to always use lower case table names so that you don't have to worry about it; you should also use lower case column names for consistency.
http://dev.mysql.com/doc/refman/5.0/en/identifier-case-sensitivity.html
9.2.2. Identifier Case Sensitivity In MySQL, databases correspond to directories within the data directory. Each table within a database
corresponds to at least one file within the database directory (and
possibly more, depending on the storage engine). Consequently, the
case sensitivity of the underlying operating system plays a part in
the case sensitivity of database and table names. This means database
and table names are not case sensitive in Windows, and case sensitive
in most varieties of Unix. One notable exception is Mac OS X, which is
Unix-based but uses a default file system type (HFS+) that is not case
sensitive. However, Mac OS X also supports UFS volumes, which are case
sensitive just as on any Unix. See Section 1.8.4, “MySQL Extensions to
Standard SQL”. The lower_case_table_names system variable also affects
how the server handles identifier case sensitivity, as described later
in this section.
Note Although database and table names are not case sensitive on some
platforms, you should not refer to a given database or table using
different cases within the same statement. The following statement
would not work because it refers to a table both as my_table and as
MY_TABLE:
mysql> SELECT * FROM my_table WHERE MY_TABLE.col=1; Column, index, and
stored routine names are not case sensitive on any platform, nor are
column aliases. Trigger names are case sensitive, which differs from
standard SQL.
By default, table aliases are case sensitive on Unix, but not so on
Windows or Mac OS X. The following statement would not work on Unix,
because it refers to the alias both as a and as A:
mysql> SELECT col_name FROM tbl_name AS a
-> WHERE a.col_name = 1 OR A.col_name = 2; However, this same statement is permitted on Windows. To avoid problems caused by such
differences, it is best to adopt a consistent convention, such as
always creating and referring to databases and tables using lowercase
names. This convention is recommended for maximum portability and ease
of use.
How table and database names are stored on disk and used in MySQL is
affected by the lower_case_table_names system variable, which you can
set when starting mysqld. lower_case_table_names can take the values
shown in the following table. On Unix, the default value of
lower_case_table_names is 0. On Windows the default value is 1. On Mac
OS X, the default value is 2.
Value Meaning 0 Table and database names are stored on disk using the
lettercase specified in the CREATE TABLE or CREATE DATABASE statement.
Name comparisons are case sensitive. You should not set this variable
to 0 if you are running MySQL on a system that has case-insensitive
file names (such as Windows or Mac OS X). If you force this variable
to 0 with --lower-case-table-names=0 on a case-insensitive file system
and access MyISAM tablenames using different lettercases, index
corruption may result. 1 Table names are stored in lowercase on disk
and name comparisons are not case sensitive. MySQL converts all table
names to lowercase on storage and lookup. This behavior also applies
to database names and table aliases. 2 Table and database names are
stored on disk using the lettercase specified in the CREATE TABLE or
CREATE DATABASE statement, but MySQL converts them to lowercase on
lookup. Name comparisons are not case sensitive. This works only on
file systems that are not case sensitive! InnoDB table names are
stored in lowercase, as for lower_case_table_names=1. If you are using
MySQL on only one platform, you do not normally have to change the
lower_case_table_names variable from its default value. However, you
may encounter difficulties if you want to transfer tables between
platforms that differ in file system case sensitivity. For example, on
Unix, you can have two different tables named my_table and MY_TABLE,
but on Windows these two names are considered identical. To avoid data
transfer problems arising from lettercase of database or table names,
you have two options:
Use lower_case_table_names=1 on all systems. The main disadvantage
with this is that when you use SHOW TABLES or SHOW DATABASES, you do
not see the names in their original lettercase.
Use lower_case_table_names=0 on Unix and lower_case_table_names=2 on
Windows. This preserves the lettercase of database and table names.
The disadvantage of this is that you must ensure that your statements
always refer to your database and table names with the correct
lettercase on Windows. If you transfer your statements to Unix, where
lettercase is significant, they do not work if the lettercase is
incorrect.
Exception: If you are using InnoDB tables and you are trying to avoid
these data transfer problems, you should set lower_case_table_names to
1 on all platforms to force names to be converted to lowercase.
If you plan to set the lower_case_table_names system variable to 1 on
Unix, you must first convert your old database and table names to
lowercase before stopping mysqld and restarting it with the new
variable setting. To do this for an individual table, use RENAME
TABLE:
RENAME TABLE T1 TO t1; To convert one or more entire databases, dump
them before setting lower_case_table_names, then drop the databases,
and reload them after setting lower_case_table_names:
Use mysqldump to dump each database:
mysqldump --databases db1 > db1.sql mysqldump --databases db2 >
db2.sql ... Do this for each database that must be recreated.
Use DROP DATABASE to drop each database.
Stop the server, set lower_case_table_names, and restart the server.
Reload the dump file for each database. Because lower_case_table_names
is set, each database and table name will be converted to lowercase as
it is recreated:
mysql < db1.sql mysql < db2.sql ... Object names may be considered
duplicates if their uppercase forms are equal according to a binary
collation. That is true for names of cursors, conditions, procedures,
functions, savepoints, stored routine parameters and stored program
local variables. It is not true for names of names of columns,
constraints, databases, statements prepared with PREPARE, tables,
triggers, users, and user-defined variables.
If you are using MySQL you can set table and column name case sensitive in my.conf by using following directive
set-variable = lower_case_table_names=1
Do not forget the restart server after update. It would be better if you use same column names in all servers