100% found this document useful (2 votes)
55 views25 pages

Data Insertion, Updating and Deletion: Duration: 3 Hrs

Data Insertion, Updating and Deletion Duration: 3 hrs Detailed Syllabus 4.4. Inserting Data: INSERT INTO [VALUES SELECT] including a column list, null values; obtaining values from a SELECT. 4.4. Updating Data: UPDATE (selected columns, selected rows, with a sub query).

Uploaded by

prasadwicum
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
55 views25 pages

Data Insertion, Updating and Deletion: Duration: 3 Hrs

Data Insertion, Updating and Deletion Duration: 3 hrs Detailed Syllabus 4.4. Inserting Data: INSERT INTO [VALUES SELECT] including a column list, null values; obtaining values from a SELECT. 4.4. Updating Data: UPDATE (selected columns, selected rows, with a sub query).

Uploaded by

prasadwicum
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Data Insertion, Updating and

Deletion

Duration: 3 hrs

1
© 2007, UCSC
Detailed Syllabus
4.4.1 Inserting Data: INSERT INTO
[VALUES|SELECT] including a column list,
null values; obtaining values from a
SELECT.

4.4.2 Updating Data: UPDATE (selected columns,


selected rows, with a sub query).

4.4.3 Deleting Data: DELETE (all data, selected


data, with a sub query).

2
© 2007, UCSC
Command: INSERT

• Function
– Places data one or more rows into a table
– Data can also be downloaded from another
computer system or collected from other sites.

INSERT INTO table-name (column-name,),


| VALUES ([constant, NULL],)
or
| SELECT retrieval condition

© 2007, UCSC
Command: INSERT
i Single-Row Insert
INSERT INTO Employee (Emp_No, Emp_Name, Age,
Dept)
VALUES (‘E1’, ‘Dias’, 26, ‘PER’)

ii Multi-Row Insert
INSERT INTO Manager (Emp_No, Emp_Name, Age, Dept)
SELECT Emp_No, Emp_Name, Age, Dept
FROM Employee
WHERE Job = ‘Manager’

© 2007, UCSC
RESTRICT INSERT
Insert with referential
integrity
In Employee Table
CONSTRAINT Emp_Dep_FK
FOREIGN KEY (Dept) REFERENCES
Department(Dept_Code)

INSERT INTO Employee


VALUES (342, ‘Dias’, 26, ‘Sale’);
An employee can only be inserted if its department is
found in department table

5
© 2007, UCSC
RESTRICT INSERT
Department
Dept_Code Dep_Name Manager

SAL Sales 179

FIN Finance 857

Employee
Emp_No Emp_Name Age Dept
179 Silva 27 SAL
857 Perera 34 FIN
342 Dias 26 Sale ×
6
© 2007, UCSC
Command: UPDATE
• Function: Changes data in one or more rows of a
table
UPDATE table-name
SET (column-name = expression,),
WHERE search-condition

Example

UPDATE STUDCLASS
SET FEES = 1200 Selective Update
WHERE STUDNO = 1234

UPDATE STUDCLASS
SET FEES = 1200 Update All Rows

© 2007, UCSC
Command: UPDATE
Example
Update with Subquery
UPDATE Works_On
SET Hours = 12
WHERE Proj_No IN(SELECT Proj_No FROM Project
WHERE Proj_Name = ‘INFORMATION TECHNOLOGY’)

UPDATE Employee
SET Age = Age+1

© 2007, UCSC
RESTRICT UPDATE
Update with referential
integrity

In Employee Table
CONSTRAINT Emp_Dep_FK
FOREIGN KEY (Dept) REFERENCES
Department(Dept_Code) ON UPDATE RESTRICT

UPDATE Department SET Dept_Code = ‘Sale’


WHERE Dept_Code = ‘SAL’
A department code can only be changed if it is not found in
employee table (i.e. no employees working for them)

9
© 2007, UCSC
RESTRICT UPDATE
Department
Dept_Code Dep_Name Manager

SAL Sales
Finance
179 ×
FIN 857

Employee
Emp_No Emp_Name Age Dept
179 Silva 27 SAL
857 Perera 34 FIN
342 Dias 26 SAL
10
© 2007, UCSC
CASCADE UPDATE
Update with referential integrity
In Employee Table
CONSTRAINT Emp_Dep_FK
FOREIGN KEY (Dept) REFERENCES
Department(Dept_Code) ON UPDATE CASCADE

UPDATE Department SET Dept_Code = ‘Sale’


WHERE Dept_Code = ‘SAL’
Updating a department code will result in changing it in the
employee table (update with new department code for the
employees working for them)

11
© 2007, UCSC
CASCADE UPDATE
Department
Dept_Code Dep_Name Manager
Sale Sales 179
FIN Finance 857

Employee
Emp_No Emp_Name Age Dept
179 Silva 27 Sale
857 Perera 34 FIN
342 Dias 26 Sale
12
© 2007, UCSC
SET NULL UPDATE
Update with referential
integrity

In Employee Table

CONSTRAINT Emp_Dep_FK
FOREIGN KEY (Dept) REFERENCES
Department(Dept_Code) ON UPDATE SET NULL

UPDATE Department SET Dept_Code = ‘Sale’


WHERE Dept_Code = ‘SAL’
Updating a department code will result in changing the
department code of their employees to NULL (only if NULL
values are allowed)
13
© 2007, UCSC
SET NULL UPDATE
Department
Dept_Code Dep_Name Manager

Sale Sales 179


FIN Finance 857

Employee
Em p_N o Em p_N am e Age D ept
1 7 9 S ilv a 27 N U LL
8 5 7 P e re ra 3 4 F IN
3 4 2 D ia s 26 N U LL
14
© 2007, UCSC
SET DEFAULT UPDATE
Update with referential integrity

In Employee Table
CONSTRAINT Emp_Dep_FK
FOREIGN KEY (Dept) REFERENCES
Department(Dept_Code) ON UPDATE
SET DEFAULT ‘XXX’

UPDATE Department SET Dept_Code = ‘Sale’


WHERE Dept_Code = ‘SAL’

Updating a department code will result in changing


the department code of their employees to a default
value 15
© 2007, UCSC
SET DEFAULT
UPDATE
Department
Dept_Code Dep_Name Manager
Sale Sales 179
FIN Finance 857

Employee
Em p_N o Em p_N am e Age D ept
1 7 9 S ilv a 27 X X X
8 5 7 P e re ra 3 4 F IN
3 4 2 D ia s 26 X X X

16
© 2007, UCSC
Command: DELETE
• Function: Removes one or more rows from a table
DELETE FROM table-name
{WHERE search-condition}

Example
DELETE FROM Employee Select Delete
WHERE Emp_No = ‘E1’

DELETE FROM Employee Delete All Rows

DELETE FROM Dependent Delete with Subquery


WHERE Emp_No = (SELECT Emp_No FROM Employee
WHERE Emp_Name = ‘Dias’)
© 2007, UCSC
RESTRICT DELETE
Delete with referential
integrity

In Employee Table

CONSTRAINT Emp_Dep_FK
FOREIGN KEY (Dept) REFERENCES
Department(Dept_Code) ON DELETE RESTRICT

DELETE FROM Department


WHERE Dept_Code = ‘SAL’
A department can only be deleted if it is not found in employee
table (i.e. no employees working for them)

18
© 2007, UCSC
RESTRICT DELETE
Department
Dept_Code Dep_Name Manager

SAL Sales 179 ×


FIN Finance 857

Employee
Emp_No Emp_Name Age Dept
179 Silva 27 SAL
857 Perera 34 FIN
342 Dias 26 SAL
19
© 2007, UCSC
CASCADE DELETE
Delete with referential integrity

In Employee Table

CONSTRAINT Emp_Dep_FK
FOREIGN KEY (Dept) REFERENCES
Department(Dept_Code) ON DELETE CASCADE

DELETE FROM Department


WHERE Dept_Code = ‘SAL’
Deleting a department will result in deleting it from the
employee table (delete employees working for them)

20
© 2007, UCSC
CASCADE DELETE
Department

Dept_Code Dep_Name Manager

SAL Sales 179


×
FIN Finance 857

Employee
Emp_No Emp_Name Age Dept
179 Silva 27 SAL ×
857 Perera 34 FIN
342 Dias 26 SAL ×
21
© 2007, UCSC
SET NULL DELETE
Delete with referential integrity

In Employee Table
CONSTRAINT Emp_Dep_FK
FOREIGN KEY (Dept) REFERENCES
Department(Dept_Code) ON DELETE SET NULL

DELETE FROM Department


WHERE Dept_Code = ‘SAL’
Deleting a department will result in changing the
department of their employees in the employee table to
NULL (only if NULL values are allowed)

22
© 2007, UCSC
SET NULL DELETE
Department
Dept_Code Dep_Name Manager

SAL Sales 179


×
FIN Finance 857

Employee
Em p_No Em p_Nam e Age D ept
1 7 9 S ilv a 27 N U LL
8 5 7 P erera 3 4 F IN
3 4 2 D ia s 26 N U LL
23
© 2007, UCSC
SET DEFAULT
DELETE
Delete with referential integrity

In Employee Table
CONSTRAINT Emp_Dep_FK
FOREIGN KEY (Dept) REFERENCES
Department(Dept_Code) ON DELETE
SET DEFAULT ‘XXX’

DELETE FROM Department


WHERE Dept_Code = ‘SAL’
Deleting a department will result in changing the
department of their employees in the employee table to a
specified default value
24
© 2007, UCSC
SET DEFAULT DELETE
Department
Dept_Code Dep_Name Manager

SAL Sales 179 ×


FIN Finance 857

Employee
Em p_No Em p_Nam e Age Dept
1 7 9 S ilv a 27 X X X
8 5 7 P e re ra 3 4 F IN
3 4 2 D ia s 26 X X X

25
© 2007, UCSC

You might also like