Create and delete a table in MySQL _ MySQL

Source: Internet
Author: User
This article describes how to create and delete a table in MySQL. This article describes how to create a table, create a temporary table, and view the created mysql table. For more information, see Create a table

Simple method

The code is as follows:


Create table person (
Number INT (11 ),
Name VARCHAR (255 ),
Birthday DATE
);

Or

The code is as follows:


Create table if not exists person (
Number INT (11 ),
Name VARCHAR (255 ),
Birthday DATE
);

View the mysql creation table:

The code is as follows:


Show create table person;

Create table 'person '(
'Number' int (11) default null,
'Name' varchar (255) default null,
'Birthday' date DEFAULT NULL
) ENGINE = MyISAM default charset = utf8;

View all columns in the table:

The code is as follows:


Show full columns from person;
+ ---------- + -------------- + ----------------- + ------ + ----- + --------- + ------- + ------------------------------- + --------- +
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+ ---------- + -------------- + ----------------- + ------ + ----- + --------- + ------- + ------------------------------- + --------- +
| Number | int (11) | NULL | YES | NULL | select, insert, update, references |
| Name | varchar (255) | utf8_general_ci | YES | NULL | select, insert, update, references |
| Birthday | date | NULL | YES | NULL | select, insert, update, references |
+ ---------- + -------------- + ----------------- + ------ + ----- + --------- + ------- + ------------------------------- + --------- +

Create a temporary table:

The code is as follows:


Create temporary table temp_person (
Number INT (11 ),
Name VARCHAR (255 ),
Birthday DATE
);

When creating a table, you can use the TEMPORARY keyword. The TEMPORARY table is visible only during the current connection. When the connection is closed, the TEMPORARY table is automatically canceled. This means that two different connections can use the same temporary table name. at the same time, the two temporary tables do not conflict with each other and do not conflict with the original non-temporary table with the same name. (The original table is hidden until the temporary table is canceled .) You must have the create temporary tables permission to CREATE a TEMPORARY table.

IF the table already EXISTS, use the keyword if not exists to prevent errors.

The code is as follows:


Create table if not exists person2 (
Number INT (11 ),
Name VARCHAR (255 ),
Birthday DATE
);

Note: it is not verified whether the structure of the original TABLE is the same as that of the TABLE in the create table statement. Note: IF you use if not exists in the create table... SELECT statement, records selected from the SELECT statement will be inserted no matter whether the table exists or NOT.

Add a SELECT statement at the end of the create table statement and CREATE a TABLE based on the TABLE.

The code is as follows:


Create table new_tbl SELECT * FROM orig_tbl;

Note: columns created using the SELECT statement are attached to the right of the table, instead of overwriting the table.

The code is as follows:


Mysql> SELECT * FROM foo;
+ --- +
| N |
+ --- +
| 1 |
+ --- +
Mysql> create table bar (m INT) SELECT n FROM foo;
Mysql> SELECT * FROM bar;
+ ------ + --- +
| M | n |
+ ------ + --- +
| NULL | 1 |
+ ------ + --- +

You can also explicitly specify a type for a generated column.

The code is as follows:


Create table foo (a tinyint not null) SELECT B + 1 AS a FROM bar;

Use LIKE to create an empty table based on the definitions of other tables (including all column attributes and indexes defined in the original table:

The code is as follows:


Create table new_tbl LIKE orig_tbl;

Create a table with a primary key, unique index, and common index:

The code is as follows:


Create table 'people '(
'Eagleid' smallint (6) not null AUTO_INCREMENT,
'Firstname' char (50) not null,
'Lastname' char (50) not null,
'Age' smallint (6) not null,
'Townid' smallint (6) not null,
Primary key ('peopleid '),
Unique key 'unique _ fname_lname '('firstname', 'lastname '),
KEY 'fname _ lname_age '('firstname', 'lastname', 'age ')
);

The peopleid is the primary key. a unique index is created for the columns firstname and lastname. a common index is created for the columns firstname, lastname, and age.

Delete table

The code is as follows:


Drop table tbl_name;

Or

The code is as follows:


Drop table if exists tbl_name;

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.