Create and Delete tables in mysql

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.