0% found this document useful (0 votes)
18 views

SQL 2

The document describes SQL commands for managing database tables, including INSERT, DELETE, TRUNCATE, DESCRIBE, DROP, UPDATE, and referential triggered actions. It provides syntax examples for each command and explains how they modify table data and structure. Referential triggered actions allow customizing the behavior of foreign key constraints by cascading changes or setting columns to null or default values.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

SQL 2

The document describes SQL commands for managing database tables, including INSERT, DELETE, TRUNCATE, DESCRIBE, DROP, UPDATE, and referential triggered actions. It provides syntax examples for each command and explains how they modify table data and structure. Referential triggered actions allow customizing the behavior of foreign key constraints by cascading changes or setting columns to null or default values.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

INSERT

• Syntax 1: INSERT INTO <tablename> Values(attr_value1, attr_value2,……);


• Ex: INSERT INTO Dept Values(1, ’CE’, ’Nadiad’);
• Attr. values should in in the order of attr. list
• Number of attr. values should be equal to the number of attr. in the attr.
list

dept_id dept_name location


1 CE Nadiad
INSERT
• Syntax 2: INSERT INTO <tablename>(attr1, attr2,attr3,…… )
Values(attr_value1, attr_value2,…….. );
• Ex: INSERT INTO Dept(dept_id, dept_name, location)
Values(2, ’IT’, ’Ahmedabad’);
• Ex: INSERT INTO Dept(dept_id, dept_name)
Values(3, ‘EC’);
dept_id dept_name location
1 CE Nadiad
2 IT Ahmedabad
3 EC NULL
DELETE
• Deletes the data from the existing table
• Syntax: DELETE FROM <tablename> WHERE <condition>;

dept_id dept_name location emp_id dept_id emp_name


1 CE Nadiad 1 3 Abhay
2 IT Ahmedabad 2 2 Kunal
3 EC Bhavnagar 3 1 Chintan
DELETE
• Examples:
• DELETE FROM Emp WHERE emp_id = 1;
emp_id dept_id emp_name
2 2 Kunal
3 1 Chintan

• DELETE FROM Emp;

emp_id dept_id emp_name


DELETE
• DELETE FROM Dept WHERE Dept_id = 1;

dept_id dept_name location emp_id dept_id emp_name


2 IT Ahmedabad 1 3 Abhay
3 EC Bhavnagar 2 2 Kunal
3 1 Chintan
TRUNCATE
• The TRUNCATE command will make the table empty i.e. all the table
data will be deleted but the structure and database object is still alive
and the table can be reused normally.
• Syntax: TRUNCATE TABLE <tablename>;
• Ex: TRUNCATE TABLE Emp;
TRUNCATE TABLE Dept;
TRUNCATE vs DELETE
• DELETE command can be rollbacked. After deleting the data also, if
you use ROLLBACK and SAVEPOINTS TCL commands, deleted data can
be restored into the table as it is. But it is not possible with
TRUNCATE.
• Whenever you are using DELETE command, you can specify
conditions in WHERE clause for deleting so and so records. But, if you
use TRUNCATE command, you cannot specify conditions and all the
records will be deleted.
DESCRIBE
• Describe the structure of the table
• Syntax 1: DESCRIBE <tablename>;
• Syntax 2: DESC <tablename>;
• Ex: DESCRIBE Emp;
DESC Dept;
DROP
• Deletes both data and table
• Syntax: DROP TABLE <tablename>;
• Ex: DROP TABLE Emp;
DROP TABLE Dept;
UPDATE
• Used for updating the data
• Syntax: UPDATE <tablename> SET <attr1> = <value1>, <attr1> = <value1>,..
WHERE <condition>;
• Ex: UPDATE Dept SET dept_name = ‘AI’ WHERE location = ‘Bhvanagar’;

dept_id dept_name location


2 IT Ahmedabad
3 AI Bhavnagar
UPDATE
• UPDATE Emp SET dept_id = 2 WHERE emp_id = 3;

emp_id dept_id emp_name


1 3 Abhay
2 2 Kunal
3 2 Chintan
UPDATE WITHOUT WHERE CLAUSE
• All the values of the column gets updated with new value
• Syntax: UPDATE <tablename> SET <attr1> = <value1>;
UPDATE
• UPDATE Dept SET dept_id = 4 WHERE dept_name = ‘AI’;

dept_id dept_name location emp_id dept_id emp_name


2 IT Ahmedabad 1 3 Abhay
4 AI Bhavnagar 2 2 Kunal
3 2 Chintan
Referential Triggered Actions
• Allows us to further describe the relationship between the referential
column and the object it references by attaching a ‘referential
triggered action’ to the foreign key.
• Three triggered actions:
Referential Triggered Actions

dept_id dept_name emp_id dept_id


1 CE 1 3
2 IT 2 2
3 EC 3 4
4 CH 4 6
5 IC 5 5
6 ME 6 1

CREATE TABLE Emp( emp_id NUMBER, dept_id NUMBER REFERENCES Dept(dept_id));


Referential Triggered Actions
• SET NULL
• dept_id NUMBER REFERENCES Dept(dept_id) ON DELETE SET NULL ON UPDATE SET NULL
• Examples:
• DELETE FROM Dept WHERE dept_id = 5;
• UPDATE Dept SET dept_id = 7 WHERE dept_id = 6;

dept_id dept_name emp_id dept_id

1 CE 1 3

2 IT 2 2

3 EC 3 4

4 CH 4 NULL

7 ME 5 NULL
6 1
Referential Triggered Actions
• SET DEFAULT
• dept_id NUMBER DEFAULT 1 REFERENCES Dept(dept_id) ON DELETE SET DEFAULT ON UPDATE SET DEFAULT
• Examples:
• DELETE FROM Dept WHERE dept_id = 2;
• UPDATE Dept SET dept_id = 8 WHERE dept_id = 3;

dept_id dept_name emp_id dept_id

1 CE 1 1

8 EC 2 1

4 CH 3 4

7 ME 4 NULL
5 NULL
6 1
Referential Triggered Actions
• SET CASCADE
• dept_id NUMBER REFERENCES Dept(dept_id) ON DELETE CASCADE ON UPDATE CASCADE
• Examples:
• DELETE FROM Dept WHERE dept_id = 4;
• UPDATE Dept SET dept_id = 9 WHERE dept_id = 1;

dept_id dept_name emp_id dept_id

9 CE 1 9

8 EC 2 9

7 ME 4 NULL
5 NULL
6 9

You might also like