当前位置:网站首页>SQL Server 2016 learning records - data update
SQL Server 2016 learning records - data update
2022-07-28 10:24:00 【Hard work Zhang Zhang】
Learning goals :
1、 insert data
2、 Modifying data
3、 Delete data
4、 Processing of null value
Learning content :
1、 insert data
There are two ways to insert data :
- Insert tuples
- Insert subquery results
– Multiple tuples can be inserted at a time
Sentence format :
INSERT
INTO < Table name > [(< Property column 1>[,< Property column 2 >…)]
VALUES (< Constant 1> [,< Constant 2>]… );
function : Inserts a new meta group into the specified table
- INTO Clause
Specify the table name and attribute column to insert data
The order of attribute columns can be inconsistent with the order in the table definition
No attribute column specified : Indicates that a complete tuple is to be inserted , And the attributes of the attribute column are consistent with the order in the table definition
Specify some attribute columns : The inserted tuple takes null values on the remaining attribute columns- VALUES Clause
The value provided must be the same as INTO Clause match
The number of values
The type of value
--[ example 3.69] Put a new student tuple ( Student number :201215128; full name : Chen Dong ; Gender : male ; Department :IS; Age :18 year ) Insert into Student In the table .
INSERT
INTO Student (Sno,Sname,Ssex,Sdept,Sage)
VALUES ('201215128',' Chen Dong ',' male ','IS',18);
--[ example 3.70] Insert the information of student Zhang Chengmin into Student In the table .
INSERT
INTO Student
VALUES ('201215126',' Zhang Chengmin ',' male ',18,'CS');
--[ example 3.71] Insert a course selection record ( '200215128','1 ').
INSERT
INTO SC(Sno,Cno)
VALUES ('201215128 ',' 1 ');
/* The relational database management system will insert the record in the new Grade The column is automatically null */
Insert subquery results
Sentence format
INSERT
INTO < Table name > [(< Property column 1> [,< Property column 2>… )]
Subquery ;
INTO Clause
- Subquery
SELECT The clause target column must be the same as INTO Clause match
The number of values
The type of value
--[ example 3.72] For every department , Ask for the average age of the students , The results are stored in the database
-- Build table
CREATE TABLE Dept_age
( Sdept CHAR(15) /* Department name */
Avg_age SMALLINT); /* The average age of students */
-- insert data
INSERT
INTO Dept_age(Sdept,Avg_age)
SELECT Sdept,AVG(Sage)
FROM Student
GROUP BY Sdept;
2、 Modifying data
Sentence format
UPDATE < Table name >
SET < Name >=< expression >[,< Name >=< expression >]…
[WHERE < Conditions >];
function
- Modify the specified table to meet WHERE Tuples of clause conditions
SET Clause gives < expression > The value of is used to replace the corresponding attribute column
If omitted WHERE Clause , Indicates that you want to modify all tuples in the table
Three modification methods :
- Modify the value of a tuple
--[ example 3.73] Put students 201215121 Change your age to 22 year
UPDATE Student
SET Sage=22
WHERE Sno=' 201215121 ';
Modify the value of multiple tuples
--[ example 3.74] Increase the age of all students 1 year .
UPDATE Student
SET Sage= Sage+1;
Modification statement with query
--[ example 3.75] Set the scores of all students in computer science department to zero .
UPDATE SC
SET Grade=0
WHERE Sno IN
(SELECT Sno
FROM Student
WHERE Sdept= 'CS' );
3、 Delete data
Sentence format
DELETE
FROM < Table name >
[WHERE < Conditions >];
function : Delete the specified table WHERE Tuples of clause conditions
WHERE Clause
- Specifies the tuple to delete
The default is to delete all tuples in the table , The definition of the table is still in the dictionary
Three ways to delete :
- Delete the value of a tuple
--[ example 3.76] Delete student ID as 201215128 Student records of .
DELETE
FROM Student
WHERE Sno= '201215128';
Delete the value of multiple tuples
--[ example 3.77] Delete all student course selection records .
DELETE FROM SC;
Delete statement with query
--[ example 3.78] Delete the course selection records of all students in computer science department .
DELETE FROM SC
WHERE Sno IN
(SELETE Sno
FROM Student
WHERE Sdept= 'CS') ;
4、 Processing of null value
(1) Null value NULL: Generally, it means unknown information or meaningless information , This is common in relational tables
(2) stay SQL In the sentence , To judge whether the value of an attribute is null , Conditional expressions should not be used xxx=NULL, It's about using IS NULL or IS NOT NULL Look at
(3) When defining attributes , There are such regulations :
Yes NOT NULL Constraints cannot be null
Yes UNIQUE Constraints cannot be null
The attributes of the code cannot be null
(4) Algorithm :
The result of arithmetic operation between null value and any value is null ;
The result of the comparison operation between null value and any value is UNKNOWN;
If there is UNKNOWN, Then follow the following operation rules :
边栏推荐
猜你喜欢

【微信小程序】项目实战—抽签应用

初识SuperMap iDesktop

Voice chat app - how to standardize the development process?

Digital transformation scheme of real estate: all-round digital intelligence system operation, helping real estate enterprises improve the effectiveness of management and control

Aqua Data Studio 18.5.0导出insert语句

SQL Server 2016 学习记录 --- 嵌套查询

B2B e-commerce website scheme for building materials industry: enable the transformation and upgrading of building materials enterprises to achieve cost reduction and efficiency improvement

第一篇:UniAPP的小程序跨端开发-----创建uniapp项目

PHP生成二维码(学习)

剑指offer
随机推荐
阿里云镜像地址
Kubernetes
9、删除链表中节点
DBeaver的操作日志
Detailed explanation of thread synchronization volatile and synchronized
传全球半导体设备巨头或将于上海建合资工厂!
️雄关漫道真如铁,而今迈步从头越️
API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
9. Delete nodes in the linked list
剑指offer
SQL Server 2016 学习记录 --- 嵌套查询
uni-app进阶之创建组件/原生渲染
Consul
胡润发布2020中国芯片设计10强民营企业:华为海思竟然没有上榜!
MySQL的SQL TRACE一例
PHP生成二维码(学习)
JVM principle
死锁算法:银行家算法和安全性算法
14、双指针——盛最多水的容器
2021-10-13arx