Download presentation
Presentation is loading. Please wait.
Published byArabella Hart Modified over 9 years ago
1
DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi
2
Objectives After completing this lesson, you should be able to do the following: Insert, Updating and deleting rows in a table Query with select. 2 Ins.Ebtesam AL-Etowi
3
Data Manipulation Language Ins.Ebtesam AL-Etowi 3 A DML statement is executed when you: Add new rows to a table Modify existing rows in a table Remove existing rows from a table A transaction consists of a collection of DML statements that form a logical unit of work.
4
Insert Rows into Table Ins.Ebtesam AL-Etowi 4 Add a new rows to a table by using the INSERT statement. INSERT INTO table[(column [,column….])] VALUES (value [, value…..]) This statement with the VALUES clause adds only one row at a time to a table Insert a new row containing values for each column. List values in the default order of the columns in the table Optionally list the columns in the INSERT clause
5
Ins.Ebtesam AL-Etowi 5 Insert Rows into Table INSERT INTO DEPARTEMENT (Dname, Dnumber,Mgr_ssn, Mgr_start_date) VALUES('Research',5,'333445555','1988-05-22') Inserting Rows with Null Values Explicit method: specify the NULL keyword. INSERT INTO DEPARTEMENT VALUES('Administration',4,'987654321','1995-01-01') Implicit method: omit the column from the column list INSERT INTO DEPARTEMENT (Dname, Dnumber,Mgr_ssn,Mgr_start_date) VALUES('Research',5,'333445555',NULL) INSERT INTO Employee VALUES('James','E','Borg','888665555','1937-11-10','450 Stone, Houston, TX','M',55000,NULL,1) INSERT INTO DEPARTEMENT (Dname, Dnumber,Mgr_ssn) VALUES('Research',5,'333445555')
6
Ins.Ebtesam AL-Etowi 6 Inserting values by Using Variables DECLARE @DP_NUMBER INT SET @DP_NUMBER=5 INSERT INTO DEPARTEMENT (Dname, Dnumber,Mgr_ssn,Mgr_start_date) VALUES('Research',@DP_NUMBER,'333445555',NULL) Copying Rows from Another table You can use the INSERT statement to add rows to a table where the values are derived from exiting tables. write your INSERT statement with a subquery. insert into table[column (, column) ] subquery Do not use the VALUES clause. Match the number of columns in the INSERT clause to those in the subquery
7
Ins.Ebtesam AL-Etowi 7 Copying Rows from Another table insert into DEPARTEMENT (Dname, Dnumber,Mgr_ssn, Mgr_start_date) select Fname,Dno,Ssn,Bdate from employee where Lname = 'Borg'
8
The UPDATE Statement 8 Ins.Ebtesam AL-Etowi Modify existing rows with the UPDATE statement UPDATE table SET column=value[, column=value,..] WHERE condition Update more than one row at a time, if required Specific row or rows are modified when you specify the where clause All rows in the table are modified if you omit the where clause. UPDATE employee set Salary = Salary * 1.1 where Ssn ='333445555’
9
Ins.Ebtesam AL-Etowi 9 UPDATE employee set Salary = Salary * 1.1 Updating with Multiple-Column Subquery Multiple-column subqueries can be implemented in the set clause of an UPDATE statement UPDATE table SET (column, column,……)= (SELECT column,column, … FROM table WHERE condition) WHERE condition
10
Ins.Ebtesam AL-Etowi 10 update employee set Super_ssn=(select Super_ssn from employee where Ssn=987654321) where Ssn =888665555 Updating Rows Based on Another Table Use subqueries in UPDATE statement to update rows in a table based on values from another table UPDATE employee set Dno = (select Dno from employee where Ssn =888665555) where Bdate = (select Bdate from employee where Ssn=888665555)
11
Updating Rows Integrity Constraint Error Ins.Ebtesam AL-Etowi 11 If you attempt to update a record with a value that is tied to an integrity constraint, you will experience an error (the Super_ssn doesn’t exit) update employee set Super_ssn=7665211 where Ssn=333445555
12
The DELETE Statement Ins.Ebtesam AL-Etowi 12 You can remove existing rows from a table by using the DELETE statement DELETE [FROM] table [WHERE condition] Deleting Rows from a Table Specific rows are deleted when you specify the WHERE clause DELETE FROM DEPARTEMENT Where Dnumber=1 * -All rows in the table are deleted if you omit the WHERE clause Deleting Rows Based on Another Table Use subqueries in DELETE statements to remove rows from a table based on values from another table DELETE FROM employee WHERE Dno = (SELECT Dnumber from DEPARTEMENT Where Dname=‘Research’)
13
Creat table 13 Ins.Ebtesam AL-Etowi use CompanyDB CREATE TABLE Employee (Fname CHAR(15) NOT NULL, Minit CHAR, Lname CHAR(15) NOT NULL, Ssn CHAR(9) NOT NULL, Bdate DATE, Address CHAR(30), Sex CHAR, Salary MONEY NOT NULL CHECK (Salary > 0), Super_ssn CHAR(9), Dno INT NOT NULL, CONSTRAINT pk_Ssn PRIMARY KEY (Ssn), CONSTRAINT fk_Super_ssn FOREIGN KEY (Super_ssn) REFERENCES Employee (Ssn)) CREATE TABLE DEPARTEMENT (Dname CHAR(15) NOT NULL, Dnumber INT NOT NULL, Mgr_ssn CHAR(9) NOT NULL, Mgr_start_date DATE, CONSTRAINT pk_Dnumber PRIMARY KEY (Dnumber), CONSTRAINT fk_Mgr_ssn FOREIGN KEY (Mgr_ssn) REFERENCES Employee (Ssn))
14
Ins.Ebtesam AL-Etowi 14 Insert rows into table
15
15 Ins.Ebtesam AL-Etowi
16
The Update Statement Ins.Ebtesam AL-Etowi 16
17
Ins.Ebtesam AL-Etowi 17
18
Ins.Ebtesam AL-Etowi 18
19
Ins.Ebtesam AL-Etowi 19
20
Deleting rows from a table Ins.Ebtesam AL-Etowi 20
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.