To create a MySQL database table, you need to connect to the database server, create (if necessary) and switch to a database, then use the CREATE TABLE statement to specify the column names and data types, and optionally specify additional column properties, and finally Execute the create statement. The steps include: Connect to the database server Create the database (optional) Switch to the created database Use the CREATE TABLE statement to create the table Specify column properties (optional) Execute the CREATE statement
Steps to create a MySQL database table
1. Connect to the MySQL database server
2. Create a database (optional)
<code>CREATE DATABASE database_name;</code>
3. Switch to the created database
<code>USE database_name;</code>
4. Create a table
<code>CREATE TABLE table_name ( column1_name data_type, column2_name data_type, ... );</code>
column_name
is the column The name. data_type
is the data type of the column. 5. Specify column attributes (optional)
You can specify additional attributes for columns, for example:
PRIMARY KEY
: Specify this column as the primary key, which uniquely identifies each row in the table. NOT NULL
: Specifies that NULL values are not allowed for this column. DEFAULT
: Specify the default value of the column. 6. Execute the create statement
<code>执行创建语句;</code>
Example:
The following statement creates a table named students
, which contains id
, name
and age
columns:
<code>CREATE TABLE students ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, age INT DEFAULT 18 );</code>
The above is the detailed content of What are the steps to create a database table in mysql. For more information, please follow other related articles on the PHP Chinese website!