In MySQL, you can use the "CREATE TABLE" statement to create a table, which is written as: "CREATE TABLE table name ([table definition option])[table option][partition option];", where "[ Table definition option]" is in the format of "column name 1 type 1 [,…] column name n type n".
(Recommended tutorial: mysql video tutorial)
After creating the database, the next step is to create the database Create a data table. The so-called creating a data table refers to creating a new table in the already created database.
The process of creating a data table is the process of specifying the attributes of the data columns, and it is also the process of implementing data integrity (including entity integrity, referential integrity and domain integrity) constraints.
Basic syntax
In MySQL, you can use the CREATE TABLE statement to create a table. The syntax format is:
CREATE TABLE <表名> ([表定义选项])[表选项][分区选项];
Among them, the format of [Table Definition Options] is:
<列名1> <类型1> [,…] <列名n> <类型n>
The CREATE TABLE command syntax is more, which mainly consists of table creation definition (create-definition), Composed of table options (table-options) and partition options (partition-options).
Here we first describe a simple example of creating a new table, and then focus on some main syntax knowledge points in the CREATE TABLE command.
The main syntax and usage instructions of the CREATE TABLE statement are as follows:
CREATE TABLE: used to create a table with a given name, you must have table CREATE permissions.
Field name | Data type | Remarks |
---|---|---|
id | INT(ll) | Employee number |
name | VARCHAR(25) | Employee name |
deptld | INT(ll) | Department number |
salary | FLOAT | Salary |
#Select the database test_db to create the table, create the tb_emp1 data table, and enter the SQL statement and running results as shown below.
mysql> USE test_db; Database changed mysql> CREATE TABLE tb_emp1 -> ( -> id INT(11), -> name VARCHAR(25), -> deptId INT(11), -> salary FLOAT -> ); Query OK, 0 rows affected (0.37 sec)
After the statement is executed, a data table named tb_emp1 is created. Use the SHOW TABLES; statement to check whether the data table is created successfully, as shown below.
mysql> SHOW TABLES; +--------------------+ | Tables_in_test_db | +--------------------+ | tb_emp1 | +--------------------+ 1 rows in set (0.00 sec)
Related recommendations: php training
The above is the detailed content of What is the way to write the table creation statement in mysql?. For more information, please follow other related articles on the PHP Chinese website!