Create and delete data tables in mysql

Source: Internet
Author: User
In mysql, apart from creating and deleting databases, we also need to create and delete data tables. The following describes how to use the command line to create and delete data tables.

In mysql, apart from creating and deleting databases, we also need to create and delete data tables. The following describes how to use the command line to create and delete data tables.

Create data table exploitation

Command: create table <表名> ( <字段名1> <类型1> [,.. <字段名n> <类型n> ]);

Syntax: CREATE [TEMPORARY] TABLE [if not exists] tbl_name [(create_definition,...)] [table_options] [select_statement]

Example

The Code is as follows:

Mysql> create table MyClass (
> Id int (4) not null primary key auto_increment,
> Name char (20) not null,
> Sex int (4) not null default '0 ',
> Degree double (16, 2 ));

Detailed description parameters

TEMPORARY: This keyword indicates that the table created with MySQL create table is a TEMPORARY table, which will automatically disappear after the end of the current session. Temporary tables are mainly used in stored procedures. For MySQL that does not currently support stored procedures, this keyword is generally not used.

If not exists: in fact, the create table operation is performed only when the table does NOT exist. This option can be used to prevent the table from being created again.

Tbl_name: name of the table you want to create. The table name must comply with the identifier rules. Generally, only letters, numbers, and underscores are used in the table name. For example, titles, our_sales, and my_user1 should be regarded as standardized table names.

Create_definition: This is the key part of the MySQL create table statement. This section defines the attributes of each column in the table.

Delete table command: Drop table <表名>

Mysql> drop table MyClass;

Drop table is used to cancel one or more tables. You must have the DROP permission for each table. All table data and table definitions will be canceled, so be careful when using this statement!

Later I shared some table-related commands.

Get table structure

Command: desc table name or show columns from Table Name

The Code is as follows:

Mysql> desc MyClass;
Mysql> show columns from MyClass;


Insert record

Command: insert <表名> [( <字段名1> [,.. <字段名n> ])] Values (value 1) [, (value n)]

For example, insert two records into the MyClass table. The two records indicate that the result of Tom numbered 1 is 96.45, and the result of Joan numbered 2 is 82.99, wang, numbered 3, scored 96.5.

Mysql> insert into MyClass values (1, 'Tom ', 96.45), (2, 'job', 82.99), (2, 'wang', 96.59 );

Note: insert into can only insert one record into the table at a time.

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.