[MySQL study notes] DDL and DML language

The syntax of SQL can be roughly divided into four categories, namely DDL, DML, DQL and DCL statements.

DDL: The full name is Data Definition Languages, that is, data definition language, which is used to define database objects (databases, tables, fields);

DML: The full name is Data Manipulation Languages, which is a data manipulation language, which is used to add, delete, and modify data in database tables;

DQL: The full name is Data Query Language, that is, data query language, which is used to query the records in the table in the database.

DCL: The full name is Data Control Language, that is, data control language, which is used to create database users and control database access rights.

This note mainly includes the use of DDL and DML languages.

Before introducing the DDL and DML languages, let's take a look at the data types of MySQL.

Data types in MySQL can be divided into three categories: numeric types, string types, and datetime types.

First of all, the first type, numerical type: it is roughly divided into integer type and floating point type.

The shaping includes TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT, and the byte size increases from 1 to 5 in turn;

Floating-point types include FLOAT and DOUBLE, and the byte sizes are 4 and 8 respectively;

The second is the string type, mainly using the char type and varchar type, the former is a fixed-length string, and the latter is a variable-length string. When varchar is defined, its maximum length can be specified. If the actual data is less than its maximum length, then varchar will automatically adjust its length to be consistent with the data size.

After using char and varchar, you need to specify its size later, such as char(10).

The string type also has BLOB and TXT types, the BLOB type is used to store binary data, and the TXT type is used to store text strings.

The last is the datetime type, which is essentially a string.

 There are mainly 5 types of date and time.

1. DATE, 3 bytes, the format is YYYY-MM-DD, that is, year-month-day.

2. TIME, 3 bytes, the format is HH:MM:SS, instant: minute: second.

3. YEAR, 1 byte, the format is YYYY, indicating the year, and the range is 1901 to 2155.

4. DATETIME, 8 bytes, the format is YYYY-MM-DD HH:MM:SS, which is a combination of DATE and TIME types.

5. TIMESTAMP, 4 bytes, the format is also YYYY-MM-DD HH:MM:SS, but the range is small.

The next class officially begins to introduce the DDL statement.

DDL statements can be divided into two types according to the operation object, the first type is the operation on the database, and the second type is the operation on the table.

Firstly, it introduces the operations of DDL statements on the database, which are mainly divided into four types: query, create, delete and use.

# 查询
-- 查询所有数据库
show databases;
-- 查询当前数据库
select database();

# 创建
create database [数据库名];

# 删除
drop database [数据库名];

# 使用
use [数据库名];

Query all databases:

All databases of the current computer are displayed.

Next, we select the system database sys and use use [database name] to select.

The appearance of Database changed means that we have selected/switched the database.

The name of the current database can be queried through the function method select database().

 Then we try to create a new database newdatabase

You can see that newdatabase has been created.

At this time, if you create a database with the same name as newdatabase, an error will be reported.

The reason is that newdatabase already exists, and the name of the database cannot be repeated.

In order to avoid this from happening, you can add a restriction statement if not exists when creating the database

No error was reported.

Then delete the newly created database and use the drop statement. 

 newdatabase has been deleted.

Similarly, if you use the delete statement again at this time, MySQL will report an error, and you need to add a restriction if exists

Next is the table operation statement of DDL, including creation, query, modification and deletion

#创建
create table [表名](
    字段1 类型 [comment 注释],
    字段2 类型 [comment 注释],
    字段3 类型 [comment 注释],
    ........
    字段n 类型 [comment 注释]
)[comment 表注释];


# 查询
-- 查询当前数据库的所有表
show tables;

-- 查询表结构
desc [表名];

-- 查询指定表的建表语句
show create table [表名]

#修改
-- 添加字段
alter table [表名] add [字段名] [类型(长度)];

-- 修改数据类型
alter table [表名] modify [字段名] [新数据类型(长度)];

-- 修改字段名和字段类型
alter table [表名] change [旧字段名] [新字段名] [类型(长度)];

-- 删除字段
alter table [表名] drop [字段名];

-- 修改表名
alter table [表名] rename to [新表名];

#删除
-- 删除表
drop table [表名];

-- 重新创建表
truncate table [表名];


First use the create table statement to create a table user

 

 At this point, there is a table in the newbase database, which can be queried with show tables.

 Use the desc statement to query the structure of the table.

 Finally, use show create table to view the table creation process.

Add a field info

 Modify the data type of info

 Modify the field name of info

 modify table name

delete table

 

The last is the DML language, which is used to add, delete, and modify data records in tables in the database. The main keywords are insert, update, and delete.

# 添加数据
-- 给指定字段添加数据
insert into [表名] (字段1,字段2,...) values (值1,值2,...) [,(值1,值2,...)];

-- 给全部字段添加数据
insert into [表名] values (值1,值2,...)[,(值1,值2,...)];

# 修改数据
update [表名] set [字段名1] = [值1],[字段名2] = [值2],... [where 条件];

# 删除数据
delete from [表名] [where 条件]; 

adding data

 change the data

 delete data

 

 

Guess you like

Origin blog.csdn.net/qq_65021355/article/details/130656091