MySQL CREATE TABLE and delete table statements

Source: Internet
Author: User
Tags mysql create mysql create table

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.

Related Article

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.