Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Definition Languages Atif Farid Mohammad UNCC.

Similar presentations


Presentation on theme: "Data Definition Languages Atif Farid Mohammad UNCC."— Presentation transcript:

1 Data Definition Languages Atif Farid Mohammad UNCC

2 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

3 DDL – Data Definition Language Creating database objects Altering table definitions Dropping database objects

4 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)

5 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 (…)

6 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

7 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)

8 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;

9 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.

10 Insert INSERT INTO Company.Employee VALUES (‘Paul’, ‘E’, ‘Fish’, , ‘ ’, ‘200 First Ave’, ‘M’, 36000, , 5) INSERT INTO Company.Employee (FName, Lname, SSN, DNo) VALUES (‘Paul’, ‘Fish’, , 5) INSERT INTO Company.Employee (FName, Lname, SSN, DNo) VALUES (‘Paul’, ‘Fish’, , 5), (‘Sally’, ‘Lambert’, , 4)

11 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”

12 Update UPDATE TABLE Company.Employee SET Salary = WHERE SSN =

13 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

14 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

15 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

16 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

17 Simple Select SELECT fname, lname FROM employee WHERE salary >  Fname, Lname (  salary > Employee) FNAMELNAME JenniferWallace JamesBorg

18 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

19 You should know…

20 Example… Select topic, Subject from Paper topic, subject (Paper)

21 Example… Select topic, Subject from Paper, Journal Where Author= First_Author topic, subject Author=First_Author (Paper X Journal)

22 Example… Select topic from Paper, Journal Where Author= First_Author and Publish_Date = 2014 topic Author=First_Author (Paper X Journal) ^ Publish_Date=2014

23 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

24 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

25 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

26 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.

27 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

28 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

29 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

30 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

31 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

32 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

33 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.

34 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.

35 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

36 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.


Download ppt "Data Definition Languages Atif Farid Mohammad UNCC."

Similar presentations


Ads by Google