Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamental of Database Systems

Similar presentations


Presentation on theme: "Fundamental of Database Systems"— Presentation transcript:

1 Fundamental of Database Systems 1401312-3
By Dr Abdullah Alzahrani د. عبدالله الزهراني SQL Data Manipulation

2 Reference الكتاب المرجع
Fundamentals of Database Systems, 5th ed., by Elmasri and Navathe, Pearson International Edition, Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

3 DM (Data Manipulation)
SQL DM DM (Data Manipulation) INSERT UPDATE DELETE Some of the materials and examples are taken from: and the reference book Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

4 SQL DM The INSERT Command
INSERT is used to add a single tuple to a relation We must specify the relation name and a list of values for the tuple. The values should be listed in the same order in which the corresponding attributes were specified. Attributes not specified in a command are set to their DEFAULT or to NULL Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

5 SQL DM The INSERT Command Forms 1st Form: inserting complete row :
INSERT INTO table_name VALUES (value1,value2,value3,...); 2nd Form: inserting partial row : INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...); Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

6 SQL DM The INSERT Command Forms
3rd Form: inserting multiple rows (tuples): INSERT INTO table_name  VALUES (value1,value2,value3,...) , (value1,value2,value3,...), (value1,value2,value3,...); Or INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...) , (value1,value2,value3,...), (value1,value2,value3,...); 4th Form copying data from another table: If the tables have same structure we need to copy complete tuples: INSERT INTO table2 SELECT * FROM table1; If we need to copy partial tuples from another table: INSERT INTO table2 (column_name(s)) SELECT column_name(s) FROM table1; Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

7 SQL DM Customers table STUDENT table
Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

8 SQL DM INSERT 1st form Example
The following SQL statement will insert a new complete row into Customers table: Example #1 insert into customers values (3,'Thomas', 'Jefferson', '931 Thomas Jefferson Pkwy', 'Charlottesville', 'VA', '22902'); Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

9 SQL DM Example #1 Results Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa
Fundamental of Database Systems 9/20/2016

10 SQL DM INSERT 2nd form Example
The following SQL statement will insert a new partial row into STUDENT table: Example #2 insert into student (student_id, student_name, GPA) values (438011,'Omar A', 2.1); Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

11 SQL DM Example #2 Results Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa
Fundamental of Database Systems 9/20/2016

12 SQL DM INSERT 3rd form Example
The following SQL statement will insert multiple new complete rows into Customers table: Example #3 insert into customers values (4,'James', 'Madison', ' Constitution Hwy', 'Orange', 'VA', '22960'), (5,'James', 'Monroe', '2050 James Monroe Parkway', 'Charlottesville', 'VA', '22902'); Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

13 SQL DM Example #3 Results Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa
Fundamental of Database Systems 9/20/2016

14 SQL DM INSERT 4th form Example
The following SQL statement will insert a new partial row from customers table where customer_id=2 into STUDENT table: Example #4 insert into student (student_id, student_name, ) SELECT customer_id,last_name, FROM customers where customer_id=2 ; Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

15 SQL DM Example #4 Results Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa
Fundamental of Database Systems 9/20/2016

16 SQL DM The UPDATE Command
UPDATE is used to modify attribute values of one or more selected tuples a WHERE clause in the UPDATE command selects the tuples to be modified from a single relation. SET clause in the UPDATE command specifies the attributes to be modified and their new values. Several tuples can be modified with a single UPDATE command An example is to give all employees in the ‘Research’ (Dno=5) department a 10 percent raise in salary Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

17 SQL DM The UPDATE Command FORM
UPDATE table_name SET column1=value1,column2=value2,... WHERE some_column=some_value; Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

18 SQL DM Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa 9/20/2016
Fundamental of Database Systems 9/20/2016

19 SQL DM UPDATE Examples Example #5
The following SQL statement will register new students (Adams & Omar who are not registered in any department) into Information Technology department (DEP_ID =3) : UPDATE student SET DEP_ID=3 WHERE DEP_ID=0; Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

20 SQL DM Example #5 Results Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa
Fundamental of Database Systems 9/20/2016

21 SQL DM UPDATE Examples Example #6
The following SQL statement will add 0.1 to all students GPA in STUDENT table : UPDATE student SET GPA=GPA + 0.1; Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

22 SQL DM Example #6 Results Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa
Fundamental of Database Systems 9/20/2016

23 SQL DM The DELETE Command DELETE removes tuples from a relation.
It includes a WHERE clause to select the tuples to be deleted. Tuples are explicitly deleted from only one table at a time. Several tuples can be deleted with a single DELETE command A missing WHERE clause specifies that all tuples in the relation are to be deleted; however, the table remains in the database as an empty table. Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

24 SQL DM The DELETE Command FORM
DELETE FROM table_name WHERE some_column=some_value; Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

25 SQL DM Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa 9/20/2016
Fundamental of Database Systems 9/20/2016

26 SQL DM DELETE Examples Example #7
The following SQL statement will remove all students who have no recorded addresses from STUDENT tables: DELETE FROM student WHERE is null; Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

27 SQL DM Example #7 Results Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa
Fundamental of Database Systems 9/20/2016

28 SQL DM DELETE Examples Example #8
The following SQL statement will remove all students from STUDENT tables : DELETE FROM student; Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

29 SQL DM Example #8 Results Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa
Fundamental of Database Systems 9/20/2016

30 End SQL Queries Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa 9/20/2016
Fundamental of Database Systems 9/20/2016


Download ppt "Fundamental of Database Systems"

Similar presentations


Ads by Google