Bài giảng Microsoft SQL server: Bài 9 - TS. Lê Thị Tú Kiên
lượt xem 3
download
"Bài giảng Microsoft SQL server - Bài 9: Chèn, sửa, xóa dữ liệu" đưa ra các thông số kỹ thuật cho một truy vấn hành động, viết mã câu lệnh INSERT, UPDATE hoặc DELETE để thực hiện hành động; tạo một bản sao của bảng bằng cách sử dụng mệnh đề INTO của câu lệnh SELECT.
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Bài giảng Microsoft SQL server: Bài 9 - TS. Lê Thị Tú Kiên
- Lecture 8 How to insert, update, and delete data Murach's SQL Server 2012, C7 Le Thi Tu Kien – FIT - HNUE Slide 1
- Objectives Applied Given the specifications for an action query, code the INSERT, UPDATE, or DELETE statement for doing the action. Create a copy of a table by using the INTO clause of the SELECT statement. Knowledge Describe the three types of action queries. Explain how to handle null values and default values when coding INSERT and UPDATE statements. Explain how the FROM clause is used in an UPDATE or DELETE statement. Murach's SQL Server 2012, C7 Le Thi Tu Kien – FIT - HNUE Slide 2
- The syntax of the SELECT INTO statement SELECT select_list INTO table_name FROM table_source [WHERE search_condition] [GROUP BY group_by_list] [HAVING search_condition] [ORDER BY order_by_list] Murach's SQL Server 2012, C7 Le Thi Tu Kien – FIT - HNUE Slide 3
- Create a complete copy of the Employee table SELECT * INTO EmployeeCopy FROM Employee; Create a partial copy of the Employee table SELECT * INTO RetireEmp FROM Employee WHERE YEAR(GETDATE())- YEAR(bdate)>60 Murach's SQL Server 2012, C7 Le Thi Tu Kien – FIT - HNUE Slide 4
- Create a table with summary rows SELECT DNo, SUM(Salary) AS SumOfSalaries INTO DepSumSalary FROM Employee GROUP BY DNo; Warnings When you use the SELECT INTO statement to create a table, only the column definitions and data are copied. Definitions of primary keys, foreign keys, indexes, default values, and so on are not included in the new table. Murach's SQL Server 2012, C7 Le Thi Tu Kien – FIT - HNUE Slide 5
- The syntax of the INSERT statement INSERT [INTO] table_name [(column_list)] [DEFAULT] VALUES (expression_1 [, expression_2]...) [, (expression_1 [, expression_2]...)...] Murach's SQL Server 2012, C7 © 2012, Mike Murach & Associates, Inc. Slide 6
- The values for a new row in the Employee table Column Value FName Thanh Minit T LName Nguyen SSN 223344555 BDate 1980-08-15 Address 460 Dallas, Houston,TX Sex F Salary 35000 SuperSSN 888665555 DNo 4 Murach's SQL Server 2012, C7 Le Thi Tu Kien – FIT - HNUE Slide 7
- Insert the row without using a column list INSERT INTO EmployeeCopy VALUES ('Thanh', 'T', 'Nguyen', '223344555', '1980-08-15','460 Dallas, Houston,TX', 'F', 35000, 888665555,4); Insert the row using a column list INSERT INTO EmployeeCopy (FName, Minit,LName,SSN,BDate,Address, Sex,Salary,SuperSSN,DNo) VALUES ('Thanh', 'T', 'Nguyen', '223344555', '1980-08-15','460 Dallas, Houston,TX', 'F', 35000, 888665555,4); The response from the system (1 row(s) affected) Murach's SQL Server 2012, C7 Le Thi Tu Kien – FIT - HNUE Slide 8
- Insert three rows INSERT INTO Employee VALUES (N'Jonh ', N'B', N'Smith', N'123456789', CAST(0x21F20A00 AS Date), N'731 Fondren, Houston, TX', N'M', 30000, N'333445555', 5), (N'Franklin ', N'T', N'Wong', N'333445555', CAST(0x83050B00 AS Date), N'638 Voss, Houston, TX', N'M', 40000, N'888665555', 5), (N'Joyce ', N'A', N'English', N'453453453', CAST(0xE8FC0A00 AS Date), N'5631 Rice, Houston, TX', N'M', 25000, N'333445555', 5); The response from the system (3 row(s) affected) Murach's SQL Server 2012, C7 Le Thi Tu Kien – FIT - HNUE Slide 9
- The definition of the ColorSample table Column Data Allow Default Name Type Length Identity Nulls Value ID Int 4 Yes No No ColorNumber Int 4 No No 0 ColorName VarChar 10 No Yes No Murach's SQL Server 2012, C7 Le Thi Tu Kien – FIT - HNUE Slide 10
- Six INSERT statements for the ColorSample table INSERT INTO ColorSample (ColorNumber) VALUES (606); INSERT INTO ColorSample (ColorName) VALUES ('Yellow'); INSERT INTO ColorSample VALUES (DEFAULT, 'Orange'); INSERT INTO ColorSample VALUES (808, NULL); INSERT INTO ColorSample VALUES (DEFAULT, NULL); INSERT INTO ColorSample DEFAULT VALUES; Murach's SQL Server 2012, C7 Le Thi Tu Kien – FIT - HNUE Slide 11
- The ColorSample table after the rows are inserted Murach's SQL Server 2012, C7 Le Thi Tu Kien – FIT - HNUE Slide 12
- The syntax of the INSERT statement for inserting rows selected from another table INSERT [INTO] table_name [(column_list)] SELECT column_list FROM table_source [WHERE search_condition] Insert retire employees into the RetireEmp table INSERT INTO RetireEmp SELECT * FROM Employee WHERE YEAR(GETDATE()) - YEAR(bdate)>60 Note: The table RetireEmp must aready exist in the database COMPANY. Murach's SQL Server 2012, C7 Le Thi Tu Kien – FIT - HNUE Slide 13
- The same INSERT statement with a column list INSERT INTO RetireEmp (FName, Minit,LName,SSN,BDate, Address,Sex,Salary,SuperSSN,DNo) SELECT FName, Minit,LName,SSN,BDate, Address,Sex,Salary,SuperSSN,DNo FROM Employee WHERE YEAR(GETDATE()) - YEAR(bdate)>60 Murach's SQL Server 2012, C7 Le Thi Tu Kien – FIT - HNUE Slide 14
- The syntax of the UPDATE statement UPDATE table_name SET column_name_1 = expression_1 [, column_name_2 = expression_2]... [FROM table_source [[AS] table_alias] [WHERE search_condition] Update two columns of a single row UPDATE EmployeeCopy SET Salary = 40000, DNo = 5 WHERE SSN = '223344555'; Murach's SQL Server 2012, C7 Le Thi Tu Kien – FIT - HNUE Slide 15
- Update one column of multiple rows UPDATE EmployeeCopy SET DNo=3 WHERE DNo = 4; Update a column using an arithmetic expression UPDATE EmployeeCopy SET Salary = Salary + 1000 WHERE DNo = 3; Warning If you omit the WHERE clause, all the rows in the table will be updated. Murach's SQL Server 2012, C7 Le Thi Tu Kien – FIT - HNUE Slide 16
- A subquery that returns the value assigned to a column UPDATE EmployeeCopy SET Salary = (SELECT AVG(Salary) FROM EmployeeCopy WHERE DNo = 5) WHERE SSN = '223344555'; Murach's SQL Server 2012, C7 Le Thi Tu Kien – FIT - HNUE Slide 17
- A subquery used in a search condition UPDATE EmployeeCopy SET Salary = Salary + 1000 WHERE DNo = (SELECT DNumber FROM Department WHERE DName = 'Research'); Murach's SQL Server 2012, C7 Le Thi Tu Kien – FIT - HNUE Slide 18
- A column in a joined table used in a WHERE clause UPDATE EmployeeCopy SET Salary = Salary + 1000 FROM EmployeeCopy JOIN Department ON DNo=DNumber WHERE DName = 'Research'; Murach's SQL Server 2012, C7 Le Thi Tu Kien – FIT - HNUE Slide 19
- The syntax of the DELETE statement DELETE [FROM] table_name [FROM table_source] [WHERE search_condition] Delete a single row from the EmployeeCopy table DELETE EmployeeCopy WHERE Ssn = '223344555'; (1 row(s) affected) Delete all the employees for a department DELETE EmployeeCopy WHERE Dno = 3; (3 row(s) affected) Murach's SQL Server 2012, C7 Le Thi Tu Kien – FIT - HNUE Slide 20
CÓ THỂ BẠN MUỐN DOWNLOAD
-
Bài giảng Microsoft SQL server: Chương 1 - Phạm Mạnh Cương
11 p | 81 | 10
-
Bài giảng Microsoft SQL server: Chương 2 - Phạm Mạnh Cương
15 p | 65 | 6
-
Bài giảng Microsoft SQL server: Chương 4 - Phạm Mạnh Cương
51 p | 56 | 6
-
Bài giảng Microsoft SQL server - Bài 12: Thủ tục lưu trú, hàm và trigger
85 p | 73 | 6
-
Bài giảng Microsoft SQL server: Bài 3 - TS. Lê Thị Tú Kiên
24 p | 40 | 5
-
Bài giảng Microsoft SQL server: Bài 7 - TS. Lê Thị Tú Kiên
22 p | 35 | 4
-
Bài giảng Microsoft SQL server: Bài 8 - TS. Lê Thị Tú Kiên
19 p | 28 | 4
-
Bài giảng Microsoft SQL server: Bài 10 - TS. Lê Thị Tú Kiên
27 p | 33 | 4
-
Bài giảng Microsoft SQL server - Bài 11: Các cấu trúc điều khiển
44 p | 53 | 4
-
Bài giảng Microsoft SQL server: Bài 6 - TS. Lê Thị Tú Kiên
16 p | 32 | 4
-
Bài giảng Microsoft SQL server: Bài 5 - TS. Lê Thị Tú Kiên
30 p | 26 | 4
-
Bài giảng Microsoft SQL server: Bài 4 - TS. Lê Thị Tú Kiên
22 p | 34 | 4
-
Bài giảng Microsoft SQL server: Bài 1 - TS. Lê Thị Tú Kiên
24 p | 51 | 4
-
Bài giảng Microsoft SQL server: Chương 5 - Phạm Mạnh Cương
15 p | 67 | 4
-
Bài giảng Microsoft SQL server: Bài 12 - TS. Lê Thị Tú Kiên
66 p | 51 | 4
-
Bài giảng Microsoft SQL server: Bài 2 - TS. Lê Thị Tú Kiên
78 p | 39 | 3
-
Bài giảng Microsoft SQL server: Bài 11 - TS. Lê Thị Tú Kiên
35 p | 27 | 3
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn