Presentation on theme: "Data Definition Languages Atif Farid Mohammad UNCC."— Presentation transcript:
Data Definition Languages Atif Farid Mohammad UNCC
SQL Most popular query language for DBMSs –current standard SQL99 most DBMSs conform.. but some features are still not implemented by some vendors. Based on the relational algebra
DDL – Data Definition Language Creating database objects Altering table definitions Dropping database objects
DDL – Data Definition Language CREATE TABLE Company.Employee ( FNameVARCHAR(15) NOT NULL, MInitCHAR, LName VARCHAR(15) NOT NULL, SSNCHAR(9) NOT NULL PRIMARY KEY, BDateDATE, AddressVARCHAR(40), SexCHAR, SalaryDECIMAL(10,2), SuperSSNCHAR(9), DNoINTEGER NOT NULL REFERENCES Company.Department, FOREIGN KEY (SuperSSN) REFERENCES Company)
Schema Schema is a named collection of objects (tables) in a database. Names need only be unique within their schema! –Create table Company.Employee(…) Company is the schema name Employee is the table name –Create table G1.Employee (…)
Schema (con’t) If no schema is created (or implied by the table creation), DB2 uses your user ID for the schema name. Any tables created by user 3gh3 will be referred to as: 3gh3.TableName You may have noticed in the tutorial that DB2 doesn’t like the numeric at the beginning of a schema name…..so…… We’ll use our groups as our schema… C2.TableName
Alter ALTER used to add columns, to increase the length of an existing VARCHAR attribute or to add or delete constraints In other cases, you need to drop the table & recreate it. ALTER TABLE Company.Employee ADD COLUMN status CHAR(1) ALTER TABLE Company.Department ALTER COLUMN DName SET DATA TYPE VARCHAR(50)
Using Alter to Add Foreign Key Constraints To specify a FK constraint in a create table statement, the referenced table must already be created. Sometimes this is impossible! Circular reference? ALTER TABLE Employee FOREIGN KEY(DNo) REFERENCES Department;
DML - Data Manipulation Language 4 main SQL statements: –INSERT - put new tuples into the database –UPDATE - change values that already exist –DELETE - remove tuples from the database –SELECT - “query”; retrieve data that satisfies some condition or set of conditions.
Batch Loads Import or load command is used to do bulk inserts into the database. import from of DEL insert into Company.Employee “Delimited Text”
Update UPDATE TABLE Company.Employee SET Salary = WHERE SSN =
Delete DELETE FROM Company.Employee * Deletes all records in Company.Employee table DELETE FROM Company.Employee where Salary < * Deletes all Employees who earn less than 25K
Select SELECT [DISTINCT] FROM [ WHERE ] Where: columns = list of attributes to be retrieved tables = list of relations needed to process the query condition = a boolean expression that identifies which tuples are to be retrieved
Assumption For the following examples, we assume the default schema thus eliminating the need to qualify the table names with a schema name. –The default is either the user name or a schema specified as: db2 set schema=H4
Simple Select SELECT plocation, dnum FROM project SELECT DISTINCT plocation, dnum FROM project SQL does not automatically eliminate duplicates.. it returns “multisets” PLOCATIONDNUM Bellaire5 Sugarland5 Houston5 Stafford4 Houston1 Stafford4 PLOCATIONDNUM Bellaire5 Sugarland5 Houston5 Stafford4 Houston1
Simple Select Retrieves everything from Department table (in no particular order) DNAMEDNUMBERMGRSSNMGRSTARTDATE Research Administration Headquarters SELECT dname, dnumber, mgrssn, mgrstartdate FROM department SELECT * FROM department
You should know…
Example… Select topic, Subject from Paper topic, subject (Paper)
Example… Select topic, Subject from Paper, Journal Where Author= First_Author topic, subject Author=First_Author (Paper X Journal)
Example… Select topic from Paper, Journal Where Author= First_Author and Publish_Date = 2014 topic Author=First_Author (Paper X Journal) ^ Publish_Date=2014
Example… Select topic, Subject from Paper, Journal Where Author= First_Author and Publish_Date = 2014 topic, subject Author=First_Author (Paper X Journal) ^ Publish_Date=2014
Table Qualifiers (“Range Variables”) –qualifiers are only necessary when it is unclear which table an attribute belongs to but..they can be used anytime you wish (as in the examples above). –SQL is case insensitive. SELECT E.fname, E.ssn FROM employee E WHERE E.salary > SELECT Employee.fname, Employee.ssn FROM employee WHERE Employee.salary > 40000
Simple Select SELECT fname, lname, address FROM employee, department WHERE dname = ‘Research’ AND dnumber = dno This is a join in SQL! Lists the names & addresses of all employees in the research department. ** Note that the string “Research” is case sensitive! FNAMELNAMEADDRESS JohnSmith731 Fondren, Houston, TX FranklinWong638 Voss, Houston, TX RameshNarayan975 Fire Oak, Humble, TX JoyceEnglish5631 Rice, Houston, TX
How is a query evaluated? Basically (“conceptual evaluation strategy”): –compute the cross product of all tables in the from-list –delete rows in the cross product that fail the “where” condition –delete all columns that do not appear in the select list –if DISTINCT is specified, eliminate duplicate rows This strategy is probably the least efficient way to compute a query! An optimizer will find more efficient strategies to compute the same answers.
Expressions & Strings in Select List each employee’s ssn, their current salary and a listing of their new salary, given that they just received a 10% raise. SELECT ssn, salary, salary * 1.10 AS NewWage FROM Employee WHERE salary > SSNSALARYNEWWAGE
Expressions & Strings in Select Find employees that earn 10,000 more than another employee. SELECT E1.ssn as Emp1, E1.salary, E2.ssn as Emp2, E2.salary FROM Employee E1, Employee E2 WHERE E1.salary = E2.salary EMP1SALARYEMP2SALARY
Pattern Matching Find the names of employees whose dependents name(s) begin with “A”. SELECT lname, dependent_name FROM employee, dependent WHERE dependent_name like ‘A%’ and ssn=essn LNAMEDEPENDENT_NAME SmithAlice WongAlice WallaceAbner
Union Make a list of employees (first names), dependents and their respective birthdates. SELECT fname, bdate FROM employee UNION SELECT dependent_name, bdate FROM dependent 1BDATE James11/10/1937 Jennifer06/20/1941 Abner02/28/1942 Franklin12/08/1955 Joy05/03/1958 Ramesh09/15/1962 Theodore10/25/1963 John01/09/1965 Alice04/05/1966 Elizabeth05/05/1967 Alicia07/19/1968 Ahmad03/29/1969 Joyce07/31/1972 Michael01/04/1988 Alice12/30/1988
Union (column renamed) Make a list of employees (first names), dependents and their respective birthdates. SELECT fname as name, bdate FROM employee UNION SELECT dependent_name as name, bdate FROM dependent NAMEBDATE James11/10/1937 Jennifer06/20/1941 Abner02/28/1942 Franklin12/08/1955 Joy05/03/1958 Ramesh09/15/1962 Theodore10/25/1963 John01/09/1965 Alice04/05/1966 Elizabeth05/05/1967 Alicia07/19/1968 Ahmad03/29/1969 Joyce07/31/1972 Michael01/04/1988 Alice12/30/1988
Union All Duplicates will be removed during a union…note the contrast to general select where we need to specify the DISTINCT keyword. To retain duplicates we use UNION ALL. SELECT fname as name, bdate FROM employee UNION ALL SELECT dependent_name as name, bdate FROM dependent
INTERSECTION Find all employees who work on both project 1 and project 3. SELECT essn FROM works_on WHERE pno = 1 INTERSECT SELECT essn FROM works_on WHERE pno=2 ESSN NOTE: INTERSECT ALL retains duplicates.
EXCEPT (Difference) Find all employees who work on project 2 but not on project 1. SELECT essn FROM works_on WHERE pno = 2 EXCEPT SELECT essn FROM works_on WHERE pno=1 ESSN NOTE: EXCEPT ALL retains duplicates.
Can you express this query in another way? Nested Queries SELECT lname, salary FROM employee WHERE ssn IN (SELECT mgrssn FROM department) Find last name and salary of all managers “nested subquery” produces a (multi)set used by the outer query
Nested Queries SELECT lname, salary FROM employee WHERE ssn NOT IN (SELECT mgrssn FROM department) Find last name and salary of all those who are not managers.