In the name of Allah Islamic University of Gaza Faculty of Engineering Computer Engineering Department ECOM 4113 DataBase Lab Lab # 3 Data Manipulation Language (DML) El-masry 2013
Objective To be familiar with another part of SQL Language that is Data Manipulation Language (DML). INSERTING DATA INSERT INTO command used to insert rows in a table. - Basic Use: INSERT INTO TableName VALUES (Data Values); The data values are listed in the order in which the columns appear in the table, separated by commas. - Specific Columns: INSERT INTO TName (Columns Names) VALUES(Data Values); - Multiple rows in a single command: INSERT INTO TName (Columns Names) VALUES (R1 Values), (R2 Values), (R3 Values); 2
QUERIES The command to retrieve data from a database is called a query. In SQL the SELECT command is used to specify queries. Attribute List : SELECT <attribute list> [AS COL. ALIAS NAME] FROM <Table List> [AS TABLE ALIAS NAME] [ WHERE <condition> ] [ GROUP BY <grouping attributes> [ HAVING <group selection condition> ] ] [ ORDER BY <column name(s)> [ASC DESC] ] It is a list of the attributes we want to retrieve. It can be: * It is a fast alternative to all column name. Q0: Retrieve DNAME, DNUMBER, MGRSSN and MGRSTARTDATE for each Dapartments. Column(s) name(s). If multitable query refers to two or more attributes with the same name, the relation name prefixed to the attribute name and separating the two by a period. Q1: Retrieve the salary for each Employee. 3
DISTINCT <column(s) name(s)> In a table, some of the columns may contain duplicate values. If you want to list only the different (distinct) values in a table. The DISTINCT keyword can be used to return only distinct (different) values. Q2: Retrieve all distinct salary values. function ([ DISTINCT] <column name> * ) Aggregate Functions in SQL Aggregate functions are used to summarize information from multiple tuples into a single-tuple summary. Grouping is used to create subgroups of tuples before summarization. A number of built-in aggregate functions exist: COUNT, SUM, MAX, MIN, and AVG. The COUNT function returns the number of tuples or values as specified in a query. The functions SUM, MAX, MIN, and AVG can be applied to a set or multiset of numeric values and return, respectively, the sum, maximum value, minimum value, and average 4
(mean) of those values. These functions can be used in the SELECT clause or in a HAVING clause (which we introduce later). Q3: Find the sum of the salaries of all employees, the maximum salary, the minimum salary, and the average salary. Q4: Retrieve the total number of employees in the company. Q5: Count the number of distinct salary values in the database. 5
Table List It can be a table or joined table. Joined Tables A joined table is a table derived from two other (real or derived) tables according to the rules of the particular join type. Join Types 1. CROSS JOIN T1 CROSS JOIN T2 T1,T2 The joined table will contain a row for every possible combination of rows from T1 to T2 consisting of all columns in T1 followed by all columns in T2. If the tables have N and M rows respectively, the joined table will have N * M rows. OR 6
2. INNER JOIN For each row R1 of T1, the joined table has a row for each row in T2 that satisfies the join condition with R1. Join Condition It can be: ON boolean_expression T1 [INNER] JOIN T2 Join_Condition The output of JION ON has all columns of the two tables. USING ( join column list ) It takes a comma-separated list of equated column names from the two tables. The output of JOIN USING has one column for each of the equated pairs of input columns, followed by the remaining columns from each table. 7
T1 NATURAL [INNER] JOIN T2 It forms a USING list consisting of all column names that appear in both input tables. As with USING, these columns appear only once in the output table. 3. LEFT OUTER JOIN T1 LEFT [OUTER] JOIN T2 Join_Condition First, an inner join is performed. Then, for each row in T1 that does not satisfy the join condition with any row in T2, a joined row is added with null values in columns of T2. Thus, the joined table always has at least one row for each row in T1. 8
4. RIGHT OUTER JOIN T1 RIGHT [OUTER] JOIN T2 Join_Condition First, an inner join is performed. Then, for each row in T2 that does not satisfy the join condition with any row in T1, a joined row is added with null values in columns of T1. This is the converse of a left join. The result table will always have a row for each row in T2. 5. FULL OUTER JOIN T1 FULL [OUTER] JOIN T2 Join_Condition First, an inner join is performed. Then, for each row in T1 that does not satisfy the join condition with any row in T2, a joined row is added with null values in columns of T2. Also, for each row of T2 that does not satisfy the join condition with any row in T1, a joined row with null values in the columns of T1 is added. 9
AS (alias) You can give a table or a column another name by using an alias. This can be a good thing to do if you have very long or complex table names or column names. An alias name could be anything, but usually it is short. SELECT COLName AS alias from TableName AS alias WHERE Clause Operators: WHERE column_name operator Value Operator Meaning = Equal <> Not equal < Less than > Greater than <= Less than or equal >= Greater than or equal between Between an inclusive range in One of multi-value Like Pattern matching: %: any number of any characters. _: one character. 10
Q6: Retrieve the birth date and address of the employee(s) whose name is John B. Smith. Q7: Retrieve the name and address of all employees who work for the Research department. OR Q8: For every project located in Stafford, list the project number, the controlling department number, and the department manager s last name, address, and birth date. 11
Q9: For each employee, retrieve the employee s first and last name and the first and last name of his or her immediate supervisor. Q10: Retrieve all employees whose address is in Houston, Texas. Q11: Show the resulting salaries if every employee working on the ProductX project is given a 10 percent raise. 12
Q12: Retrieve all employees in department 5 whose salary is between $30,000 and $40,000. Q13: Find the sum of the salaries of all employees of the Research department, as well as the maximum salary, the minimum salary, and the average salary in this department. Q14: Retrieve the number of employees in the Research department. Is NULL When we want to compare any value with Null or not null, we use is null and is not null respectively. Q15: Retrieve all employees who have no manager. 13
Grouping: The GROUP BY and HAVING Clauses GROUP BY Clause is used to group together those rows in a table that have the same values in all the columns listed in selection list. Group by <grouping attributes> HAVING provides a condition on the groups which necessarily involve an aggregate function. HAVING <group selection condition> Q16: For each department, retrieve the department number, the number of employees in the department, and their average salary. Q17: For each project, retrieve the project number, the project name, and the number of employees who work on that project. 14
Q18: For each project on which more than two employees work, retrieve the project number, the project name, and the number of employees who work on the project. Ordering of Query Results ORDER BY clause allows the user to order the tuples in the result of a query by the values of one or more of the attributes that appear in the query result. The default order is in ascending order of values. ORDER BY <column name(s)> [ASC DESC] Q19: Retrieve a list of employees and the projects they are working on, ordered by department and, within each department, ordered alphabetically by last name, then first name. 15
Updating Data UPDATE command is used to modify attribute values of one or more selected tuples. UPDATE TName SET col1=v1[, col2= ] where selection condition; U1: Change the location and controlling department number of project number 10 to Bellaire and 5, respectively. Deleting Data DELETE command removes tuples from a relation. DELETE FROM TName WHERE selection condition; If you simply write: DELETE FROM TName; Then all rows in the table will be deleted! 16
INSERT INTO WITH SELECT A variation of the INSERT command inserts multiple tuples into a relation in conjunction with creating the relation and loading it with the result of a query. 17
EXERCISE It will be in the lab. Prepare yourself well to get bonus. 18