mysql-Database Operations

Source: Internet
Author: User

1. Create a table:

1 Create Table table name (2     column name  type  can be empty,3     column name  type  can be empty 4 ) ENGINE=DEFAULT CHARSET=UTF8
Build Table
1         Default value, you can specify a default value when creating a column, and automatically add a default value of 2              when inserting data if it is not actively set Create Table tb1 (3                 int  not NULL 2 , 4                 int  not NULL 5             )
Default Value
1 self-increment, if you set the self-increment column for a column, you do not have to set this column when inserting data, and the default will be self-increment (only one self-increment in the table)2             Create TableTB1 (3Nidint  not NULLAuto_incrementPrimary Key,4Numint NULL5             )6 or7             Create TableTB1 (8Nidint  not NULLAuto_increment,9Numint NULL,Ten                 Index(NID) One             ) AAttention:1, for self-increment columns, must be an index (with a primary key).  -                  2, and for self-increment, you can set the step and start values
Self-increment
primary key, a special unique index that does not allow null values, and if the primary key uses a single column, its value must be unique, and if it is multiple columns, its combination must be unique. Create Tabletb1 (Nidint  not NULLAuto_incrementPrimary Key, Numint NULL) orCreate Tabletb1 (Nidint  not NULL, Numint  not NULL,                Primary Key(nid,num))
PRIMARY Key
1 foreign key, a special index, can only be the specified content2creatTableColor (3Nidint  not NULL Primary Key,4NameChar( -) not NULL5             )6 7             Create TableFruit (8Nidint  not NULL Primary Key,9SmtChar( +)NULL ,Tencolor_idint  not NULL, One                 constraintFk_ccForeign Key(COLOR_ID)ReferencesColor (NID) A)
FOREIGN Key

2. Delete a table

Drop Table -- Delete a table
Delete Table

3. Clear the Table

Delete  from -- Clear All data truncate Table -- Clear all data and reset the table
Clear

4. Modify the table

1To add a column:Alter TableTable nameAddColumn Name Type2To delete a column:Alter TableTable nameDrop columnColumn Name3 To Modify a column:4         Alter TableTable name ModifycolumnColumn name type;--type5         Alter TableTable name change original column name new column name type;--column name, type6   7 To add a primary key:8         Alter TableTable nameAdd Primary Key(column name);9 To delete a primary key:Ten         Alter TableTable nameDrop Primary Key; One         Alter TableTable name Modify Column nameint,Drop Primary Key; A    -To add a foreign key:Alter TableFrom the tableAdd constraintForeign Key Name (shape: fk_ from Table _ Main Table)Foreign KeyFrom a table (Foreign key field)Referencesprimary table (primary key field); -To delete a foreign key:Alter TableTable nameDrop Foreign KeyFOREIGN Key Name the    -To modify the default value:ALTER TABLETestalter_tblALTERISET DEFAULT  +; -Delete default values:ALTER TABLETestalter_tblALTERIDROP DEFAULT;
Modify Table 6666

mysql-Database Operations

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.