MySQL: 2. database-related operations

1. Create a database CREATE DATABASE

CREATE DATABASE [IF NOT EXISTS] <数据库名>
[[DEFAULT] CHARACTER SET <字符集名>] 
[[DEFAULT] COLLATE <校对规则名>];

[]The content is optional. Syntax is as follows:

  • <数据库名>: Creating a database name. MySQL's catalog will indicate how the data storage area MySQL database, the database name must conform to the operating system folder naming conventions, we can not start with a number, as far as possible to have practical significance. Note that a case-insensitive in MySQL.
  • IF NOT EXISTS: Judge before creating a database to perform the operation only if the database does not yet exist. This option can be used to avoid repeating mistakes and create the database already exists.
  • [DEFAULT] CHARACTER SET: Specify the character set of the database. Specifies the character set object is to avoid distortion of the data stored in the database. If you do not specify the character set when you create a database, then use the system's default character set.
  • [DEFAULT] COLLATE: Default collation specified character set.

MySQL character set (CHARACTER) and collation (COLLATION) are two different concepts. Embodiment is used to define the character set stored MySQL string collation string comparison is defined manner.

Examples :

  • The easiest to create a database
mysql> CREATE DATABASE test_db;
  • Whether created before the examination already exists
mysql> CREATE DATABASE IF NOT EXISTS test_db;
  • Specify the character set and collation when creating a database
mysql> CREATE DATABASE IF NOT EXISTS test_db_char
    -> DEFAULT CHARACTER SET utf8
    -> DEFAULT COLLATE utf8_chinese_ci;
Query OK, 1 row affected (0.03 sec)

View defining declaration database

SHOW CREATE DATABASE <数据库名>;

For example:
The following results illustrate the database testcharacter set, the default latin1character set.

mysql> show create database test;
+----------+-----------------------------------------------------------------+
| Database | Create Database                                                 |
+----------+-----------------------------------------------------------------+
| test     | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)

View support for all character sets

SHOW CHAR SET;

E.g:

mysql> SHOW CHAR SET;
+----------+---------------------------------+---------------------+--------+
| Charset  | Description                     | Default collation   | Maxlen |
+----------+---------------------------------+---------------------+--------+
| big5     | Big5 Traditional Chinese        | big5_chinese_ci     |      2 |
| dec8     | DEC West European               | dec8_swedish_ci     |      1 |
| cp850    | DOS West European               | cp850_general_ci    |      1 |
| hp8      | HP West European                | hp8_english_ci      |      1 |
| koi8r    | KOI8-R Relcom Russian           | koi8r_general_ci    |      1 |
| latin1   | cp1252 West European            | latin1_swedish_ci   |      1 |
| latin2   | ISO 8859-2 Central European     | latin2_general_ci   |      1 |
| swe7     | 7bit Swedish                    | swe7_swedish_ci     |      1 |
| ascii    | US ASCII                        | ascii_general_ci    |      1 |
| ujis     | EUC-JP Japanese                 | ujis_japanese_ci    |      3 |
| sjis     | Shift-JIS Japanese              | sjis_japanese_ci    |      2 |
| hebrew   | ISO 8859-8 Hebrew               | hebrew_general_ci   |      1 |
| tis620   | TIS620 Thai                     | tis620_thai_ci      |      1 |
| euckr    | EUC-KR Korean                   | euckr_korean_ci     |      2 |
| koi8u    | KOI8-U Ukrainian                | koi8u_general_ci    |      1 |
| gb2312   | GB2312 Simplified Chinese       | gb2312_chinese_ci   |      2 |
| greek    | ISO 8859-7 Greek                | greek_general_ci    |      1 |
| cp1250   | Windows Central European        | cp1250_general_ci   |      1 |
| gbk      | GBK Simplified Chinese          | gbk_chinese_ci      |      2 |
| latin5   | ISO 8859-9 Turkish              | latin5_turkish_ci   |      1 |
| armscii8 | ARMSCII-8 Armenian              | armscii8_general_ci |      1 |
| utf8     | UTF-8 Unicode                   | utf8_general_ci     |      3 |
| ucs2     | UCS-2 Unicode                   | ucs2_general_ci     |      2 |
| cp866    | DOS Russian                     | cp866_general_ci    |      1 |
| keybcs2  | DOS Kamenicky Czech-Slovak      | keybcs2_general_ci  |      1 |
| macce    | Mac Central European            | macce_general_ci    |      1 |
| macroman | Mac West European               | macroman_general_ci |      1 |
| cp852    | DOS Central European            | cp852_general_ci    |      1 |
| latin7   | ISO 8859-13 Baltic              | latin7_general_ci   |      1 |
| utf8mb4  | UTF-8 Unicode                   | utf8mb4_general_ci  |      4 |
| cp1251   | Windows Cyrillic                | cp1251_general_ci   |      1 |
| utf16    | UTF-16 Unicode                  | utf16_general_ci    |      4 |
| utf16le  | UTF-16LE Unicode                | utf16le_general_ci  |      4 |
| cp1256   | Windows Arabic                  | cp1256_general_ci   |      1 |
| cp1257   | Windows Baltic                  | cp1257_general_ci   |      1 |
| utf32    | UTF-32 Unicode                  | utf32_general_ci    |      4 |
| binary   | Binary pseudo charset           | binary              |      1 |
| geostd8  | GEOSTD8 Georgian                | geostd8_general_ci  |      1 |
| cp932    | SJIS for Windows Japanese       | cp932_japanese_ci   |      2 |
| eucjpms  | UJIS for Windows Japanese       | eucjpms_japanese_ci |      3 |
| gb18030  | China National Standard GB18030 | gb18030_chinese_ci  |      4 |
+----------+---------------------------------+---------------------+--------+
41 rows in set (0.00 sec)

2. Check database SHOW DATABASES

In MySQL, you can use the SHOW DATABASESstatement to view or display the database user privileges within the current range .
View database syntax is:

SHOW DATABASES [LIKE '数据库名'];

Syntax is as follows:

  • LIKEClause is optional, used to match the specified database name. LIKEPartial match clause can also be an exact match.
  • Database name from the single quotes ' 'surrounded.

Examples :

  • View all databases
mysql> SHOW DATABASES;
  • View exact match for the name of the database
mysql> SHOW DATABASES LIKE 'test';
+-----------------+
| Database (test) |
+-----------------+
| test            |
+-----------------+
1 row in set (0.00 sec)
  • View at mythe beginning of the database
mysql> SHOW DATABASES LIKE 'my%';
+----------------+
| Database (my%) |
+----------------+
| mysql          |
+----------------+
1 row in set (0.00 sec)
  • View the schemadatabase at the end of
mysql> SHOW DATABASES LIKE '%schema';
+--------------------+
| Database (%schema) |
+--------------------+
| information_schema |
| performance_schema |
+--------------------+
2 rows in set (0.00 sec)
  • View name contains sqlthe database
mysql> SHOW DATABASES LIKE '%sql%';
+------------------+
| Database (%sql%) |
+------------------+
| mysql            |
+------------------+
1 row in set (0.00 sec)

3. Modify the database ALTER DATABASE

Can only be modified character set and collation used by the database in MySQL database, these characteristics are stored in the database db.optfile.

Modify the database syntax is:

# 修改字符集
ALTER DATABASE <数据库名> [DEFAULT] CHARACTER SET <字符集名>;
# 修改校对规则
ALTER DATABASE <数据库名> [DEFAULT] COLLATE <校对规则名>;

# 同时修改字符集和校对规则
ALTER TABLE <数据库名> [DEFAULT] CHARACTER SET utf8 [DEFAULT] COLLATE utf8_general_ci;

Syntax is as follows:

  • ALTER DATABASEUsed to change the global database characteristics.
  • Use ALTER DATABASEneed to get the database ALTERpermissions.
  • The database name can be ignored, then the statement corresponds to the default database.
  • CHARACTER SETClause to change the default database character set.

Examples :

  • The data set testthe default character set is changed utf8, the default collation changedutf8_general_ci
mysql> ALTER DATABASE test DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)

mysql> SHOW CREATE DATABASE test;
+----------+---------------------------------------------------------------+
| Database | Create Database                                               |
+----------+---------------------------------------------------------------+
| test     | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)

4. Delete database DROP DATABASE

When you need to delete a database has been created, you can use DROP DATABASEstatement. The syntax is:

DROP DATABASE [IF EXISTS] <数据库名>;

Syntax is as follows:

  • <数据库名>Specify the database name to be deleted.
  • IF EXISTSUsed to prevent errors from occurring when the database does not exist.
  • DROP DATABASEDelete all tables in the database and delete the database. Be very careful when using this statement, after executing the command, MySQL will not give any confirmation prompt .
  • If you want to use DROP DATABASE, you need to get the database DROPpermissions.

Note: After MySQL installation, the system will automatically create a database called the two systems information_schema and mysql, the database system and database to store some relevant information, if you delete these two databases, MySQL will not work properly.

Examples :

  • Create and delete test_deldatabase
mysql> CREATE DATABASE test_del;
Query OK, 1 row affected (0.01 sec)

mysql> DROP DATABASE test_del;
Query OK, 0 rows affected (0.05 sec)

mysql> SHOW DATABASES LIKE 'test_del';
Empty set (0.00 sec)
  • Plus deleted IF EXISTSsentence
mysql> DROP DATABASE test_del;
ERROR 1008 (HY000): Can't drop database 'test_del'; database doesn't exist

mysql> DROP DATABASE IF EXISTS test_del;
Query OK, 0 rows affected, 1 warning (0.00 sec)

5. Select Database USE

When used CREATE DATABASEafter creating a database statement, the database does not automatically become the database, need USEto specify the current database.

The syntax is:

USE <数据库名>;

This statement may notify the MySQL <数据库名>database as indicated by the current database. The database remains the default database until the end of the discourse, or until I met a different USEstatement.

Only use USEafter the statement to specify a database as the current database to the data objects stored in the database and perform operations.

Examples :

  • Use test_dbDatabase
mysql> USE test_db;
Database changed

reference

MySQL Tutorial: MySQL database learning book (Mastering)

Published 19 original articles · won praise 10 · views 4585

Guess you like

Origin blog.csdn.net/Dragon_Prince/article/details/104202766