Use of SQL syntax and DDL statements


Preface

This article mainly introduces the general syntax of SQL and the use of DDL statements. Readers should understand the storage structure of the database before reading this article.
lab environment:Windows11 operating system, Mysql database


1. SQL general syntax

关于sql语句的语法主要有以下几个方面:
(1) SQL语句可以单行或多行书写,以分号结尾
(2) SQL语句可以使用空格/缩进来增强语句的可读性
(3) MySQL数据库的SQL语句不区分大小写,关键字建议使用大写
(4) 注释
    单行注释:-- 注释内容 或 # 注释内容
    多行注释:/*  注释内容 */

2. DDL statement

1. Introduction to DDL functions

SQL statements can be mainly divided into the following four categories according to their functions: DDL, DML, DQL, and DCL .
The function of the DDL statement introduced in this article is: data definition language, used to define database objects (database, tables, fields )

2. DDL statements operate on the database

(1) Query all databases

show databases;

When we enter this statement, the database we currently have will be displayed: mysql, information_schema, etc.
Insert image description here

(2) Query the current database

select database();

Because no database is currently used, it is empty:
Insert image description here

(3) Create database

create database [if not exists ] 数据库名 
[default charset 字符集] [collate 排序规则];
# 方括号中的都是可选内容,即使不书写也会采用默认方案

For example, if we create a database named test and use the database's default character set and sorting rules, we
Insert image description here
can see that the test database in the current database server has been successfully created.

And because in the same database server, two databases with the same name cannot be created at the same time, otherwise an error will be reported ( if we create a database named test again, an error will be reported )
Insert image description here
. In order to avoid such errors, we can use the if not exists parameter to To solve this problem, do not create the database if it exists, but create it if it does not exist.
As shown in the figure below, when we create the test database again, no error will be reported:
Insert image description here

The statement used when we create a database named test1 and specify the character set as utf8mb4:
Insert image description here

(4) Delete database

drop database [if exists] 数据库名;

As shown below, we delete the created test1 database

(5) Switch database

use 数据库名;

When we want to operate a table under a database, we need to use this command to switch to the corresponding database, otherwise the operation will not be possible.
As shown in the figure below, we switch to the test database:
Insert image description here

(6) Query all tables in the current database

show tables;

Note: When using this statement, you must first use the use statement to switch to a database.

For example, if we check the table data in the test database, since there is no table under the current test database, it is displayed as empty:
Insert image description here

(7) Create table structure

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

We create a table structure as shown in the figure below, and name the table characters: You
Insert image description here
Insert image description here
can see that we successfully created a table structure named characters in the database named test.

(8) View the specified table structure

desc 表名;

Through this command, we can check the fields of the specified table, the type of the field, whether it can be NULL, whether there is a default value and other information.
Insert image description here

(9) Query the table creation statement of the specified table

show create table 表名;

This command is mainly used to view the table creation statement. Some parameters will be queried even if we did not specify them when creating the table, because these are the default values ​​​​of the database, such as: storage engine, character set wait.
Insert image description here

(10) Add fields to the table

alter table 表名 add 字段名 类型 [comment 注释] [约束];

Add a field named tel to the characters table, with type varchar(11) as shown in the following figure:
Insert image description here

(11) Modify the specified data type in the table

alter table 表名 modify 字段名 新数据类型;

Modify the type of tel field to varchar(12), as shown in the following figure:
Insert image description here

(12) Modify field names and field types in the table

alter table 表名 change 旧字段名 新字段名 类型 [comment 注释] [约束];

For example, change the field tel to newname and the type to varchar(30), as shown in the following figure:
Insert image description here

(13) Delete fields in the table

alter table 表名 drop 字段名;

Delete the field newname field, as shown in the following figure:
Insert image description here

(14) Modify table name

alter table 表名 rename to 新表名;

Change the characters table name to test as shown below:
Insert image description here

(15) Delete table

drop table [if exists] 表名;

We delete the test table, as shown in the figure below:
Insert image description here

(16) Delete the specified table and re-create the table

truncate table 表名;

The function of this statement is to delete the specified table and re-create a table with the same table name and header as the original table, except that the data stored in the table is cleared, which functions as a refresh table.

Summarize

This article mainly introduces the use of DDL statements in SQL statements. I hope it will be helpful to you.

Guess you like

Origin blog.csdn.net/weixin_63614711/article/details/132447820