The SQL language is divided into three parts: data definition Language, DDL, Data Manipulation Language, DML, and data Control language Language, referred to as DCL).
Today the main introduction to DDL statements, DDL statements: CREATE ALTER DROP, mainly the operation of the table.
1. Establish database operation
Syntax: CREATE database name;
Description: Creates a database with the specified name. This statement fails if the database that you are creating already exists or does not have the appropriate permissions to create it.
Example: Create a besttest library.
mysql> CREATE DATABASE besttest Charset=utf8;
2. Display database structure
SHOW CREATE DATABASE besttest;
You can view information about the database (such as MySQL version ID number, default character set, CREATE DATABASE SQL statement, and so on).
3. Delete a database
Syntax: DROP database name;
Description: Deleting a database means deleting a database that already exists in the database system. After the database is deleted, the original allocated space is reclaimed. It is important to note that deleting a database deletes all tables and all data and indexes in that database. Therefore, special care should be taken.
Example: Delete the Besttest library.
mysql> DROP DATABASE besttest;
4. Create a table
Syntax: CREATE table table name (
Column name 1 column type [column integrity constraint],
Column Name 2 column type [column integrity constraint],
......);
Example: Establish a table school, which consists of two columns, the first column property is non-null, and do the primary key, and self-increment
CREATE TABLE School (
school_id Int (ten) NOT NULL Auto_increment primary key,
School_name varchar (default) NULL,
Phone int (Ten) unique
);
5. Primary key
Definition: Uniquely identifies a record, cannot be duplicated, is not allowed to be empty, and the primary key can only have one.
Function: Used to ensure data integrity.
6. Common Uniqueness constraints
PRIMARY key main code constraint (primary key)
Unique uniqueness Constraint
Not NULL non-null value constraint
Auto_increment for integer series default self-increment 1
UNSIGNED unsigned integer
Defaults default_value default value constraint
Default Cur_timestamp Save current time when creating new record (timestamp data column only)
On UPDATE Cur_timestamp Save the current time by default when modifying records (timestamp data columns only)
CHARACTER Set name Specifies the character set (string only)
7. View Table Structure
DESC table name;
Show create table table name;
8, modify the table
Modifying a table refers to modifying the definition of a table that already exists in the database. Modifying a table is simpler than redefining the table, does not need to reload the data, and does not affect ongoing
The service. Modify the table in MySQL by using the ALTER TABLE statement. Modification tables include modifying table names, modifying field data types, modifying field names, adding fields, deleting
In addition to fields, modify the placement of fields, change the default storage engine, and delete foreign key constraints for tables.
(1) Modify table name
Syntax: ALTER table Old table name RENAME [to] new table name;
Example: ALTER TABLE School Rename School2;
(2) Modifying the data type of a field
Syntax 1:alter table name MODIFY property name data type;
Example: Alter Table School Modify school _name Char (20);
Syntax 2:alter table name change old property name new data type of new property name;
Example: Alter Table School Change school_name name Char (20);
The difference between modify and change is that modify does not need to specify a new field name when modifying the table structure, directly following the property that needs to be modified.
Change in the table structure, you need to specify the old field name and new field name and new property.
(3) Add field
Syntax: ALTER table name ADD property name 1 data type [integrity constraint] [first | After property name 2];
First | After: Indicates that the field is incremented before or after the specified location. By default, the field is added last.
Example: Alter Table School Add addr varchar (a) not NULL first;
(4) Modify the position of the field
Syntax: ALTER table name MODIFY property name 1 data type First | After property name 2;
Where the "property name 1" parameter refers to the name of the field where the location needs to be modified; The data type parameter refers to the data type of property name 1; the "first" parameter refers to the position
Set to the first position of the table; the After property name 2 parameter specifies that property name 1 is inserted after property name 2.
(5) Delete a field
Syntax: ALTER table name DROP field name;
Example: ALTER TABLE school DROP Addr;
(6) Clear the table
Syntax: Truncate table name;
Example: Truncate students;
Emptying a table is to empty all the existing data in the table, use the TRUNCATE keyword, and after truncate, the ID of the self-growing column starts when the table is built
The ID starts, and the delete is the difference between the delete and the data is emptied, but the ID of the self-growing column does not start with the start ID, Truncat cannot be rolled back, and the delete
, you can roll back.
(7) Delete a table
Syntax: DROP table name;
Example: Drop table School;
Deleting a table means deleting a table that already exists in the database. When you delete a table, all data in the table is deleted. Therefore, you should pay special attention when you delete a table. Mysql
The table is dropped by the drop TABLE statement.
The DDL of the SQL language