(My)SQL DDL statement

SQL statements can be divided into the following three categories:
1: DDL statements: data definition language. Common keywords mainly include create, drop, alter, etc.
2: DML statement: data manipulation language for adding, deleting, updating, and searching. It only operates on the internal data of the table, and does not involve modification of the table definition and structure. Common keywords mainly include insert, delete, uptdate and select.
3: DCL statements: data control statements, statements used to control the direct permissions and access levels of different data segments. The main keywords include grant and revoke.

Insert picture description here

The above is the interface to start the mysql service.
In the above command line, mysql represents the client command, "-u" is followed by the connected database user, and "-p" means that a password is required.
If the database settings are normal, and the correct password is entered. You will see the above section of the welcome screen and a "mysql>" prompt.
The following parts are explained in the welcome interface:
the end of the command ends with ";" or "\g".
The connection id of the client records the number of times the database is currently connected.
The version of the mysql server, in this example, is 8.0.18.
Use the "help;" or "\h" command to display the help content, and use the "\c" to clear the command line buffer.
Enter the SQL statement to be executed after the mysql> prompt, and each SQL statement starts with " ;"Or"\g" to end, press Enter to execute.

2.2.1 DDL statement:
1: Create a database:

mysql> create database test1;

2: View the database existing in the system:

mysql> show databases;

Insert picture description here
information_schema: mainly stores some database object information in the system.
mysql: Stores information about system user permissions.

3: Select the database to be operated:

mysql> use test1

4: After selecting the operating database, view all the data tables created in test1:

mysql> show tables;

5: Delete the database:

mysql> drop database test1;

After the database is deleted, all the table data below will be deleted, so you must carefully check and backup before deleting.

6: Create table:
mysql table name exists on the disk in the form of a directory.
The basic syntax for creating a table in the database is as follows:
CREATE TABLE table(column_name_1 column_type_1 constraints,...)
column_name: the name of the column.
column_type: the data type of the column.
constraints: The constraints of the column.

For example, create a table named emp. The table includes ename (name), hireata (employment date) and sal (salary) 3 fields, the field types are varchar (10), data, int (2).

mysql> create table emp(ename varchar(10),hiredata data,sal decimal(10,2),deptno int(2));

7: View table:

mysql> desc smp;

Insert picture description here

In order to get more comprehensive table information, sometimes you need to view the SQL statement that created the table:

mysql> show create table func \G;

Insert picture description here

As you can see, through the above statement, in addition to the definition of the table, you can also see the engine (storage engine) and charset (charset) of the table. The meaning of the "\G" option is to enable the records to be arranged vertically according to the field in order to better display the longer records.

8: Delete table:

mysql> drop table emp;

9: Modify the table:
For the created table, the paint is a table that already has a lot of data. If you need to make some structural changes, you can delete the table first, and then rebuild the table according to the new definition. But this has certain drawbacks. If a service is accessing the table, it will also affect the service.
Therefore, in most cases, the alter table statement is used to change the table structure. The following are some commonly used commands:

1) Modify the table type, the syntax is as follows:
for example, modify the ename field definition of the table emp, change varchar(10) to varchar(20).

mysql> alter table emp modify ename varchar(20);

2) Add table fields, the syntax is as follows:
add field age in the center of table emp, the type is int(3):

mysql> alter table emp add age int(3);

3) Delete table field:
delete the field age:

mysql> alter table emp drop column age;

4) Rename the field:
rename age to age1, and modify the field type to int(4):

mysql> alter table emp change age age1 int(4);

Note: Both change and modify can modify the definition of the table. The difference is that it is inconvenient to write column names twice after change. But the advantage of change is that you can modify the column name, but modify cannot.

5: Modify the order
of the fields : In the field addition and modification syntax introduced earlier, there is an option first|after column_name, this option can be used to modify the position of the field in the table, the new field added by ADD is added to the table by default The last position of the field, and CHNANGE/MODIFY does not change the position of the field by default.

For example, add the new field birth data after ename:

mysql> alter table emp add birth data after ename;

Modify the field age and put it at the top:

mysql> alter table emp modify age int(3) first;

Note: CHANGE/FIRST|AFTER COLUMN These keywords are all extensions of MySQL on standard SQL, and may not be applicable to other databases.

6: Change the table name:
rename emp to emp1:

mysql> alter table emp rename emp1;

Guess you like

Origin blog.csdn.net/weixin_43916777/article/details/104357956
DDL