Part 4: Database Language - SQL
|
|
|
- Alicia Agatha Tucker
- 10 years ago
- Views:
Transcription
1 Part 4: Database Language - SQL Junping Sun Database Systems 4-1 Database Languages and Implementation Data Model Data Model = Data Schema + Database Operations + Constraints Database Languages such as SQL and QUEL can be viewed as a tool to implement database schema and data operations at logical or implementation level. Database Language = Database Definition Language (DDL) + Database Manipulation Language (DML) DDL implements database schema DML implements database operations Separation of DDL and DML is the major distinction between the application systems developed by database languages and developed by programming languages. Junping Sun Database Systems 4-2 Page 1
2 SQL - Structural Query Language SQL: It is the most accepted and implemented interface language for relational database systems(intergalactic dataspeak). History of Relational Database Languages: SEQUEL ( ) It was the Application Programing Interface (API) to System R. It was revised to SEQUEL/2 after several years, and later SEQUEL/2 was changed to SQL. SQL/DS (1981) DB2 (1983) SQL (ANSI-86) the first standardized version of SQL, called SQL1 SQL (ANSI-89) SQL (ANSI-92), called SQL2 SQL3, support recursive operation and object-oriented paradigm SQL-99 Standard Junping Sun Database Systems 4-3 Data Definition Schema Definition at Three Level of Databases: View data schema (table) definition: A view table can be defined on the top of one or more base table Base data table schema definition: A base table is corresponding to one physical data file in the storage system. Physical Each base table can be stored in different type of storage schema or data organization structure such as sequential file, hash index, ISAM, VSAM B-Tree, B + -Tree, B * -Tree, K-D Tree, KDB Tree, R-Tree, R + -Tree, R * -Tree Integrity constraints on schema Authorization, and security mechanism on user defined database operations such as query, update, and insert/delete operations. Junping Sun Database Systems 4-4 Page 2
3 Data Definition Create Statements: create table statement (to define a base table) create index statement (to define an index at internal level) create view statement (to define an view at user level) create schema statement (to treat a database as whole unit in SQL89 &SQL2) Drop Statements: drop table statement (to delete the definition and all instances of the table) drop index statement (to remove an existing index) drop view statement (to delete the view) drop schema statement (to delete schema) Junping Sun Database Systems 4-5 SQL Schema: Schema and Catalog in ANSI-SQL Standard It is identified by a schema name, and includes an authorization identifier to indicate the user or account who owns the schema. Example: CREATE SCHEMA COMPANY AUTHORIZATION JSMITH; It creates a schema called COMPANY, owned by the user with authorization identifier JSMITH. Syntax: schema ::= CREATE SCHEMA schema-name AUTHORIZATION user [ schema-element-list ] Junping Sun Database Systems 4-6 Page 3
4 CREATE TABLE EMPLOYEE Statement CREATE TABLE EMPLOYEE (NAME VARCHAR2(19) NOT NULL, SSN CHAR(9), BDATE DATE, ADDRESS VARCHAR(30), SEX CHAR, SALARY NUMBER(10,2), SUPERSSN CHAR(9), DNO VARCHAR(8) NOT NULL, CONSTRAINT EMPPK PRIMARY KEY(SSN), CONSTRAINT EMPSUPERFRK FOREIGN KEY (SUPERSSN) REFERENCES EMPLOYEE (SSN) DISABLE, CONSTRAINT EMPDUMFRK FOREIGN KEY (DNO) REFERENCES DEPARTMENT (DNUMBER) DISABLE); The constraint can be enabled by using the ALTER TABLE statement after the data is loaded into the table. ALTER TABLE EMPLOYEE ENABLE CONSTRAINT EMPSUPERFRK; Junping Sun Database Systems 4-7 Specifying Referential Triggered Actions CREATE TABLE EMPLOYEE (NAME VARCHAR2(19) NOT NULL, SSN CHAR(9), BDATE DATE, ADDRESS VARCHAR(30), SEX CHAR, SALARY NUMBER(10,2) CHECK SALARY BETWEEN AND 99000, DNO VARCHAR(9) NOT NULL DEFAULT 1, CONSTRAINT EMPPK PRIMARY KEY (SSN), CONSTRAINT EMPSUPERFK FOREIGN KEY (SUPERSSN) REFERENCES EMPLOYEE (SSN) ON DELETE CASCADE DISABLE); ORACLE supports ON DELETE CASCADE. Junping Sun Database Systems 4-8 Page 4
5 Specifying Referential Triggered Actions CREATE TABLE DEPARTMENT (DNAME VARCHAR2(15) NOT NULL, DNUMBER VARCHAR(8), MGRSSN CHAR(9) NOT NULL DEFAULT , CONSTRAINT DEPTPK PRIMARY KEY (DNUMBER), CONSTRAINT DEPTSK UNIQUE (DNAME), CONSTRAINT DEPTMGRFRK FOREIGN KEY (MGRSSN) REFERENCES EMPLOYEE(SSN) ON DELETE CASCADE DISABLE); ALTER TABLE EMPLOYEE ADD (CONSTRAINT EMPDNOFRK FOREIGN KEY (DNO) REFERENCES DEPARTMENT(DNUMBER) ); Junping Sun Database Systems 4-9 Data Types SQL Data Types: (ANSI-SQL) CHARACTER(n) CHARACTER VARYING(n) NUMERIC(p,s) DECIMAL(p,s) INTEGER INT SMALLINT FLOAT(b) DOUBLE PRECISION REAL DATE SQL Data Types: (ORACLE) CHAR(n) VARCHAR(n) VARCHAR2(2) NUMBER(p,s) NUMBER(38) NUMBER DATE RAW LONG LONG RAW ROWID Junping Sun Database Systems 4-10 Page 5
6 Data Manipulation in SQL Data Manipulation at Base Table Level: Query the database via statement Modify data (tuples) in a table of the database via update statement Remove data (tuples) a table of the database via delete statement. Append data (tuples) into a table in the database via insert statement. Data Manipulation at View (virtual table) Level: Query the partial database via statement view Update or modify the partial data defined at the view level mapping view update to the underlying base table single table update multiple table update still has unsolved problem. Junping Sun Database Systems 4-11 Query Database In SQL Querying database in SQL is done via statement. General format of statement: where <attribute list> <table list> <condition> <attribute list> is a list of attribute names whose values are to be retrieved by the query. <table list> is a list of the relation names required to process the query. multiple tables listed in the <table list> implies join operation involved. <condition> is a conditional (Boolean) expression that identifies the tuples to be retrieved by the query. <condition> specifies the ion and join operations. <condition> can include another statement as a subquery of nested query. Junping Sun Database Systems 4-12 Page 6
7 SELECT-PROJECT QUERY Q0: Retrieve the birth date and address of the employee whose name is John B. Smith. SQL Script for Q0: Q0: bdate, address employee where fname = John and minit = B and lname = Smith ; Relation Algebra Expression for Q0: S <bdate, address> ( V fname = John and minit = b and lname = smith (employee) Target Attribute: Constraint: Target Relation: bdate, address fname = John and minit = B and lname = Smith employee Junping Sun Database Systems 4-13 SELECT-PROJECT-JOIN QUERY Q1. Retrieve the first and last names and addresses of all employees who work for the 'Research ' department. fname, lname, address employee, department where dname = 'Research' and dnumber = dno; Target Attributes: Constraint: Select Condition: Join Condition: Target Relations: fname, lname, address dname = 'Research' dnumber = dno employee, department This query involves one ion on department relation and a join on relations employee and department. Junping Sun Database Systems 4-14 Page 7
8 Q2. For every project located in 'Stafford, list the project number, the controlling department number, and the department manager's last name, address, and birthdate. where pnumber, dnum, lname, address, bdate project, department, employee plocation = 'Stafford' and dnum = dnumber and mgrssn = ssn; Target Attributes: pnumber, dnum, lname, address, bdate Constraints: Select Condition: plocation='stafford' Join Condition: dnum=dnumber, mgrssn = ssn Target Relations: project, department, employee ion operation on project relation to project tuples located in 'Stafford'. join with project and department relation to find the controlling department join with department and employee relation to find manager s information in employee relation. two join operations implement two relationships in ER schema of the database, MANAGES and Controls. Junping Sun Database Systems 4-15 Dealing with Ambiguous Attribute Names and Aliasing Q1A: fname, lname, address employee, department where department.dname = 'Research' and department.dnumber = employee.dnumber ; if the attribute names for department number are the same in both employee and department tables, then qualifier will be necessary in specifying a query to avoid ambiguity. Q8. For each employee, retrieve the employee's first and last name and the first and last name of his or her immediate supervisor. e.fname, e.lname, s.fname, s.lname employee e, employee s where e.superssn = s.ssn; Junping Sun Database Systems 4-16 Page 8
9 Discussion on Aliasing ambiguity will arise in the case of queries that refer to the same relation name twice. the above query statement declares alternative relation names of employee relation e and s. e and s can be imagined as two different copies of the employee relation. e represents employees in the role of supervisees s represents employees in the role of supervisors join and ion operations are involved. join attributes are superssn and ssn. the join condition e.superssn = s.ssn links the employee s supervisor s corresponding information such as fname and lname. the join condition implements the recursive relationship supervision in original ER schema. this is an example of one level recursion. a general recursive query, with unknown number of levels, can be not specified. Junping Sun Database Systems 4-17 Query with PROJECT: Query Examples Q9: List all employees social security number. ssn employee; Query with SELECT: Q1C: Retrieve all employees tuples department 5. * employee where dno = 5; Junping Sun Database Systems 4-18 Page 9
10 Query Examples Query with CARTESIAN PRODUCT: Q10: List all combinations of EMPLOYEE SSN and DEPARTMENT DNAME ssn, dname employee, department; Query with Retrieving Distinct Attribute Values: Q11: Retrieve the salary of every employee ALL salary employee; Q11A: Retrieve all distinct salary values DISTINCT salary employee; Junping Sun Database Systems 4-19 Query Involving with Union Q4. Make a list of all project numbers for projects that involve an employee whose last name is Smith as a worker or as a manager of the department that controls the project. ( where union ( where distinct pnumber project, employee, department lname = Smith and dnum = dnumber and mgrssn = ssn) distinct pnumber project, employee, works_on lname = Smith and pnumber = pno and essn = ssn); the first query retrieves the projects that involve a 'Smith' as a manager of department that controls the project. the second query retrieves the projects that involve a 'Smith' as a worker on the project. if several employees have the last name 'Smith', the project names involving any of them would be retrieved. Junping Sun Database Systems 4-20 Page 10
11 Discussion The first part of union: Target Attributes: Constraints: Select Condition: Join Condition: Target Relations: pnumber lname = Smith dnum = dnumber (implement relationship control) mgrssn = ssn (implement relationship manager) project, employee, department The second part of union: Target Attributes: pnumber Constraints: Select Condition: lname = Smith Join Condition: pnumber = pno and essn = ssn (implement M:N relationship works_on) Target Relations: project, employee, works_on Junping Sun Database Systems 4-21 Predicate IN The IN predicates s those rows for which a specified value appears in a list of constant values enclosed in parentheses or the results a subquery. Q13: Retrieve the social security numbers of all employees who work on any one of the project with project number 1, 2, or 3. distinct essn works_on where pno in (1, 2, 3); Result the query: essn Junping Sun Database Systems 4-22 Page 11
12 Workson Table Junping Sun Database Systems 4-23 Predicate NOT IN The NOT IN predicate is true if the expression preceding the keyword IN does not match any value in the list. Q13b: Retrieve the social security numbers of all employees who work on the project other than projects 1, 2, and 3. essn works_on where pno not in (1, 2, 3); Result the query: essn Junping Sun Database Systems 4-24 Page 12
13 Quantifier ANY/SOME Predicate ANY /SOME: The ANY/SOME predicates those rows for which a specified value appears in the results a subquery. Query: Retrieve the social security numbers of employees who works on some projects controlled by department 5. distinct essn works_on where pno = any ( pnumber project where dnum = 5); =any predicate is same as the IN predicate. ANSI-SQL supports both ANY and SOME predicates, even they are equivalent. ORACLE only supports ANY predicate not SOME. The difference between IN and = ANY(=SOME) predicates is that IN could be connected with a set of values but ANY(SOME) only subqueries. Junping Sun Database Systems 4-25 Quantifier SOME and ANY Both SOME and ANY are designed to link a simple relational operator with a subquery that return a multi-row result. The sequence preceding the subquery has the following format: {expression relational-operator quantifier} is called quantifier predicate Expression Comparison-operator Quantifier Subquery quantity > ANY (... ) The whole quantifier predicate will be applied to each row of subquery result in return. Logical expression is true if and only if one or more rows in the subquery result satisfy the comparison. It is false if and only if absolutely none of the subquery result rows satisfy the comparison. Junping Sun Database Systems 4-26 Page 13
14 Quantifier ALL Quantifier ALL: The ALL predicates evaluates to true if and only if a comparison between a single value and the set of values retrieved by the subquery is true for all values retrieved by the subquery. Query: List the names of employees whose salary is greater than the salary of all the employees in department 5. lname, fname employee where salary > all ( salary employee where dno = 5); Predicate ANY, SOME, and ALL could be prefixed with any comparison operators such as { =, t!d z } z can be expressed by <> or!= in the sql condition expression. Junping Sun Database Systems 4-27 Discussions on Predicates IN and NOT IN The predicate a IN (x, y, z) is equivalent to a = x OR a = y OR a = z essn works_on where pno = 1 or pno = 2 or pno = 3; The predicate a NOT IN (x, y, z) is equivalent to a <> x AND a <> y AND a<> z a NOT IN (x, y, z) is equivalent to a <> ALL (x, y, z) essn works_on where pno <> and pno <> 2 and pno <> 3; The predicate a <> ANY/SOME (x, y, z) is equivalent to (a <> x) or (a <> y) or (a <> z). Junping Sun Database Systems 4-28 Page 14
15 Nested Query (Type-N) Q4A. Make a list of all project names for projects that involve an employee whose last name is Smith as a worker, or as a manager of the department that controls the project. distinct pname project where pnumber in ( pnumber project, department, employee where lname = Smith and dnum = dnumber and mgrssn =ssn) or pnumber in ( pno works_on, employee where lname = Smith and essn = ssn); The comparison operator IN compares a value V (here V is pnumber) with a set of (or multiset) of values V and evaluates to TRUE if V is one of the elements in V. Junping Sun Database Systems 4-29 Subquery 1: Decomposition of Nested Query temp1: where pnumber project, department, employee dnum = dnumber and mgrssn =ssn and lname ='Smith' Subquery 2: temp2: where pno workson, employee essn = ssn and lname = 'Smith' Subquery 3: where distinct pnumber project pnumber = temp1.pno o r pnumber = temp2.pno Junping Sun Database Systems 4-30 Page 15
16 Comparison Nested and Flatten Queries Query: Retrieve the social security numbers of employees who work on some projects controlled by department 5. distinct essn works_on where pno = ( pnumber project where dnum = 5); Equivalent Query: essn works_on, project where dnum = 5 and pno = pnumber ; The first implementation by using subquery can avoid join operation. The second implementation has to use join operation where pno = pnumber is the join condition or join path. Junping Sun Database Systems 4-31 Correlated Nested Query (Type-J) Q12. Retrieve the name of each employee who has a dependent with the same first name and same sex as the employee. e.fname, e.lname employee e where e.ssn in ( essn dependent where essn = e.ssn and sex = e.sex and e.fname = dependent_name); The where clause of inner query block contains join predicates that references the table of an outer query block (and the table is not included in the clause of the inner query block). essn = e.ssn correlates the current dependent tuple with the corresponding employee the dependent belongs to. sex = e.sex and e.fname = dependent_name checks the equivalence of sex and fname values between employee and dependent tuples. Junping Sun Database Systems 4-32 Page 16
17 Rule for Subqueries and Nested Queries 1. The subquery should be enclosed within parentheses. 2. Subqueries may contain nested subqueries. When subqueries are nested, SQL evaluates them the inside out. a. The innermost query is processed first b. Then the result of query is passed to the next outer query. 3. In general, we might have several levels of nested queries, the ambiguity among attribute names will be possible if attributes of the same name exist, one in a relation in the -clause of the outer query, and the other in a relation in the -clause of the nested query (inner query). The rule is that a reference to an unqualified attribute refers to the relation declared in the innermost nested query. 4. Column name in a subquery are implicitly qualified by the table name in the FROM clause of the subquery (that is the FROM clause at the same level). 5. A subquery may refer only to column names tables which are named in outer queries or in subquery s own FROM clause. A subquery may not access tables which are used only by a child query. 6. When a subquery is one of the two operands involved in a comparison, the subquery must be written as the second operand. Junping Sun Database Systems 4-33 Query with Exists Function Q12B: Retrieve the name of employee who has a dependent with the same first name and same sex as the employee. e.fname, e.lname employee e where exists ( * dependent where essn = e.ssn and sex = e.sex and e.fname = dependent_name); Junping Sun Database Systems 4-34 Page 17
18 The Exists Function in SQL exists and not exists in SQL is used to check whether the result of a correlated query is empty. exists and not exists in SQL are usually used in conjunction with a correlated nested query. In the example 12, the nest query within the exists function references the ssn, fname, and sex attributes of employee relation the outer query. For each employee tuple, evaluate the nested query, which retrieves all dependent tuples with the same social security number ssn, sex and name as the employee tuple. if at least one tuple exists in the results of the nested query, then that employee tuple. In general, exists(q) returns TRUE if there is at least one tuple in the result of query Q and returns FALSE otherwise. not exists(q) returns TRUE if there are no tuples in the result of query Q and returns FALSE otherwise. Junping Sun Database Systems 4-35 Query with Not Exists Function Q6: Retrieve the names of employees who have no dependents. fname, lname employee where not exists ( * dependent where ssn = essn); The correlated nested query retrieves all dependent tuples related to an employee tuple, if none exist, the employee tuple is ed. For each employee tuple, the nested query s all dependent tuples whose essn value matches the employee ssn. If the result of the nested query is empty then no dependents are related to the employee, so that employee tuple is ed and its fname and lname are retrieved. This is the implementation of difference operation. Junping Sun Database Systems 4-36 Page 18
19 Nested Query with Two Exists Function Q7. List the names of managers who have at least one dependent. fname, lname employee where exists ( * dependent where ssn = essn) and exists ( * department where ssn = mgrssn); the first nested query s all dependent tuple related to an employee the second nested query s all department tuples managed by the employee tuple. if at least one of the fist one and at least one of the second exist with the same ssn, the employee tuple is ed and the fname and lname are retrieved. this is the implementation of intersection operation. Junping Sun Database Systems 4-37 Query with Division (use contains) Q3. Retrieve the name of each employee who works on all the projects controlled by department 5. fname, lname employee where (( pno works_on where ssn = essn) contains ( pnumber project where dnum = 5)); the second nested query which is not correlated to the outer query retrieves the project numbers of all projects controlled by department 5. for each employee tuple, the first nested query, which is correlated, retrieves the project numbers on which the employee works; if these contain all projects controlled by department 5, the employee tuples is ed and the name of that tuple is retrieved. ANSI-SQL and most SQL engine do not support the contains operator. Junping Sun Database Systems 4-38 Page 19
20 Query with Division Q3: Retrieve the name of each employee who works on all the projects controlled by department 5. fname, lname employee e where not exists ( ( pnumber project where dnum = 5) minus ( pno workson w where e.ssn = w.essn) ) Junping Sun Database Systems 4-39 Query with Division Q3: Retrieve the name of each employee who works on all the projects controlled by department 5. fname, lname employee where not exists ( * workson b where (b.pno in ( pnumber project where dnum = 5)) and not exists ( * workson c where c.essn = ssn and c.pno = b.pno)); Junping Sun Database Systems 4-40 Page 20
21 Discussion The outer nested query s any works_on (b) tuples whose pno is of a project controlled by department 5 and there is not a works_on (c) with the same pno and the same ssn as that of the employee tuple under consideration in the outer query. if no such tuple exists, we the employee tuple, and retrieve the fname and lname of that employee tuple. the equivalent interpretation of the query script is as follows: there does not exist a project controlled by department 5 that the employee does not work on. equivalently, each employee who works on all the projects controlled by department 5. Junping Sun Database Systems 4-41 Renaming Attributes and Join Tables Q8a: Retrieve the last name of each employee and his or her supervisor, while renaming the resulting attribute names as employee_name and supervisor_name. where e.lname as employee_name, s.lname as supervisor_name employee as e, employee as s e.superssn = s.ssn; Q1a: Retrieve the names of the employees who work for Research department. where fname, lname, address (employee join department on dno = dnumber) dname = Research ; The concept of a joined table is only supported in ANSI-SQL92. Junping Sun Database Systems 4-42 Page 21
22 Natural Join, Outer Join, and Nested Join Q1b: fname, lname, address (employee natural join (department as dept(dname, dno, mssn, msdate) where dname = Research ; Q8b: Retrieve the last names of all employees and his or her supervisor if these employees have a supervisor. e.lname as employee_name, s.lname as supervisor_name (employee e left outer join employee s on e.superssn = s.ssn); Q2A: pnumber, dnum, lname, address, bdate ((project join department on dnum = dnumber) join employee on mgrssn = ssn) where plocation = Stafford ; Junping Sun Database Systems 4-43 Outer Join in ORACLE Q8b: Retrieve the last names of all employees and his or her supervisor if these employees have a supervisor. e.lname as employee_name, s.lname as supervisor_name employee e, employee s where e.superssn = s.ssn (+); This is equivalent to that the employee table as the role of employee left outer joins the employee table as the role of supervisor. Q8c: Retrieve the last names of all employees and his or her supervisees if these employees have a supervisee. s.lname as employee_name, e.lname as supervisor_name employee s, employee e where s.ssn = e.superssn (+); This is equivalent to that the employee as the role of supervisor left outer joins the employee table as the role of supervisee. Junping Sun Database Systems 4-44 Page 22
23 Aggregation Functions Aggregate Functions: It takes an entire column as an argument and compute a single value based on the contents of the column. The function result is an aggregate of the individual data values in the rows of the column. Q15 : Find the total number of employees in the company, the sum of the salaries of all employees, the maximum, the minimum, and the average salary. count(*), sum(salary), max(salary), min(salary), avg(salary) employee; count(*) is applied to count the total number of tuple employee tuple. sum(), max(), min(), and avg() functions is applied to salary column value of the tuples in employee table. Junping Sun Database Systems 4-45 Q16 : Find the total number of employees of the Research department, as well as the summation of the salaries, the maximum salary, the minimum salary, and the average salary in this department. where count(*), sum(salary), max(salary), min(salary), avg(salary) employee dno = dnumber and dname = Research ; all the aggregation functions, count(), sum(), max(), min(), and avg() are applied to these employee tuples Research department. the constraints dno = dnumber and dname = Research in where clause are evaluated first before aggregate functions are evaluated. Q19: Count the number of distinct salary values in the database. count (distinct salary) employee; Junping Sun Database Systems 4-46 Page 23
24 Q5: Retrieve the names of all employees who have two or more dependents Incorrect one: lname, fname employee where ( count(*) dependent where ssn = essn ) >= 2; when a subquery is one of the two operands involved in a comparison, the subquery must be written as the second operand. Correct one: lname, fname employee where 2 <= ( count(*) dependent where ssn = essn ); Junping Sun Database Systems 4-47 Group By Clause In many cases, we want to apply aggregate functions to subgroups of tuples in a relation based on some attribute values. Example: Find the average salary of employees in each department find the number of employees who work on each project. In these cases, we want to group the tuples have the same value of some attribute(s), called the grouping attribute(s), and apply the function to each such group independently. SQL has a group by clause for this purpose. The group by clause specifies the grouping attributes, which must also appear in the clause, so that the value of applying each function on the group of tuples appears along with the value of the grouping attribute(s). Junping Sun Database Systems 4-48 Page 24
25 Group by Clause Q20: For each department, retrieve the department number, the number of employees in the department, and their average salary. dno, count(*), avg(salary) employee group by dno; Q21: For each project, retrieve the project number, the project name, and number of employees who work on that project. pnumber, pname, count(*) project, works_on where pnumber = pno group by pnumber, pname; the grouping and aggregate functions are applied after the joining of the two relations. Junping Sun Database Systems 4-49 Having Clause Q22. For each project on which more than two employees work, retrieve the project number, project name, and number of employees work on that project. pnumber, pname, count(*) project, workson where pnumber = pno group by pnumber, pname having count(*) > 2; SQL provides a having clause, which can appear only in conjunction with group by clause having provides a condition on the group of tuples associated with each value of the grouping attributes, and only the groups that satisfy the condition are retrieved in the result of the query. ion condition in the where clause limits the tuples to which group function are applied. the having clause limits the whole groups. Junping Sun Database Systems 4-50 Page 25
26 Q23. For each project, retrieve the project number, project name, and number of employee department 5 who works on that project pnumber, pname, count(*) project, workson, employee where pnumber = pno and ssn = essn and dno = 5 group by pnumber, pname; Q5. Retrieve the name s of all employees who have two or more dependents. lname, fname employee where ssn in ( essn dependent where ssn = essn group by essn having count (essn) >= 2); Junping Sun Database Systems 4-51 Where Condition before Having Q24. Count the total number of employees with salaries greater than $40,000 who work in each department, but only these department with more than five employees. dname, count(*) department, employee where dnumber = dno and salary > group by dname having count(*) > 5; this is not the correct query statement. ion condition (salary > 40000) has eliminated these employee tuples whose salary <= before the group by and having clauses. it will only departments that have more than five employees who each earns more than $40,000. the rule is that the where clause is executed first to individual tuples; the having clause is applied later to individual groups of tuples. the tuples are already restricted to employees earning more than $40,000 before the function in the having clause is applied. Junping Sun Database Systems 4-52 Page 26
27 The correct one: where group by dname, count(*) department, employee dnumber = dno and salary > and dno in ( dno employee group by dno having count(*) > 5) dname; the constraints dnumber = dno and salary > in where clause join the department tuples with employee tuples whose salary is greater than the subquery which includesfive employees work. Junping Sun Database Systems 4-53 Having Clause HAVING clause is designed for use in conjunction with GROUP BY when it is desired to restrict the groups which appears in the final result. HAVING conditions often involve aggregation functions, permitting the filtering of groups based on summary calculations. Aggregation functions may not be used within a WHERE clause. WHERE clause filters individual rows going to the final result or intermediate result. HAVING filters groups going into the final result. WHERE and HAVING may be used together cooperatively: WHERE is applied first to filter single rows, then group are formed the rows which remain, then finally the HAVING clause is applied to filter the groups. Generally, the HAVING clause immediately follows the GROUP BY clause. Junping Sun Database Systems 4-54 Page 27
28 Summary of GROUP BY/HAVING Clauses 1. Attribute names or column names not listed in the GROUP BY clause may not appear in the HAVING condition in ANSI-1989 and ANSI-1992 SQL. 2. Aggregation functions may always be used in the HAVING clause, even if they do not appear in the SELECT attribute list. 3. The HAVING condition can involve compound conditions formed by combining simple logical expressions with the logical operators AND, OR, and NOT. 4. HAVING and WHERE can work together. HAVING condition is always applied to GROUP BY Clause. WHERE condition is always applied to attributes involved in ion or join. 5. Non-aggregation expression may be used in the HAVING clause, providing the expressions involve only columns which are named in the GROUP BY clause. Junping Sun Database Systems 4-55 Syntax Structure of SELECT Statements SELECT FROM [WHERE [GROUP BY [HAVING [ORDER BY <attribute list> <table list> <condition>] <grouping attribute(s)>] <grouping condition>] <attribute list>] SELECT clause lists the attributes or functions to be retrieved. FROM clause specifies all relations needed in the query but not those in nested query. WHERE clause specifies the conditions for ion of tuples these relations. GROUP BY specifies grouping attribute(s), whereas HAVING clause specifies a condition on the groups being ed rather than on the individual tuples. The built in aggregation functions COUNT, SUM, MIN, MAX, and AVG are used in conjunction with grouping. ORDER specifies an order Junping Sun Database Systems 4-56 Page 28
29 Sequence 1. FROM: The FROM clause is processed first. It specifies the table(s) or views which serve as the source of all data for the final result. If multiple tables are involved, the join operation is necessary. 2. WHERE: The WHERE clause is processed second. It eliminates those rows defined in FROM clause which do not satisfy the search condition. 3. GROUP BY: The GROUP BY clause groups the remaining rows on the basis of shared values in the GROUP BY column(s). The partial result now has the form of a set of groups. 4. HAVING: The HAVING clause is now applied to eliminate those groups which do not satisfy the HAVING condition. 5. SELECT: The SELECT list is used to remove unwanted columns or attributes the partial result. Only elements which appear in the SELECT list remain. 6. ORDER BY: The final result in the order based on ORDER BY list. Junping Sun Database Systems 4-57 Insert Statement: Insert Statement in SQL Insert a new tuple into employee table: insert into values employee ( Richard, K, Marini, , 30-DEC-52, 98 Oak Forest, Katy, TX', 'M', 37000, ' ', 4); insert into employee(fname, lname, ssn) values ( Richard, Marimi, ); Attributes that are not specified in the insert statement are set to their DEFAULT or to NULL if the attributes are defined with DEFAULT or NULL. The insert operation will be rejected if NOT NULL has been specified for those attributes. Junping Sun Database Systems 4-58 Page 29
30 Insert a set of tuples into a table: create a relation and load it with result of a query. create table depts_info (deptname vchar(15), noofemps integer, totalsal integer); insert into where group by depts_info (deptname, noofemps, totalsal) dname, count(*), sum(salary) department, employee dnumber = dno dname; Junping Sun Database Systems 4-59 Delete Statement in SQL Delete a tuple: to delete the employee tuple with lname Brown delete employee where lname = Brown ; Delete a set of tuples: to delete the employee tuples Research department delete employee where dno in ( dnumber department where dname = Research ); To delete all the tuples in employee table: delete employee; (this gives an empty table) Junping Sun Database Systems 4-60 Page 30
31 Update a single tuple: Update Statement in SQL to change the location and controlling department number of project number 10 to Bellaire and 5. update project set plocation = Bellaire, dnum = 5 where pnumber = 10; Update a set of tuples in a table: to raise the salary of employees Research department by 10%. update employee set salary = salary * 1.1 where dno in ( dnumber department where dname = Research ); Junping Sun Database Systems 4-61 View: Views in SQL It is a single table is derived other tables, these other tables can be base tables or previously defined views. A view does not necessarily exist in physical form, it is considered as a virtual table in contrast to base tables whose tuples are actually stored in the database. Advantages and Disadvantages of View: The advantage is that a frequent query involving with join operations can be represented. Queries involving join operations do not have to do join operations every time by querying the view. The disadvantage is that the possible update operations applied to views are limited. Junping Sun Database Systems 4-62 Page 31
32 Specification of Views in SQL Create a view on fname, lname, pname, hours V1: create view works_on1 as fname, lname, pname, hours employee, project, works_on where ssn = essn and pno = pnumber; works_on1: fname lname pname hours V2: create view dept_info (dept_name, no_of_emps, total_sal) as dname, count(*), sum(salary) department, employee where dnumber = dno group by dname; dept_info dept_name no_of_emps total_sal Junping Sun Database Systems 4-63 Querying on View QV1: To retrieve the last name, first name of all employees who work on ProjectX pname, fname, lname works_on1 where pname = ProductX ; A view is always up to date, if we modify the tuples in the base tables which the view is defined, the view automatically reflects these changes. The view is not realized at the time of view definition but rather at the time we specify a query on the view. It is the responsibility of the DBMS and not the user to make sure that the view is up to date. If the view is no longer useful, then view can be disposed by drop command. V1d: drop view works_on1; V2d: drop view dept_info; Junping Sun Database Systems 4-64 Page 32
33 Single Table View Update: Updating in Views An update on a view defined on a single table can be mapped to an update on the underlying base table. Multi Table View Update: An view involving joins, an update operation may be mapped to update operations on the underlying base relations in multiple ways. Suppose there is a view update the PNAME attribute of John Smith ProductX to ProductY. UV1: update works_on1 set pname = ProductY where lname = smith and fname = john and pname = ProductX this query can be mapped into several updates on the base relations to give the desired update on the view. Junping Sun Database Systems 4-65 There are two possible update (a) and (b) on the base relations corresponding to UV1. (a). update works_on set pno = ( pnumber project where pname ='ProdcutY') where essn = ( ssn employee where lname = 'Smith' and fname ='John') and pno = ( pnumber project where pname ='ProductX') (b). update project set pname = 'ProductY' where pname = 'ProductX' Junping Sun Database Systems 4-66 Page 33
34 Discussion Update (a) relates "John Smith to the Product Y project tuple in place of the Product X, and is the most likely to desired updated. Original update changes the project name pname in works_on1 view, it is unlikely that the update wants to change the PNAME itself, the semantics here is to update the project that John Smith works on. So the update (a) will update the correspondent project number where PNAME = Product Y in works_on base table. Update (b) would also give the desired updated effect on the view, but it accomplishes this by changing the name of of the Product X tuple in the project relation to Product Y. It is quite unlikely that the user who specified the view update UV1 wants to update to be interpreted as in update (b). Junping Sun Database Systems 4-67 Observation A view with a single defining table is updatable if the view attributes contain the primary key or some other candidate key of the base relation, because this maps each (virtual) view tuple to a single base tuple. Views defined on multiple tables using joins are generally not updatable. Views defined using grouping and aggregate function are not updatable. Example: UV2: modify dept_info set total_sal = where dname = Research ; A view update is feasible when only one possible update on the base relations can accomplish the desired update effect on the view. Whenever an update on the view can be mapped to more than one update on the underlying base relations, we must have a certain procedure to choose the desired update. some researchers have developed methods for choosing the most likely update. while other researchers prefer to have the user choose the desired update mapping view definition. Junping Sun Database Systems 4-68 Page 34
35 Specifying Additional Constraints as Assertions To specify the constraint The salary of an employee must not be greater than the salary of the manager of the department that employee works for. create assertion salary_constraint check ( not exists ( * employee e, employee m, department d where e.salary > m.salary and e.dno = d.dnumber and d.mgrssn = m.ssn) ); if tuples in the database cause the condition of an Assertion statement to evaluate to be FALSE, the constraint is violated. Junping Sun Database Systems 4-69 Specifying index on single attribute: I1: create index lname_index on employee (lname ); Specifying Index in SQL Specifying index on multiple attributes: I2: create index names_index on employee (lname asc, fname desc, minit); Specifying index on the attribute with unique value: I3: create unique index ssn_index on employee(ssn); Specifying cluster index: I4: create index dno_index on employee (dno) cluster; Junping Sun Database Systems 4-70 Page 35
36 Cluster in ORACLE create cluster deptandemp (deptemp varchar(9) ); create table department ( dname varchar(19), dnumber varchar(9),... ) cluster deptandemp (dnumber) ; create table employee ( name varchar(19),... dno varchar(9), ) cluster deptandemp (dno) ; Junping Sun Database Systems 4-71 Discussion on Index The reseason and motivation for index is to support efficient search and maintenance. Advantages: Indices support binary search Indices support dynamic maintenance Disadvantages: It costs extra memory space. Algorithms to support indices are more complex. Key work unique can be used to enforce the key constraint. The reason behind linking the definition of a key constraint with specifying an index is that it is much more efficient to enforce uniqueness of key values on a file if an index is defined on the key attribute, since the search on index is much more efficient. A clustering and unique index is similar to primary index. A clustering and non-unique index is similar to cluster index. A nonclustering index is similar to secondary index. Junping Sun Database Systems 4-72 Page 36
Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views
Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views Data Definition, Constraints, and Schema Changes Used to CREATE, DROP, and ALTER the descriptions of the tables (relations) of a database
CSC 443 Data Base Management Systems. Basic SQL
CSC 443 Data Base Management Systems Lecture 6 SQL As A Data Definition Language Basic SQL SQL language Considered one of the major reasons for the commercial success of relational databases SQL Structured
Introduction to SQL: Data Retrieving
Introduction to SQL: Data Retrieving Ruslan Fomkin Databasdesign för Ingenjörer 1056F Structured Query Language (SQL) History: SEQUEL (Structured English QUery Language), earlier 70 s, IBM Research SQL
SQL-99: Schema Definition, Basic Constraints, and Queries
8 SQL-99: Schema Definition, Basic Constraints, and Queries The SQL language may be considered one of the major reasons for the success of relational databases in the commercial world. Because it became
Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems
CSC 74 Database Management Systems Topic #0: SQL Part A: Data Definition Language (DDL) Spring 00 CSC 74: DBMS by Dr. Peng Ning Spring 00 CSC 74: DBMS by Dr. Peng Ning Schema and Catalog Schema A collection
Summary on Chapter 4 Basic SQL
Summary on Chapter 4 Basic SQL SQL Features Basic SQL DDL o Includes the CREATE statements o Has a comprehensive set of SQL data types o Can specify key, referential integrity, and other constraints Basic
SQL Nested & Complex Queries. CS 377: Database Systems
SQL Nested & Complex Queries CS 377: Database Systems Recap: Basic SQL Retrieval Query A SQL query can consist of several clauses, but only SELECT and FROM are mandatory SELECT FROM
How To Create A Table In Sql 2.5.2.2 (Ahem)
Database Systems Unit 5 Database Implementation: SQL Data Definition Language Learning Goals In this unit you will learn how to transfer a logical data model into a physical database, how to extend or
Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Outline More Complex SQL Retrieval Queries
Lab Assignment 0. 1. Creating a Relational Database Schema from ER Diagram, Populating the Database and Querying over the database with SQL
SS Chung Lab Assignment 0 1. Creating a Relational Database Schema from ER Diagram, Populating the Database and Querying over the database with SQL 1. Creating the COMPANY database schema using SQL (DDL)
CS 338 Join, Aggregate and Group SQL Queries
CS 338 Join, Aggregate and Group SQL Queries Bojana Bislimovska Winter 2016 Outline SQL joins Aggregate functions in SQL Grouping in SQL HAVING clause SQL Joins Specifies a table resulting from a join
Relational Schema. CS 4700/6700 A Sample of Small Database Design Using Microsoft Access
CS 4700/6700 A Sample of Small Database Design Using Microsoft Access Company relational database schema from the textbook (Fundamentals of Database systems, 6 th Edition, by Ramez Elmasri and Shamkant
More SQL: Assertions, Views, and Programming Techniques
9 More SQL: Assertions, Views, and Programming Techniques In the previous chapter, we described several aspects of the SQL language, the standard for relational databases. We described the SQL statements
The Relational Algebra
The Relational Algebra Relational set operators: The data in relational tables are of limited value unless the data can be manipulated to generate useful information. Relational Algebra defines the theoretical
COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries
COMP 5138 Relational Database Management Systems Week 5 : Basic COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-1 Today s Agenda Overview Basic Queries Joins Queries Aggregate Functions
New York University Computer Science Department Courant Institute of Mathematical Sciences
New York University Computer Science Department Courant Institute of Mathematical Sciences Homework #5 Solutions Course Title: Database Systems Instructor: Jean-Claude Franchitti Course Number: CSCI-GA.2433-001
Structured Query Language (SQL)
Objectives of SQL Structured Query Language (SQL) o Ideally, database language should allow user to: create the database and relation structures; perform insertion, modification, deletion of data from
Information Systems SQL. Nikolaj Popov
Information Systems SQL Nikolaj Popov Research Institute for Symbolic Computation Johannes Kepler University of Linz, Austria [email protected] Outline SQL Table Creation Populating and Modifying
CHAPTER 8: SQL-99: SCHEMA DEFINITION, BASIC CONSTRAINTS, AND QUERIES
Chapter 8: SQL-99: Schema Definition, Basic Constraints, and Queries 1 CHAPTER 8: SQL-99: SCHEMA DEFINITION, BASIC CONSTRAINTS, AND QUERIES Answers to Selected Exercises 8. 7 Consider the database shown
ER & EER to Relational Mapping. Chapter 9 1
ER & EER to Relational Mapping Chapter 9 1 Figure 3.2 ER schema diagram for the company database. Fname Minit Lname Number Name Address N 1 WORKS_FOR Name Locations Sex Salary Ssn Bdate EMPLOYEE NumberOfEmployees
Introduction to Microsoft Jet SQL
Introduction to Microsoft Jet SQL Microsoft Jet SQL is a relational database language based on the SQL 1989 standard of the American Standards Institute (ANSI). Microsoft Jet SQL contains two kinds of
www.gr8ambitionz.com
Data Base Management Systems (DBMS) Study Material (Objective Type questions with Answers) Shared by Akhil Arora Powered by www. your A to Z competitive exam guide Database Objective type questions Q.1
Instant SQL Programming
Instant SQL Programming Joe Celko Wrox Press Ltd. INSTANT Table of Contents Introduction 1 What Can SQL Do for Me? 2 Who Should Use This Book? 2 How To Use This Book 3 What You Should Know 3 Conventions
CIS 631 Database Management Systems Sample Final Exam
CIS 631 Database Management Systems Sample Final Exam 1. (25 points) Match the items from the left column with those in the right and place the letters in the empty slots. k 1. Single-level index files
Oracle 10g PL/SQL Training
Oracle 10g PL/SQL Training Course Number: ORCL PS01 Length: 3 Day(s) Certification Exam This course will help you prepare for the following exams: 1Z0 042 1Z0 043 Course Overview PL/SQL is Oracle's Procedural
Oracle SQL. Course Summary. Duration. Objectives
Oracle SQL Course Summary Identify the major structural components of the Oracle Database 11g Create reports of aggregated data Write SELECT statements that include queries Retrieve row and column data
IT2305 Database Systems I (Compulsory)
Database Systems I (Compulsory) INTRODUCTION This is one of the 4 modules designed for Semester 2 of Bachelor of Information Technology Degree program. CREDITS: 04 LEARNING OUTCOMES On completion of this
VIEWS virtual relation data duplication consistency problems
VIEWS A virtual relation that is defined from other preexisting relations Called the defining relations of the view A view supports multiple user perspectives on the database corresponding to different
IT2304: Database Systems 1 (DBS 1)
: Database Systems 1 (DBS 1) (Compulsory) 1. OUTLINE OF SYLLABUS Topic Minimum number of hours Introduction to DBMS 07 Relational Data Model 03 Data manipulation using Relational Algebra 06 Data manipulation
More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan
More on SQL Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan SELECT A1, A2,, Am FROM R1, R2,, Rn WHERE C1, C2,, Ck Interpreting a Query
Oracle Database: SQL and PL/SQL Fundamentals
Oracle University Contact Us: 1.800.529.0165 Oracle Database: SQL and PL/SQL Fundamentals Duration: 5 Days What you will learn This course is designed to deliver the fundamentals of SQL and PL/SQL along
Chapter 5. SQL: Queries, Constraints, Triggers
Chapter 5 SQL: Queries, Constraints, Triggers 1 Overview: aspects of SQL DML: Data Management Language. Pose queries (Ch. 5) and insert, delete, modify rows (Ch. 3) DDL: Data Definition Language. Creation,
The Structured Query Language. De facto standard used to interact with relational DB management systems Two major branches
CSI 2132 Tutorial 6 The Structured Query Language (SQL) The Structured Query Language De facto standard used to interact with relational DB management systems Two major branches DDL (Data Definition Language)
History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)
Relational Database Languages Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG) Domain relational calculus QBE (used in Access) History of SQL Standards:
2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA www.sybex.com
Essential SQL 2 Essential SQL This bonus chapter is provided with Mastering Delphi 6. It is a basic introduction to SQL to accompany Chapter 14, Client/Server Programming. RDBMS packages are generally
Advance DBMS. Structured Query Language (SQL)
Structured Query Language (SQL) Introduction Commercial database systems use more user friendly language to specify the queries. SQL is the most influential commercially marketed product language. Other
Relational Databases
Relational Databases Jan Chomicki University at Buffalo Jan Chomicki () Relational databases 1 / 18 Relational data model Domain domain: predefined set of atomic values: integers, strings,... every attribute
Database Query 1: SQL Basics
Database Query 1: SQL Basics CIS 3730 Designing and Managing Data J.G. Zheng Fall 2010 1 Overview Using Structured Query Language (SQL) to get the data you want from relational databases Learning basic
Oracle Database: SQL and PL/SQL Fundamentals NEW
Oracle University Contact Us: + 38516306373 Oracle Database: SQL and PL/SQL Fundamentals NEW Duration: 5 Days What you will learn This Oracle Database: SQL and PL/SQL Fundamentals training delivers the
DBMS / Business Intelligence, SQL Server
DBMS / Business Intelligence, SQL Server Orsys, with 30 years of experience, is providing high quality, independant State of the Art seminars and hands-on courses corresponding to the needs of IT professionals.
Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff
D80198GC10 Oracle Database 12c SQL and Fundamentals Summary Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff Level Professional Delivery Method Instructor-led
Question 1. Relational Data Model [17 marks] Question 2. SQL and Relational Algebra [31 marks]
EXAMINATIONS 2005 MID-YEAR COMP 302 Database Systems Time allowed: Instructions: 3 Hours Answer all questions. Make sure that your answers are clear and to the point. Write your answers in the spaces provided.
Lecture 6. SQL, Logical DB Design
Lecture 6 SQL, Logical DB Design Relational Query Languages A major strength of the relational model: supports simple, powerful querying of data. Queries can be written intuitively, and the DBMS is responsible
Relational Database: Additional Operations on Relations; SQL
Relational Database: Additional Operations on Relations; SQL Greg Plaxton Theory in Programming Practice, Fall 2005 Department of Computer Science University of Texas at Austin Overview The course packet
4. SQL. Contents. Example Database. CUSTOMERS(FName, LName, CAddress, Account) PRODUCTS(Prodname, Category) SUPPLIERS(SName, SAddress, Chain)
ECS-165A WQ 11 66 4. SQL Contents Basic Queries in SQL (select statement) Set Operations on Relations Nested Queries Null Values Aggregate Functions and Grouping Data Definition Language Constructs Insert,
Oracle Database 10g: Introduction to SQL
Oracle University Contact Us: 1.800.529.0165 Oracle Database 10g: Introduction to SQL Duration: 5 Days What you will learn This course offers students an introduction to Oracle Database 10g database technology.
Oracle Database 12c: Introduction to SQL Ed 1.1
Oracle University Contact Us: 1.800.529.0165 Oracle Database 12c: Introduction to SQL Ed 1.1 Duration: 5 Days What you will learn This Oracle Database: Introduction to SQL training helps you write subqueries,
A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX
ISSN: 2393-8528 Contents lists available at www.ijicse.in International Journal of Innovative Computer Science & Engineering Volume 3 Issue 2; March-April-2016; Page No. 09-13 A Comparison of Database
Oracle Database: SQL and PL/SQL Fundamentals
Oracle University Contact Us: +966 12 739 894 Oracle Database: SQL and PL/SQL Fundamentals Duration: 5 Days What you will learn This Oracle Database: SQL and PL/SQL Fundamentals training is designed to
{ PreviousEducation ( CollegeName, StartDate, EndDate, { Degree (DegreeName, Month, Year) }, { Transcript (CourseName, Semester, Year, Grade) } ) }
Tutorial 3 Solution Exercise1: Exercise 2: { PreviousEducation ( CollegeName, StartDate, EndDate, { Degree (DegreeName, Month, Year) }, { Transcript (CourseName, Semester, Year, Grade) } ) } Exercise 3:
Chapter 9, More SQL: Assertions, Views, and Programming Techniques
Chapter 9, More SQL: Assertions, Views, and Programming Techniques 9.2 Embedded SQL SQL statements can be embedded in a general purpose programming language, such as C, C++, COBOL,... 9.2.1 Retrieving
Week 4 & 5: SQL. SQL as a Query Language
Week 4 & 5: SQL The SQL Query Language Select Statements Joins, Aggregate and Nested Queries Insertions, Deletions and Updates Assertions, Views, Triggers and Access Control SQL 1 SQL as a Query Language
MOC 20461C: Querying Microsoft SQL Server. Course Overview
MOC 20461C: Querying Microsoft SQL Server Course Overview This course provides students with the knowledge and skills to query Microsoft SQL Server. Students will learn about T-SQL querying, SQL Server
Databases 2011 The Relational Model and SQL
Databases 2011 Christian S. Jensen Computer Science, Aarhus University What is a Database? Main Entry: da ta base Pronunciation: \ˈdā-tə-ˌbās, ˈda- also ˈdä-\ Function: noun Date: circa 1962 : a usually
Programming with SQL
Unit 43: Programming with SQL Learning Outcomes A candidate following a programme of learning leading to this unit will be able to: Create queries to retrieve information from relational databases using
SQL Query Evaluation. Winter 2006-2007 Lecture 23
SQL Query Evaluation Winter 2006-2007 Lecture 23 SQL Query Processing Databases go through three steps: Parse SQL into an execution plan Optimize the execution plan Evaluate the optimized plan Execution
D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:
D61830GC30 for Developers Summary Duration Vendor Audience 5 Days Oracle Database Administrators, Developers, Web Administrators Level Technology Professional Oracle 5.6 Delivery Method Instructor-led
T-SQL STANDARD ELEMENTS
T-SQL STANDARD ELEMENTS SLIDE Overview Types of commands and statement elements Basic SELECT statements Categories of T-SQL statements Data Manipulation Language (DML*) Statements for querying and modifying
RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. [email protected]. Joining Tables
RDBMS Using Oracle Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture Joining Tables Multiple Table Queries Simple Joins Complex Joins Cartesian Joins Outer Joins Multi table Joins Other Multiple
In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina
This Lecture Database Systems Lecture 5 Natasha Alechina The language, the relational model, and E/R diagrams CREATE TABLE Columns Primary Keys Foreign Keys For more information Connolly and Begg chapter
The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1
The Relational Model Ramakrishnan&Gehrke, Chapter 3 CS4320 1 Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. Legacy systems in older models
BCA. Database Management System
BCA IV Sem Database Management System Multiple choice questions 1. A Database Management System (DBMS) is A. Collection of interrelated data B. Collection of programs to access data C. Collection of data
SQL NULL s, Constraints, Triggers
CS145 Lecture Notes #9 SQL NULL s, Constraints, Triggers Example schema: CREATE TABLE Student (SID INTEGER PRIMARY KEY, name CHAR(30), age INTEGER, GPA FLOAT); CREATE TABLE Take (SID INTEGER, CID CHAR(10),
Lab Manual. Database Systems COT-313 & Database Management Systems Lab IT-216
Lab Manual Database Systems COT-313 & Database Management Systems Lab IT-216 Lab Instructions Several practicals / programs? Whether an experiment contains one or several practicals /programs One practical
3.GETTING STARTED WITH ORACLE8i
Oracle For Beginners Page : 1 3.GETTING STARTED WITH ORACLE8i Creating a table Datatypes Displaying table definition using DESCRIBE Inserting rows into a table Selecting rows from a table Editing SQL buffer
UNIT 6. Structured Query Language (SQL) Text: Chapter 5
UNIT 6 Structured Query Language (SQL) Text: Chapter 5 Learning Goals Given a database (a set of tables ) you will be able to express a query in SQL, involving set operators, subqueries and aggregations
Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables
SQL: Queries, Programming, Triggers Chapter 5 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Example Instances We will use these instances of the Sailors and Reserves relations in our
SQL: Queries, Programming, Triggers
SQL: Queries, Programming, Triggers Chapter 5 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 R1 Example Instances We will use these instances of the Sailors and Reserves relations in
ICAB4136B Use structured query language to create database structures and manipulate data
ICAB4136B Use structured query language to create database structures and manipulate data Release: 1 ICAB4136B Use structured query language to create database structures and manipulate data Modification
1 Structured Query Language: Again. 2 Joining Tables
1 Structured Query Language: Again So far we ve only seen the basic features of SQL. More often than not, you can get away with just using the basic SELECT, INSERT, UPDATE, or DELETE statements. Sometimes
Relational Algebra. Query Languages Review. Operators. Select (σ), Project (π), Union ( ), Difference (-), Join: Natural (*) and Theta ( )
Query Languages Review Relational Algebra SQL Set operators Union Intersection Difference Cartesian product Relational Algebra Operators Relational operators Selection Projection Join Division Douglas
6 CHAPTER. Relational Database Management Systems and SQL Chapter Objectives In this chapter you will learn the following:
6 CHAPTER Relational Database Management Systems and SQL Chapter Objectives In this chapter you will learn the following: The history of relational database systems and SQL How the three-level architecture
SQL: Queries, Programming, Triggers
SQL: Queries, Programming, Triggers CSC343 Introduction to Databases - A. Vaisman 1 R1 Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for
A basic create statement for a simple student table would look like the following.
Creating Tables A basic create statement for a simple student table would look like the following. create table Student (SID varchar(10), FirstName varchar(30), LastName varchar(30), EmailAddress varchar(30));
Mini User's Guide for SQL*Plus T. J. Teorey
Mini User's Guide for SQL*Plus T. J. Teorey Table of Contents Oracle/logging-in 1 Nested subqueries 5 SQL create table/naming rules 2 Complex functions 6 Update commands 3 Save a query/perm table 6 Select
The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3
The Relational Model Chapter 3 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase,
Functional Dependency and Normalization for Relational Databases
Functional Dependency and Normalization for Relational Databases Introduction: Relational database design ultimately produces a set of relations. The implicit goals of the design activity are: information
b. Examine the following histories. Draw their serialization graph and identify which of them is serializable given reasons.
SELECTED SOLUTIONS TO THE EVISION EECISES: 1. In the following questions the operations are as follows rn() transaction n reads data item, wn () transaction n writes data item, cn transactions n commits,
SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7
SQL DATA DEFINITION: KEY CONSTRAINTS CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7 Data Definition 2 Covered most of SQL data manipulation operations Continue exploration of SQL
Databases in Engineering / Lab-1 (MS-Access/SQL)
COVER PAGE Databases in Engineering / Lab-1 (MS-Access/SQL) ITU - Geomatics 2014 2015 Fall 1 Table of Contents COVER PAGE... 0 1. INTRODUCTION... 3 1.1 Fundamentals... 3 1.2 How To Create a Database File
DBMS Questions. 3.) For which two constraints are indexes created when the constraint is added?
DBMS Questions 1.) Which type of file is part of the Oracle database? A.) B.) C.) D.) Control file Password file Parameter files Archived log files 2.) Which statements are use to UNLOCK the user? A.)
10CS54: DATABASE MANAGEMENT SYSTEM
CS54: DATABASE MANAGEMENT SYSTEM QUESTION BANK Chapter 1: Introduction to Database Systems Objective: Databases and data base system have become an essential component of everyday life in modern society.
3. Relational Model and Relational Algebra
ECS-165A WQ 11 36 3. Relational Model and Relational Algebra Contents Fundamental Concepts of the Relational Model Integrity Constraints Translation ER schema Relational Database Schema Relational Algebra
There are five fields or columns, with names and types as shown above.
3 THE RELATIONAL MODEL Exercise 3.1 Define the following terms: relation schema, relational database schema, domain, attribute, attribute domain, relation instance, relation cardinality, andrelation degree.
Oracle Database 11g SQL
AO3 - Version: 2 19 June 2016 Oracle Database 11g SQL Oracle Database 11g SQL AO3 - Version: 2 3 days Course Description: This course provides the essential SQL skills that allow developers to write queries
4 Logical Design : RDM Schema Definition with SQL / DDL
4 Logical Design : RDM Schema Definition with SQL / DDL 4.1 SQL history and standards 4.2 SQL/DDL first steps 4.2.1 Basis Schema Definition using SQL / DDL 4.2.2 SQL Data types, domains, user defined types
Programming Database lectures for mathema
Programming Database lectures for mathematics students April 25, 2015 Functions Functions are defined in Postgres with CREATE FUNCTION name(parameter type,...) RETURNS result-type AS $$ function-body $$
Using Temporary Tables to Improve Performance for SQL Data Services
Using Temporary Tables to Improve Performance for SQL Data Services 2014- Informatica Corporation. No part of this document may be reproduced or transmitted in any form, by any means (electronic, photocopying,
SQL. Short introduction
SQL Short introduction 1 Overview SQL, which stands for Structured Query Language, is used to communicate with a database. Through SQL one can create, manipulate, query and delete tables and contents.
SQL Server 2008 Core Skills. Gary Young 2011
SQL Server 2008 Core Skills Gary Young 2011 Confucius I hear and I forget I see and I remember I do and I understand Core Skills Syllabus Theory of relational databases SQL Server tools Getting help Data
A Brief Introduction to MySQL
A Brief Introduction to MySQL by Derek Schuurman Introduction to Databases A database is a structured collection of logically related data. One common type of database is the relational database, a term
SQL Server. 2012 for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach
TRAINING & REFERENCE murach's SQL Server 2012 for developers Bryan Syverson Joel Murach Mike Murach & Associates, Inc. 4340 N. Knoll Ave. Fresno, CA 93722 www.murach.com [email protected] Expanded
Chapter 6: Physical Database Design and Performance. Database Development Process. Physical Design Process. Physical Database Design
Chapter 6: Physical Database Design and Performance Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden Robert C. Nickerson ISYS 464 Spring 2003 Topic 23 Database
Advanced SQL. Lecture 3. Outline. Unions, intersections, differences Subqueries, Aggregations, NULLs Modifying databases, Indexes, Views
Advanced SQL Lecture 3 1 Outline Unions, intersections, differences Subqueries, Aggregations, NULLs Modifying databases, Indexes, Views Reading: Textbook chapters 6.2 and 6.3 from SQL for Nerds : chapter
Database Administration with MySQL
Database Administration with MySQL Suitable For: Database administrators and system administrators who need to manage MySQL based services. Prerequisites: Practical knowledge of SQL Some knowledge of relational
Guide to SQL Programming: SQL:1999 and Oracle Rdb V7.1
Guide to SQL Programming: SQL:1999 and Oracle Rdb V7.1 A feature of Oracle Rdb By Ian Smith Oracle Rdb Relational Technology Group Oracle Corporation 1 Oracle Rdb Journal SQL:1999 and Oracle Rdb V7.1 The
Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL
Chapter 9 Joining Data from Multiple Tables Oracle 10g: SQL Objectives Identify a Cartesian join Create an equality join using the WHERE clause Create an equality join using the JOIN keyword Create a non-equality
Introduction to SQL and database objects
Introduction to SQL and database objects IBM Information Management Cloud Computing Center of Competence IBM Canada Labs 1 2011 IBM Corporation Agenda Overview Database objects SQL introduction The SELECT
Introduction to SQL (3.1-3.4)
CSL 451 Introduction to Database Systems Introduction to SQL (3.1-3.4) Department of Computer Science and Engineering Indian Institute of Technology Ropar Narayanan (CK) Chatapuram Krishnan! Summary Parts
