MySQL Basics: Create, view, modify, and delete tables

Source: Internet
Author: User

Mysql Create, view, modify, and delete tables

1. Create a table

Create a syntax form for a table:

CREATE TABLE Table Name (property name  data type  constraint,                                 property name  data type  constraint,                                 ...                               ) ENGINE=DEFAULT CHARSET= character encoding name;  

Table of Constraint conditions:

  

Attention:

1) Multiple primary keys can be added. Form: added in parentheses after the table name: PRIMARY KEY (field name 1, field Name 2, ...) ;

2) storage engine and character encoding can be omitted and not written;

3) When setting the table field name to the default value, you need to add "default" after the "defult" keyword;

4) foreign key settings:

The syntax rules are:

CONSTRAINT  Foreign key aliases  FOREIGNkey (attribute 1.  1, attribute 1. 2         , ...)   REFERENCES  Table Name (Property 2.  1, attribute 2. 2, ...)

  

2. View the table

Three viewing tables in the form of:

DESCRIBE   table name; DESC    table name; SHOW  CREATE table   table name;

3. Modify the table

1) Modify the table name:

ALTER  TABLE  Old table name   RENAME   

2) Modify the data type of the field:

ALTER  TABLE  Table name   MODIFY   property name   data type;

3) Modify the field name:

ALTER  TABLE  Table name   change   old property name new   data type of new property name   ;

4) add field:

ALTER  TABLE  Table name   ADD   Property name 1   data type   constraint   [First| After property name 2];

  Note: The first, after property name 2 is the place to add the added field to the beginning or after the property name 2. Where [] does not need to be added.

5) Delete the field:

ALTER  TABLE  Table name   DROP   attribute name;

6) Change the storage engine for the table:

ALTER  TABLE  Table name   engine= name of the storage engines;

7) Delete the foreign KEY constraint for the table:

ALTER  TABLE  Table name  DROP  FOREIGNkey    foreign key alias;

4. Delete a table

DROP  TABLE   Table name;

Attention:

When you delete a table, you need to be aware of whether it is associated with another table. If there is an association with another table, you need to delete the associated table or delete the association relationship before you can delete the table.

MySQL Basics: Create, view, modify, and delete tables

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.