mysql-- create a table, modify table, drop table (concept explained in detail)

First, create a table 

create table table name ( 

                      column name Data type 1 1 [integrity constraints], 
                      Column Name Data Type 2 2 [integrity constraints], 
                      Column Name Data Type 3 3 [integrity constraints], 
                      column name data type 4 4 [integrity constraints], 
                      ...... 
                      column name data type n n 
                  ); table name: the name of the table to be created column name: indicates the name of a field in a table data type: type of data specified parameter complete of constraints: some special constraints specified field 
: Note










 1 , when creating a table, you first need to select the database 'use the database name', otherwise it will display an error

 2 when a table can have one or more attributes, definitions , capitalization can be separated by a comma between each attribute, after the last comma property does not require





 
two, integrity constraints table primary key   : the attribute that identifies the primary key of the table corresponding to the unique identifier tuple




Key Foreign : This attribute identifies the foreign key for the table, with a contact of a table's primary key 

Not null : identifies the attribute can not be null UNIQUE : identifies the attribute value is unique default : set a default value for the property AUTO_INCREMENT : value that identifies the attribute is automatically increased 
three, integrity constraints













 a set table's primary key 

main purpose of the primary key is to help the fastest to find a record in the table, with the proviso primary key must be met is that the primary key must be unique; 

table arbitrary two values of the primary key must be different, the primary key is non-null value; 

primary key can be a single field, or may be a combination of a plurality of fields; 


example 1: set a single field of the primary key 

Create table example0 (stu_id int primary key , 
                        stu_name VARCHAR ( 20 is ), 
                        stu_sex Boolean 
                      ); 

example 2: set a plurality of the primary key fields
 
create table example1 ( 
                        stu_id int, 
                        COURSE_ID int, 
                        Grade a float, Primary Key (stu_id, COURSE_ID) 
                      );
                       


 2 , foreign key to the table 

provided principle is to rely on the parent table in the database already present in the primary key foreign key, foreign key can be null 

foreign keys are associated relationship with the parent table of the table




 3 , table set -null constraints
 
nonempty refers to the value of the field can not be null (NULL) , will ensure that all non-empty records constraint in the field has value of 

non-empty table is constraint setting means to create a table to add some special fields table NOT nULL constraint 

example: 

Create table Example4 (ID int Not null Primary KYE , 
                        name VARCHAR ( 20 is) Not null , 
                        stu_id int,
                        constraint d_fk Foreign Key (stu_id)
                            References example1 (stu_id) 
                       ;) 

: Note stu_id field is a foreign key, d_fk foreign key alias, example1 table is the parent table example4 tables, foreign key example4 table dependent on the parent table's primary key stu_id




 . 4 , table settings uniqueness constraint
 
uniqueness means that all the records in the value of the field can not be repeated
 
for example: 

Create table example5 (ID int Primary Key, 
                       stu_id int uNIQUE , 
                       name VARCHAR ( 20 is) not null 
                      );


 . 5 , the attribute value table automatically increased
 
auto_increment is MYSQL a particular constraint in which the table is the main role is newly inserted automatically generate a unique ID for a new record ; 

a table can have only one field using auto_increment constraints, and the main part of the key field must ;
 
auto_increment constraint field can be any integer type; default value of this field is from 1 began to increase 
example: 
Create Table Example6 (ID int Primary Key auto_increment , 
                       stu_id int UNIQUE , 
                       name VARCHAR (




 20) not null 
                      ); 


Note: If the first record set initial value of this field, the new record will begin to increase increased from an initial value of


 6 , the property table set the default value
 
when you create a table can be a default value of the specified field in a table, 

if the insert a new record is not, then the database system will automatically insert the default value for this field assigned to this field 



for example: 

Create table example7 (ID int Primary Key AUTO_INCREMENT , 
                       stu_id int UNIQUE, 
                       name VARCHAR ( 20 is) Not null , 
                       Dictionary Dictionary English VARCHAR ( 20 is) default ' ZERO ' , 
                       Math a float default 0 , 
                       Computer a float default 0 
                      ); 


NOTE: If there is no default value specified field default keyword has not designated a non-empty field, then default is an empty field is NULL 







 
four, see table structure 

Display structure refers to: define the table already exists in the database view

 1 to view the table of the basic structure of the statement: DESCRIBE 

DESCRIBE statement can view the basic definition table: including field names , field data type, primary key and whether the default values and the like; 

syntax describe the following statement: describe table name; 
describe may be abbreviated as desc
                              





 2 , see table detailed structure of the sentenceshow create table

This statement can view information table field names, field data types, integrity constraints and so on. In addition to the default table can also view the storage engine and character encoding 

show create table syntax as follows: 
                                       show create table name; 


to clean syntax displayed in the following format: 
                                       show create table table \ G; 


Example: 
      show create table example \ G; 










V. modify table

 1 , modifying the table name

 syntax: ALTER table old table name rename [to] the new name; 


example: alter table example1 rename user; 

NOTE: modified example1 table does not exist there is only a new table named user, but its content is the same, just changed name


2 , the data type modify the field of syntax: the ALTER the table table name modify the attribute name data type; Note: the table refers you want to modify data type the name of the field in the table; property name means: to modify the name of the field data type; data type refers to: new data types modified Example: alter table Modify User name VARCHAR ( 30 ); prior to modification with modifications desc user desc user will find the data type name field changed 3 , modify the field name syntax: ALTER TABLE Table name change old new attribute property name type the name of the new data; : Examples Table example1 Change stu_sex Sex ALTER int ( 2 ;) the new data type refers to the type of the modified data, such as no modification, new data is arranged into the same type of original: annotation may modify the integer type when affect the data table, special attention is the character type of the field is preferably not modified to an integer or floating point type type 4 , increasing the field syntax: ALTER add table table 1 attribute name data type [integrity constraints] [ first | After attribute name 2]; integrity constraint: is an optional parameter used to set the new integrity constraint field first: is an optional parameter, its role is to set the new field to a table of word after: is an optional parameter, whose role is to add new fields to the "attribute name 2" field within the meaning of SQL statements executed if There is no "first" or "after the property name 2" parameter specifies the location of the new fields, the new field defaults to the last column of the table ( 1 ) increasing the field without integrity constraints Example: alter Phone VARCHAR the Add User table ( 20 is ); ( 2 ) increasing an integrity constraint fields example: alter table user the Add Age int ( . 4) Not null; ( . 3 first position) to increase the field table example: alter table user NUM int the Add ( . 8 ) First Primary Key; ( . 4 to increase the field after a specified position) table example: ALTER address the Add User table VARCHAR ( 20 is) Not null after Phone; . 5 , delete field delete field is deleted in the table has been defined table in a field, its data will be deleted after the field belongs to is deleted syntax: the ALTER tABLE name drop the table property name; 6, Arrangement position modified field syntax format: alter table modify table data type attribute name 1 First | After attribute name 2; ( 1 ) changes to the first field position for example: alter table modify User name VARCHAR ( 20 is ) First; ( 2 ) field modifications to the specified location for example: alter table modify User Sex tinyint ( . 1 ) After Age; . 7 , the change table storage engine syntax: ALTER table table engine = storage engine name; 8 , remove the foreign key constraint table **** ************************************************** **************** 9 , delete a table ( 1 ) is not associated with normal delete table syntax: drop table name; ( 2) Deleting the parent table is associated with the other tables foreign key constraint delete sub-table, and then delete the parent table ***************************** ************************************************

Guess you like

Origin www.cnblogs.com/xiaobaibailongma/p/12088692.html