Detailed description of the tutorial for creating a table in MySQL, detailed description of mysql
Command requirements for creating a table:
- Table Name
- Table field name
- Definition of each field
Syntax:
Below is a general SQL syntax to create a MySQL table:
CREATE TABLE table_name (column_name column_type);
Now, we will create the following tutorial database table.
tutorials_tbl( tutorial_id INT NOT NULL AUTO_INCREMENT, tutorial_title VARCHAR(100) NOT NULL, tutorial_author VARCHAR(40) NOT NULL, submission_date DATE, PRIMARY KEY ( tutorial_id ));
The following items need to be described:
- The field attribute not null is used because we do NOT want this field to be NULL. Therefore, if you try to create a record with a NULL value, MySQL will generate an error.
- The field property AUTO_INCREMENT tells MySQL to continue increasing, and the id field of the next available number.
- The primary key keyword is used to define a column as the primary key. You can use multiple columns separated by commas to define a primary key.
Create a table from a command prompt:
It is easy to create a mysql table from the MySQL> prompt. You will use the SQL command CREATE TABLE to CREATE a TABLE.
Example:
The following is an example of creating tutorials_tbl:
root@host# mysql -u root -pEnter password:*******mysql> use TUTORIALS;Database changedmysql> CREATE TABLE tutorials_tbl( -> tutorial_id INT NOT NULL AUTO_INCREMENT, -> tutorial_title VARCHAR(100) NOT NULL, -> tutorial_author VARCHAR(40) NOT NULL, -> submission_date DATE, -> PRIMARY KEY ( tutorial_id ) -> );Query OK, 0 rows affected (0.16 sec)mysql>
Note: MySQL terminates the command until you end with a semicolon.
Use a PHP script to create a MySQL table:
To create any existing database in a new table, use the PHP function mysql_query (). The second parameter and the correct SQL command will be used to create a table.
Example:
The following is an example of using a PHP script to create a table: