Docsity

Lecture Slides on SQL Update - Database Systems | CSCI 585, Study notes of Deductive Database Systems

Partial preview of the text

Download Lecture Slides on SQL Update - Database Systems | CSCI 585 and more Study notes Deductive Database Systems in PDF only on Docsity!

USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani

SQL: Update

USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani

Example Schema

Dept (DeptNo, Name, Location) Emp (EmpNo, Name, Job, Sal, Comm, HireDate, Mgr, DeptNo)

DeptNo

Number(2) Name^

Varchar2(14) Location

Varchar2(13)

EmpNo

Number(4) Name^

Varchar2(10) Job^

Varchar2(9) Sal^

Number(7,2) Comm

Number(7,2) HireDate

Date Mgr^

Number(4) DeptNo

Number(2) Emp

Dept

USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani

INSERT Statement

To create a tuple in SQL the following ‘Insert’ command is required:insert into R (

attribute

,^ attribute 1

, …^ attribute 2

) n

values (

value^1

,^ value

, …^ value 2

) n

insert into Emp

(empno, name, job, sal, comm, hiredate, mgr, deptno

)

values (7839, ‘King’, ‘President’, 5000, NULL, ‘17-Nov-81’, NULL, 10)insert into Emp

(empno, name, job, sal, comm, hiredate, mgr, deptno

)

insert into Dept values (7698, ‘Blake’, ‘Manager’, 1600, NULL, ’01-May-81’, 7839, 30)

(deptno, name, location

)

values (10, ‘Accounting’, ‘New York’)insert into Dept

(deptno, name, location

)

values (30, ‘Sales’, ‘Chicago)

The insert order matters interms of referential integrity

constraints!

USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani

Group INSERT Statement

To create a set of tuples in SQL the following ‘Insert’ command can be used:insert into R (

attribute

,^ attribute 1

, …^ attribute 2

) n

select (

attribute

,^ attribute 1

, …^ attribute 2

) n

from^ relation

,^ relation^1

, …^ relation 2

n

[where

condition-expression

]

[group by

attribute

,^ attribute 1

, …^ attribute 2

] n^

[having

condition-expression

]

[order by

attribute

,^ attribute 1

, …^ attribute 2

] n

Example: copy details of all employees that work in department 10 fromthe Emp relation into the DepA relation.insert into DepA (staffno, name, job, hiredate)select empno, name, job, hiredatefrom Empwhere deptno = 10;

correspondingattributes have tobe of the same type

Each tuple to be inserted has to be unique!

USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani

UPDATE Statement

To alter a set of tuples in SQL the following ‘Update’ command is used:update Rset^ attribute

= datavalue 1

attribute

= datavalue 2

attribute

= datavaluen

n

[where

condition-expression

]

Example: increase the salary of all employees that work in department10 by 10%.update Empset sal = sal *1.1where deptno = 10;

If the where clause is omitted then

all

tuples in the relation will be altered!

USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani

ORDER BY

select emp.name, dept.namefrom Emp, Deptwhere (emp.deptno = dept.deptno)and (emp.deptno = 10 or emp.deptno = 30) order by

emp.name

asc ;

Blake^

Sales King^

Accounting

select emp.name, dept.namefrom Emp, Deptwhere (emp.deptno = dept.deptno)and (emp.deptno = 10 or emp.deptno = 30) order by

dept.name

desc ;

Blake^

Sales King^

Accounting

select namefrom Dept order by

name ;

AccountingSales

Remember in relations^ Remember in relationsneither tuples nor attributesneither tuples nor attributeshave any intrinsic order!have any intrinsic order!

USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani

Example ™^ A possible view definitionCREATE VIEW

StudOccupied

AS

SELECT

g.StudId,

SUM(

Hours

) AS^

Occupied

FROM

Grades g, Course p WHERE

g.CourId

=^ p.CourId

AND

Grade

IS NULL

GROUP BY

StudId

;

™^ Deleting a viewDROP VIEW

StudOccupied

;