The basic operation of the 02 MySQL data tables

01- Create a data table

# Switching database 
use TEST_DB; 

# create a data table 
syntax rules are as follows: 
Create Table table name ( 
    field name 1, data type [Column-level constraints] [Default], 
    field name 2, data type [Column-level constraints] [Default value], 
    ...... 
    [table-level constraints] 
); 

Note:
     1 , table names to be created, not case sensitive, can not use SQL keywords.
    2 , the data table to create multiple columns, separated by commas. 
Example: 
the Create the Table TEST_DB ( 
    the above mentioned id INT ( 11 ), 
    name VARCHAR ( 25 ), 
    deptID INT ( 11 ), 
    salary FLOAT 
); 

# to view the data table is to create a successful 
show tables;

1.1 The primary key constraint

Primary key constraint requires unique primary key column of data, and does not allow empty. Primary keys divided into two types: primary key and multi-segment list primary key field.

# Single-field primary key 
1 , syntax rules are as follows: 
Field Name Data Type PRIMARY KEY [Default] 

create Table TEST_DB ( 
    ID the INT ( . 11 ) a PRIMARY KEY, 
    name VARCHAR ( 25 ), 
    deptID the INT ( . 11 ), 
    the salary FLOAT 
); 

or 
create TEST_DB Table ( 
    ID the INT ( . 11 ), 
    name VARCHAR ( 25 ), 
    deptID the INT ( . 11 ), 
    the salary FLOAT, 
    a PRIMARY KEY (ID) 
);
# Multiple-field primary key
 
1 , syntax rules are as follows: 
a PRIMARY KEY (field 1, field 2, field 3, ....) 
Example: 
Create Table TEST_DB ( 
    name VARCHAR ( 25 ), 
    deptId the INT ( . 11 ), 
    the salary FLOAT, 
    a PRIMARY KEY (name, deptId) 
);

1.2 foreign key constraints

Foreign key is used between two data tables to link, or may be a plurality of rows. A table can have multiple foreign keys, foreign key is null, if not a null value, each foreign key value must be equal to a primary key of another table.

Foreign keys is to maintain data consistency and integrity.

Grammar rules foreign key as follows: 
constraint foreign key name foreign key field name 1 References primary table officiating key column a 

foreign key name: foreign key constraint defined name, a table can have the same foreign key name; 
field name: Current table fields need to add foreign key column; 
master table: table foreign key quilt depends name of the table; 
primary key columns: primary key column or primary key combination as defined in the primary table. 

Example: 
Create Table tb_dept1 ( 
    ID the INT ( . 11 ) a PRIMARY KEY, 
    name VARCHAR ( 22 is ) the NOT NULL, 
    LOCATION VARCHAR ( 50 ) 
); 

the CREATE TABLE tb_dept5 ( 
    ID the INT ( . 11 ) a PRIMARY KEY, 
    name VARCHAR ( 25 ), 
    deptId the INT ( . 11 ),  
    salary FLOAT,
    CONSTRAINT fk_emp_dept1 a FOREIGN KEY (deptId) the REFERENCES tb_dept1 (ID) 
);

1.3 Non-empty constraint

Non-empty constraint (NOT NULL) refers to the value of the field can not be blank.

The syntax rules are as follows: 
Field Name Data Type NOT NULL

1.4 The only constraint

The only constraint (Unique Constraint) This column requires a unique, allowed to be empty, but can occur only once null.

The syntax rules are as follows: 

Field Name Data Type UNIQUE 

Create Table tb_dept1 ( 
    ID the INT ( . 11 ) a PRIMARY KEY, 
    name VARCHAR ( 22 is ) UNIQUE, 
    LOCATION VARCHAR ( 50 ) 
); 

# or 
Create Table tb_dept1 ( 
    ID the INT ( . 11 ) a PRIMARY KEY, 
    name VARCHAR ( 22 is ), 
    LOCATION VARCHAR ( 50 ), 
    CONSTRAINT the SHT UNIQUE (name) 
);
UNIQUE and PRIMARY KEY difference is: 
a table can have multiple fields declared as UNIQUE, but only one PRIMARY KEY. 
PRIMARY KEY column declared as not null values, but declared as UNIQUE field allows a null value (NULL) is present.

1.5 default constraint

Specify a default value for a column. DEFAULT

create  table  tb_dept4 (
    id  INT(11)  PRIMARY KEY,
    deptId  INT(11)  DEFAULT 1234,
    name  VARCHAR(22) UNIQUE,
    location  VARCHAR(50)
);

Table 1.6 set attribute value is automatically increased

Want the system to automatically generate primary key field. AUTO_INCREMENT keyword

create  table  tb_dept4 (
    id  INT(11)  PRIMARY KEY  AUTO_INCREMENT,
    name  VARCHAR(22) NOT NULL,
    location  VARCHAR(50)
);

1.7 View data table structure

# View the data sheet of the basic structure of the statement DESCRIBE / DESC 
DESCRIBE table; 
or simply: 
DESC table name; 

MySQL > desc tb_dept1;
 + ---------- + ---------- + ------ + ----- + --- --------- + ------- + 
| Field, | Type | Null | Key | the Default | Extra | 
+ - ------------- + --------- + --------- + ------ + ----- + --- + ---- 
| ID | int (. 11) | NO | the PRI | NULL | | 
| name | VARCHAR (22 is) | NO | | NULL | | 
| LOCATION | VARCHAR (50) | YES | | NULL | | 
+ - ------------- + --------- + --------- + ------ + ----- + --- + ---- 
. 3 rows in SET (0.01 sec)
# Display the detailed structure of SHOW CREATE TABLE statement 
can be used to display the CREATE TABLE statement when you create a table. 

# Syntax is as follows: 
SHOW the CREATE TABLE table name \ G; 

Example: 
MySQL > SHOW the CREATE TABLE tb_dept1 \ G;
 ************************************************************ Row *************************** 1. * 
       the Table: tb_dept1 
the Create the Table: tb_dept1` the CREATE TABLE `( 
  ` id` int ( . 11 ) NULL the NOT, 
  `name` VARCHAR ( 22 is ) the NOT NULL, 
  ` location` VARCHAR ( 50 ) the DEFAULT NULL, 
  a PRIMARY KEY ( `id`) 
) ENGINE = the InnoDB the DEFAULT the CHARSET = UTF8
 . 1 Row in SET (0.00 sec) 

ERROR:
No query specified

02- modify data table

MySQL use the ALTER TABLE statement to modify a table.

Operations are used to modify the table: the table name modifications, changes to field names or field types of data, adding and deleting fields, the arrangement position of the field to modify, change table storage engine, remove the foreign key table constraints.

2.1 modify the table name

The syntax rules are as follows: 
the ALTER TABLE name RENAME TO old table new table name; 

Example: 
MySQL > Show the Tables;
 + ------------------- + 
| Tables_in_test_db | 
+ --- + ---------------- 
| tb_dept1 | 
+ ------------------- + 
1 Row in the SET (0.00 sec) 

MySQL > the ALTER TABLE RENAME tb_dept1 the TO tb_deptment1; 
Query the OK, 0 rows affected ( 0.03 sec) 

MySQL > Show the Tables;
 + ------------------- + 
| Tables_in_test_db | 
+ - + ----------------- 
| tb_deptment1 | 
+ ------------------- + 
1 Row in the SET (0.00 sec)

2.2 modify the data type of the field

The syntax rules are as follows: 
the ALTER TABLE table MODIFY Field Name Data Type 

Table Name: To change the name of the table where the field data types; 
field names: modify the data types of fields; 
data type: After modifying the new field's data type.
ALTER  TABLE  tb_dept1  MODIFY  name  VARCHAR(30);

2.3 modify field names

The syntax rules are as follows: 
the ALTER TABLE table name CHANGE old name for the new field name field new data types;

2.4 Add field

The syntax rules are as follows: 
the ALTER TABLE table name ADD a new name for the new field Data Type Constraints [FIRST | AFTER existing field name]; 

FIRST and AFTER are optional; 
FIRST: adding a new field to the first field in the table; 
AFTER: the newly added add fields to the specified ' existing field names ' back.
# Add field without integrity constraints 
ALTER TABLE tb_dept1 the ADD managerID the INT (10 ); 

# add integrity constraints field 
ALTER TABLE tb_dept1 the ADD Memo VARCHAR (50) Not null; 

# add a field of the first column of the table 
ALTER TABLE the ADD column1 the INT tb_dept1 (10 ) FIRST; 

# add a field after a specified list of 
ALTER tABLE tb_dept1 ADD column2 INT (10 ) ALTER name;

2.5 Delete field

The syntax rules are as follows: 
the ALTER TABLE table DROP field name;

2.6 modified arrangement position field

Syntax is as follows: 
the ALTER TABLE 1 Table Name Field Data Type MODIFY FIRST | an AFTER 2 field;

2.7 Changing table storage engine

Syntax: 
the ALTER TABLE table name ENGINE after changing the storage engine = name;

2.8 Delete table foreign key constraint

Syntax: 
the ALTER TABLE table name DROP FOREIGN KEY foreign key constraint name; 

foreign key constraint name: CONSTRAINT subsequent keywords parameter definition table means

03- delete data table

# 1, table deletion is not associated 
DROP TABLE [IF EXISTS] Tables 1, 2, ... n-tables; 
Example: the DROP TABLE tb_dept1 the IF EXISTS; 

# 2, to delete the other primary table associated tables 
foreign key data when the association, if directly delete the parent table, the result will show the failure. 
If you have to delete, you can delete the child table associated with it, and then delete the parent table. 
But in some cases, you may want to keep the child table, if you want to delete a single parent table, simply a foreign key table constraints cancel, then you can delete the parent table. 

Foreign key constraint of having a child table fk_emp_dept tb_empt, associated with the parent table id is the primary table tb_empt2. 

Example: 
the ALTER TABLE tb_emp the DROP a FOREIGN KEY fk_emp_empt; 
the DROP TABLE tb_empt2;

 

Guess you like

Origin www.cnblogs.com/pgxpython/p/11719822.html