Fundamental of Database Systems 1401312-3 By Dr Abdullah Alzahrani د. عبدالله الزهراني aahzahrani@uqu.edu.sa SQL Data Manipulation
Reference الكتاب المرجع Fundamentals of Database Systems, 5th ed., by Elmasri and Navathe, Pearson International Edition, 2007. http://dev.mysql.com/doc/ Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
DM (Data Manipulation) SQL DM DM (Data Manipulation) INSERT UPDATE DELETE Some of the materials and examples are taken from: http://www.w3schools.com/ and the reference book Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
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. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
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. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
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. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
SQL DM Customers table STUDENT table Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
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', 'tjefferson@usa.gov', '931 Thomas Jefferson Pkwy', 'Charlottesville', 'VA', '22902'); Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
SQL DM Example #1 Results Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
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. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
SQL DM Example #2 Results Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
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', 'jmadison@usa.gov', '11350 Constitution Hwy', 'Orange', 'VA', '22960'), (5,'James', 'Monroe', 'jmonroe@usa.gov', '2050 James Monroe Parkway', 'Charlottesville', 'VA', '22902'); Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
SQL DM Example #3 Results Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
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, email) SELECT customer_id,last_name,email FROM customers where customer_id=2 ; Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
SQL DM Example #4 Results Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
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. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
SQL DM The UPDATE Command FORM UPDATE table_name SET column1=value1,column2=value2,... WHERE some_column=some_value; Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
SQL DM Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa 9/20/2016 Fundamental of Database Systems 1401312-3 9/20/2016
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. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
SQL DM Example #5 Results Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
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. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
SQL DM Example #6 Results Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
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. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
SQL DM The DELETE Command FORM DELETE FROM table_name WHERE some_column=some_value; Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
SQL DM Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa 9/20/2016 Fundamental of Database Systems 1401312-3 9/20/2016
SQL DM DELETE Examples Example #7 The following SQL statement will remove all students who have no recorded email addresses from STUDENT tables: DELETE FROM student WHERE email is null; Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
SQL DM Example #7 Results Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
SQL DM DELETE Examples Example #8 The following SQL statement will remove all students from STUDENT tables : DELETE FROM student; Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
SQL DM Example #8 Results Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016
End SQL Queries Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa 9/20/2016 Fundamental of Database Systems 1401312-3 9/20/2016