Chapter 3 Relational Database Standard Language SQL

Summarize SQL operations: operations
on tables or views need to write table or view, such as create table table_name (...)
operations on data in tables or views, such as insert into table_name values(...)

3.1_SQL Features

1. Comprehensive and unified
integration of data definition language (DDL), data manipulation language (DML), and data control language (DCL) functions in one.
2. Highly non-procedural
3. Set-oriented operation mode
4. Provide multiple usage modes with the same grammatical structure
5. Concise language, easy to learn and use

3.3_ data definition

3.3.1_ Definition, deletion and modification of basic tables

1. Define the basic table
CREATE TABLE <table name>
(<column name> <data type>[ <column-level integrity constraints>]
[, <column name> <data type>[ <column-level integrity constraints>] ]…
[, <table-level integrity constraints>])
2. Modify the basic table
Insert picture description here
3. Delete the basic table
DROP TABLE <table name> [RESTRICT| CASCADE]

3.3.3_ Index creation and deletion

1. Create index
CREATE [UNIQUE] [CLUSTER] INDEX <index name> ON <table name>(<column name>[<order>][,<column name>[<order>] ]...)
2. Delete index
DROP INDEX <index name>

3.4_ data query

The difference between the HAVING phrase and the WHERE clause : The target is different. The
WHERE clause acts on the base table or view, and selects the tuples that meet the conditions. The
HAVING phrase acts on the group, and selects the group that meets the conditions .

Detailed explanation of having and where usage

3.4.1_ Eliminate duplicate rows when querying

例:SELECT DISTINCT Sno FROM SC

3.4.2_ Query involving null values

select * from Student where score IS null
Note that IS cannot be'='

3.4.3 The use of_BETWEEN AND

select * from Student where age BETWEEN 20 AND 30
注意:是20<= age <=30

3.4.4_ Character matching

Insert picture description here
Insert picture description here

3.4.5_ Aggregate function

Insert picture description here

3.5_ data update

3.5.1_ Insert data

Method 1 :
INSERT
INTO <table name> [(<attribute column 1> [, <attribute column 2>... )]
[subquery]


INSERT
INTO <table name> [(<attribute column 1> [,<attribute column 2>… )]
values((<attribute column 1> [,<attribute column 2>… ))

Example:

 INSERT INTO  Dept_age(Sdept,Avg_age)
              SELECT  Sdept,AVG(Sage)
              FROM  Student
              GROUP BY Sdept

或
 INSERT INTO  Dept_age(Sdept,Avg_age)
 values(1110,20)

3.5.2_ Modify data

UPDATE <table name>
SET <column name>=<expression>[,<column name>=<expression>]...
[WHERE <condition>]

3.5.3_ Delete data

DELETE
FROM <table name>
[WHERE <condition>]

3.6_ View (except for creation, the other is the same as the operation on the table)

3.6.1_Create a view

CREATE VIEW
<view name> [(<column name> [,<column name>]...)]
AS <subquery>
[WITH CHECK OPTION]
WITH CHECK OPTION means to ensure update, insert and delete operations on the view The inserted or deleted row satisfies the predicate condition in the view definition

--建立信息系学生的视图,并要求进行修改和插入操作时仍需保证该视图只有信息系的学生 。
        CREATE VIEW IS_Student
        AS 
        SELECT Sno,Sname,Sage
        FROM  Student
        WHERE  Sdept= 'IS'
        WITH CHECK OPTION

3.6.2_ Delete view

DROP VIEW <view name>

Guess you like

Origin blog.csdn.net/qq_43907296/article/details/110790844