SQL
1. SQL overview
A database language that integrates DDL, DML, and DCL
1. Leading words in DDL sentences
- Create
- Alter (modified)
- Drop
Mode definition and deletion, including the definition of Database, Table, View, Index, integrity constraints, etc., as well as the definition of objects (RowType row objects, Type column objects)
2. DML sentence guide words
- Insert
- Delete
- Update
- Select
- Various methods of update and retrieval operations, such as direct input of records, input from other Tables (created by SubQuery)
- Retrieval of various complex conditions, such as connection search, fuzzy search, group search, nested search, etc.
- Various aggregation operations, such as average, sum, etc., group aggregation, group filtering, etc.
3. Leading words in DCL sentences
- Grant
- Revoke (Revocation of authorization)
Security control: authorization and revocation of authorization
2. SQL statement
Include two things:
- Define the database and table (using DDL)
- Append tuples to the table (using DML)
1. DDL(Data Definition Language)
DDL is usually used by DBA, and it is also used by application programmers after DBA authorization
DDL basic sentences
- Create a database:
Create Database
- Create the Table in the DB (define the relational schema):
Create Table
- Define the constraints of the Table and its various attributes (define integrity constraints)
- Define View (define external mode and EC image)
- Define Index, Tablespace... etc. (define physical storage parameters)
- Revocation and amendment of the above definitions
DDL example
Create DB
Create Database 数据库名;
Create Table
Create Table 表名(列名 数据类型[Primary key | Unique][Not null][,列名 数据类型[Not null],...]);
Primary key: Primary key constraint. Each table can only create one primary key constraint
Unique: unique constraint (ie, candidate key). There can be multiple uniqueness constraints
Not null: non-null constraints. Means that the column does not allow null values to appear
2. DML(Data Manipulation Language)
DML is usually used by users or application programmers to access authorized databases
DML basic sentences
- Append a new tuple to Table:
Insert
- Modify some attributes in some tuples of Table:
Update
- Delete some tuples in Table:
Delete
- Retrieve various conditions for the data in the Table:
Select
DML example
Add tuples to the table. If the column name is omitted, it must be in the same order as the defined or stored column name. If the column name is not omitted, it must be in the same order as listed in the statement.
Insert into 表名[(列名[,列名]...)]
Values(值[, 值],...);
3. Revision and cancellation of database definitions SQL statements
1. Revise the basic table
Alter Table 表名
[Add [[列名 数据类型],...]]
[Drop 完整性约束名]
[Modify [[列名 数据类型],...]];
Add
: Add a new columnDrop
: Remove integrity constraintsModify
: Modify column definition
Example: Delete the constraint that the student's name must be unique
Alter Table Student
Drop Unique(Sname);
2. Delete table
Drop Table 表名;
3. Delete the database
Drop Database 数据库名;
4. Specify/Close the current database
use|close 数据库名;
4. Basic query SQL statement
1. Simple query
Equivalent to ∏ column name,..., Column name (δ search condition (table name)) \prod_(column name,...,column name)(\delta_(search condition)(table name))∏Column name , . . . , The column name( dSubject search bar member( Table name ) ) , pay attention to operator precedence and() () forsearch conditions( ) Use
Select 列名[[, 列名]...]
From 表名
[Where 检索条件];
2. Use the query results Distincet
to duplicate records
Select Distinct 列名[[, 列名]...]
From 表名
[Where 检索条件];
3. query results using Order By
sorting
Sort according to the specified column name, asc
or omit it and write it in ascending order, desc
as descending
Select 列名[[, 列名]...]
From 表名
[Where 检索条件]
Order By 列名 [asc|desc];
4. Fuzzy query
Use Like
or Not Like
keyword
Select 列名[[, 列名]...]
From 表名
Where [Not] Like "匹配字符串";
Matching rules:
%
: Match 0 or more characters_
: Match any single character\
: Escape characters, treat some special characters as ordinary characters
5. Connect query
Equivalent to ∏ column name,..., Column name (δ search condition (table name 1 × table name 2 ×...)) \Prod_(column name,...,column name)(\delta_{search condition} (Table name 1×table name 2×...))∏Column name , . . . , The column name( dSubject search bar member( Table name 1×Table name 2×...))
Select 列名[[, 列名]...]
From 表名1, 表名2,...
Where 检索条件;
When a multi-table join two tables same attribute name, you need to use 表名.属性名
way to define which attribute table belongs to the
6. as
Duplicate name processing
Select 列名 as 列别名 [[, 列名 as 列别名]...]
From 表名1 as 表别名1, 表名2 as 表别名2,...
Where 检索条件;
as
Can be omitted
7. and Insert
, Delete
and Update
nested sub queries binding
Batch data insertion operation
Insert into 表名[(列名[, 列名]...)]
子查询;
Delete certain query data
Delete From 表名
Where 属性名 in
(子查询);
Update the specified data
Update 表名
Set 列名 = 表达式|(子查询)
[[, 列名 = 表达式|(子查询)]...]
[Where 条件表达式];