Create and Delete tables in mysql
The table creation command must be:
- The name of the tables Table.
- Region field name
- Define each field (type, length, etc)
Syntax
The following is a general SQL syntax used to create a MySQL table:
CREATE TABLE table_name (column_name column_type);
Now, we will create the following table in the test database.
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 ));
Here, some data items need to be explained:
The values field uses the not null attribute because we do NOT want the value of this field to be NULL. Therefore, if you try to create a record with a NULL value, MySQL will generate an error.
The AUTO_INCREMENT attribute of the dimensions field tells MySQL to automatically add the next available id for the id field.
The PRIMARY keyword primary key is used to define this column as the primary key. You can use commas to separate multiple columns to define the primary key.
1. Create a table from a command prompt
At the mysql> prompt, it is easy to create a MySQL table. Use the SQL command CREATE TABLE to CREATE a TABLE.
Example
The following is an example to create a table: 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 does not terminate the command until a semicolon (;) is given, indicating that the SQL command has ended.
It is easy to delete an existing MySQL table, but you must be very careful. When you delete any existing table, the lost data cannot be recovered.
Syntax
This is the general SQL syntax used to delete MySQL tables:
DROP TABLE table_name ;
1. delete a table from the command line prompt
This only needs to be executed at the MySQL> promptDROP TABLE SQLCommand.
Example
The following is an example of deleting a table: tutorials_tbl
root@host# mysql -u root -pEnter password:mysql> use test;Database changedmysql> DROP TABLE tutorials_tblQuery OK, 0 rows affected (0.8 sec)mysql>
Summary
The above is a detailed description of mysql table creation and deletion instances. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!