Top Banner
Database Database cs453 Lab5 1 Ins.Ebtesam AL-Etowi
20

DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

Jan 05, 2016

Download

Documents

Arabella Hart
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

DatabaseDatabase

cs453

Lab5

1Ins.Ebtesam AL-Etowi

Page 2: DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

Objectives

After completing this lesson, you should be able to do the following:

Insert, Updating and deleting rows in a tableQuery with select .

2Ins.Ebtesam AL-Etowi

Page 3: DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

Data Manipulation Language

Ins.Ebtesam AL-Etowi 3

A DML statement is executed when you:Add new rows to a tableModify existing rows in a tableRemove existing rows from a table

A transaction consists of a collection of DML statements that form a logical unit of work.

Page 4: DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

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

Page 5: DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

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 DEPARTEMENTVALUES('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 EmployeeVALUES('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')

Page 6: DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

Ins.Ebtesam AL-Etowi 6

Inserting values by Using Variables

DECLARE @DP_NUMBER INTSET @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

Page 7: DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

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'

Page 8: DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

The UPDATE Statement

8Ins.Ebtesam AL-Etowi

Modify existing rows with the UPDATE statement

UPDATE tableSET 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’

Page 9: DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

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 tableSET (column, column,……)=

( SELECT column,column… , FROM table

WHERE condition)WHERE condition

Page 10: DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

Ins.Ebtesam AL-Etowi 10

update employeeset 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 employeeset Dno = (select Dno from employee

where Ssn =888665555)where Bdate = (select Bdate from employee

where Ssn=888665555)

Page 11: DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

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

Page 12: DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

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 DEPARTEMENTWhere Dnumber=1

*- All rows in the table are deleted if you omit the WHERE clauseDeleting Rows Based on Another Table

Use subqueries in DELETE statements to remove rows from a table based on values from another table

DELETE FROM employeeWHERE Dno=

) SELECT Dnumber from DEPARTEMENT Where Dname=‘Research)’

Page 13: DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

Creat table

13Ins.Ebtesam AL-Etowi

use CompanyDBCREATE 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))

Page 14: DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

Ins.Ebtesam AL-Etowi 14

Insert rows into table

Page 15: DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

15Ins.Ebtesam AL-Etowi

Page 16: DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

The Update Statement

Ins.Ebtesam AL-Etowi 16

Page 17: DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

Ins.Ebtesam AL-Etowi 17

Page 18: DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

Ins.Ebtesam AL-Etowi 18

Page 19: DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

Ins.Ebtesam AL-Etowi 19

Page 20: DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.

Deleting rows from a table

Ins.Ebtesam AL-Etowi 20