command requirements for creating tables:
The name of the table
table field Name
Definition of each field
Grammar:
The following is a common SQL syntax to create a MySQL table:
CREATE TABLE table_name (column_name column_type);
Now, we will create the following Tutorial database table.
The code is as follows |
Copy Code |
TUTORIALS_TBL ( tutorial_id INT not NULL auto_increment, Tutorial_title VARCHAR (m) not NULL, Tutorial_author VARCHAR () not NULL, Submission_date date, PRIMARY KEY (tutorial_id) ); |
Here are a few items to note:
Use the field property not NULL because we do not want this field to be null. So if the user tries to create a record with a null value, then MySQL generates an error.
Field Properties Auto_increment tells MySQL to continue incrementing the next available Number ID field.
The keyword PRIMARY key is used to define a column as the primary key. You can define a primary key by using multiple columns separated by commas.
To create a table from a command prompt:
It is easy to create a MySQL table from the mysql> prompt. You will create a table using the SQL command creation table.
Example:
Here is an example that creates a tutorials_tbl:
The code is as follows |
Copy Code |
root@host# Mysql-u Root-p Enter password:******* mysql> use tutorials; Database changed Mysql> CREATE TABLE Tutorials_tbl ( -> tutorial_id INT not NULL auto_increment, -> tutorial_title VARCHAR (MB) not NULL, -> Tutorial_author VARCHAR () 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 give a semicolon (;) at the end of the SQL command.
Grammar:
The following is a generic SQL syntax discard (delete) MySQL table:
DROP TABLE table_name;
To delete a table from a command prompt:
You only need to execute the drop TABLE SQL command at the mysql> prompt.
Example:
Here is an example that deletes the table tutorials_tbl:
The code is as follows |
Copy Code |
root@host# Mysql-u Root-p Enter password:******* mysql> use tutorials; Database changed mysql> DROP TABLE Tutorials_tbl Query OK, 0 rows affected (0.8 sec) Mysql> |
Note that if you want to create a table in Php+mysql is the same as deleting a table method just put the above statement in SQL and then use the mysql_query to execute the same OH.