UNIT – III Form of basic SQL Query Examples of Basic SQL Queries

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Review Session ER and Relational –ER  Relational –Constraints, Weak Entities, Aggregation, ISA Relational Algebra  Relational Calculus –Selections/Projections/Joins/Division.
Database Management System Module 3:. Complex Constraints In this we specify complex integrity constraints included in SQL. It relates to integrity constraints.
Introduction to Database Systems 1 SQL: The Query Language Relation Model : Topic 4.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 Lecture 11: Basic SQL, Integrity constraints
CS 166: Database Management Systems
SQL: Queries, Constraints, Triggers
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5.
SQL.
SQL: The Query Language Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein and etc for some slides.
SQL Part II: Advanced Queries. 421B: Database Systems - SQL Queries II 2 Aggregation q Significant extension of relational algebra q “Count the number.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
CS 405G: Introduction to Database Systems
M ATH IN SQL. 222 A GGREGATION O PERATORS Operators on sets of tuples. Significant extension of relational algebra. SUM ( [DISTINCT] A): the sum of all.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL 2 – The Sequel R&G, Chapter 5 Lecture 10. Administrivia Homework 2 assignment now available –Due a week from Sunday Midterm exam will be evening of.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Database Systems I SQL Queries.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
Introduction to Structured Query Language (SQL)
Rutgers University SQL: Queries, Constraints, Triggers 198:541 Rutgers University.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.
1 SQL: Structured Query Language Chapter 5. 2 SQL and Relational Calculus relationalcalculusAlthough relational algebra is useful in the analysis of query.
Database Systems More SQL Database Design -- More SQL1.
CSC343 – Introduction to Databases - A. Vaisman1 SQL: Queries, Programming, Triggers.
Chapter 5.  Data Manipulation Language (DML): subset of SQL which allows users to create queries and to insert, delete and modify rows.  Data Definition.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
SQL Examples CS3754 Class Note 11 CS3754 Class Note 11, John Shieh,
SQL 2 Introduction Structured Query Language (SQL): the most widely used commercial relational database language Originally.
Unit 5/COMP3300/ SQL: Queries, Programming, Triggers Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
SQL: Queries, Programming, Triggers. Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for.
Structured Query Language (SQL).  A query is a statement requesting the retrieval of information  The portion of a DML that involves information retrieval.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
1 Database Systems ( 資料庫系統 ) October 24, 2005 Lecture #5.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Example Instances R1 S1 S2  We will use these instances of the Sailors and Reserves relations in our.
Introduction to SQL ; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Using SQL as a Query Language COSC 6340.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
CMPT 258 Database Systems SQL Queries (Chapter 5).
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Overview: Features of SQL  Data definition language: used to create, destroy, and modify tables and.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL: The Query Language Part 1 R &G - Chapter 5 The important thing is not to stop questioning. Albert Einstein.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
SQL: The Query Language Part 1 R&G - Chapter 5 1.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
SQL – Part 2.
Basic SQL Queries Go over example queries, like 10 > ALL.
COP Introduction to Database Structures
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
SQL The Query Language R & G - Chapter 5
Database Systems October 14, 2009 Lecture #5.
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
SQL: The Query Language Part 1
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CS4222 Principles of Database System
SQL: Structured Query Language
Presentation transcript:

UNIT – III Form of basic SQL Query Examples of Basic SQL Queries Introduction to Nested Queries Correlated Nested Queries set Comparison operators Aggregative operators NULL values – comparison using NULL values Logical connectivity’s – AND, OR and NOT Impact on SQL constructs Outer joins Disallowing NULL values Complex Integrity Constraints in SQL Triggers Active Data base.

Structured Query Language (SQL)

SELECT [DISTINCT ] select-list A query is a statement requesting the retrieval of information The portion of a DML that involves information retrieval is called a query language SQL is the most widely used commercial relational database language THE FORM OF A BASIC SQL QUERY SELECT [DISTINCT ] select-list FROM from-list WHERE qualification SELECT clause specifies columns to be retained in the result FROM clause specifies a cross-product of tables

Optional WHERE clause specifies selection conditions on the tables mentioned in the FROM clause Example Attribute (or) Field Record (or) Tuple Fig 4.1 Sailors table

FROM Sailors Question: Find the names and ages of all sailors Query: SELECT DISTINCT sname, age FROM Sailors Output: Note: The DISTINCT keyword can be used to return only distinct (different) values

FROM Sailors Question: Find the names and ages of all sailors Query: SELECT sname, age FROM Sailors Output:

FROM Sailors WHERE rating >7 SELECT * FROM Sailors Question: Find all sailors with a rating above 7 Query: SELECT sid, sname, rating, age FROM Sailors WHERE rating >7 (or) SELECT * FROM Sailors WHERE rating>7 Output:

The SQL language has Three parts -Data Definition Language (DDL) -Data Manipulation Language (DML) -Data Control Language(DCL) Data Definition Language (DDL): DDL supports the creation, deletion, and modification of definitions for tables and views Integrity constraints can be defined on tables, either when the table is created or later The DDL also provides commands for specifying access rights or privileges to tables and views It also provide commands for creating and deleting indexes

Data Manipulation Language (DML): DML allows users to pose queries and to insert, delete, and modify rows Data Control Language (DCL): DCL controls a database, including administrative privileges and saving data

Specifies the kind of data that a field stores Data type Specifies the kind of data that a field stores Character data types VARCHAR2 Stores variable-length character data up to a maximum of 4,000 characters Values in different records can have a different number of characters VARCHAR Same as VARCHAR2, but it is in ANSI standard Syntax: Fieldname VARCHAR2(maximum-size)

Fixed-length character data up to a maximum size of 2,000 characters Data values for different records all have the same number of characters DBMS adds trailing blank spaces to the end of the entry to make the entry fill the maximum size value Data longer than maximum size causes an error Syntax: Fieldname CHAR[(maximum-size)] NVARCHAR2 and NCHAR Analogous to VARCHAR2 and CHAR but use Unicode rather than ASCII Used to hold character data in languages other then English

Variable-Length character data up to 2 GB, max. of one per table LONG Variable-Length character data up to 2 GB, max. of one per table Number data types Stores negative, positive, fixed and floating point numbers with precision up to 38 decimal places Syntax: Fieldname NUMBER[(precision , scale)] Integer: Fieldname NUMBER (precision) Fixed Point: Fieldname NUMBER[(precision , scale)] Date and Time data types Date Dates from December 31,4712 BC to December 31,4712 AD Default format DD-MON-YY Default time format HH:MI:SS AM

Similar to DATE but stores fractional seconds Syntax: Fieldname DATE Timestamp Similar to DATE but stores fractional seconds Syntax: Fieldname TIMESTAMP(Fractional-seconds-pre) DDL Commands -CREATE -ALTER -DROP -GRANT -REVOKE -TRUNCATE

Used to add attributes to an existing relation(table) CREATE command Used to create table Syntax: CREATE TABLE table-name (Fieldname1 data_type , Fieldname2 data_type , …. ….. ….) Example CREATE TABLE Sailors ( sid NUMBER(2), sname VARCHAR2(20), rating NUMBER(2)) ALTER command Used to add attributes to an existing relation(table) All rows in the relation(table) are assigned ‘null’ as the value for the new attributes Syntax: ALTER TABLE tablename ADD (fieldname Field_datatype)

MODIFY (fieldname new_field_datatype) Example: ALTER TABLE Sailors ADD (age NUMBER(2)) Modifying an existing field Syntax: ALTER TABLE tablename MODIFY (fieldname new_field_datatype) Example ALTER TABLE Sailors MODIFY (age NUMBER(3,1)) Deleting an existing field DROP COLUMN Fieldname Example: ALTER TABLE Sailors DROP COLUMN age DROP command Used to delete an existing table Syntax: DROP TABLE tablename Example: DROP TABLE Sailors

INSERT INTO Sailors values (22,’Dustin’,7,45.0) DML Commands -INSERT -DELETE -UPDATE -SELECT INSERT command Inserting record into a table Syntax: INSERT INTO table-name VALUES (field1,field2,…) Example: INSERT INTO Sailors values (22,’Dustin’,7,45.0) Inserting a record that has some null attributes Requires identifying the fields that actually get data Syntax: INSERT INTO table-name (field1,field4) VALUES (value1,value2)

Inserting records from another table Syntax: INSERT INTO table_name1 SELECT * FROM table_name2 UPDATE command For modifying attribute values of (some) tuples in a table Syntax: UPDATE tablename SET column1=value1,…, columnn=valuen WHERE condition Example: UPDATE Sailors SET age=34.5 WHERE sid=22 DELETE command Removing specified rows from a table Syntax: DELETE FROM tablename WHERE condition Example: DELETE FROM Sailors WHERE sid=22

(or) Removing all rows from a table Syntax: DELETE FROM tablename Example: DELETE FROM Sailors (or) Syntax: TRUNCATE table Sailors Example: TRUNCATE table Sailors Note: TRUNCATE Removes all rows from a table without backup Fig 4.2 Boats table

Fig 4.3 Reserves table

Used to define range limits SNAME Dustin Lubber Horatio AND, OR and NOT Operators Question: Find the names of sailors who have reserved boat number 103 Query: SELECT sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND R.bid=103 Output: BETWEEN Used to define range limits Question: Find all sailors whose age is in between 45.0 and 63.5 Query: SELECT * FROM Sailors S WHERE S.age BETWEEN 45.0 AND 63.5 SNAME Dustin Lubber Horatio

SID SNAME RATING AGE SID SNAME RATING AGE Output: IN Used to check whether an attribute value matches a value contained within a set of listed values Question: Find all sailors whose age is in the list of values(15.0,33.2,45.7,63.5) Query: SELECT * FROM Sailors S WHERE S.age IN (15.0,33.2,45.7,63.5) SID SNAME RATING AGE 22 Dustin 7 45 31 Lubber 8 55.5 95 Bob 3 63.5 SID SNAME RATING AGE 95 Bob 3 63.5

SID SNAME RATING AGE SID SNAME RATING AGE STRING operators “%” character is used to match any substring “_” character is used to match any character Expresses patterns by using the ‘like’ comparison operator Example1 SELECT * FROM Sailors WHERE sname LIKE '_u%' Output: Example2 SELECT * FROM Sailors WHERE sname LIKE 'A_d_' SID SNAME RATING AGE 22 Dustin 7 45 31 Lubber 8 55.5 58 Rusty 10 35 SID SNAME RATING AGE 32 Andy 8 25.5

<query 1> <set operator> <query 2> SET operators Operations such as union, intersect, minus and exists operate on relations Corresponding to relational-algebra operations U, ∩ and – Relations participating in the operations must be compatible; i.e., must have same set of attributes <query 1> <set operator> <query 2> union returns a table consisting of all rows either appearing in the result of <query1> or in the result of <query 2> intersect returns all rows that appear in both results <query 1> and <query 2> minus returns those rows that appear in the result of <query 1> but not in the result of <query 2>

SELECT * FROM Sailors UNION SELECT * FROM Sailors1 Example (union) Output:

SELECT * FROM Sailors INTERSECT SELECT * FROM Sailors1 Example (intersect) SELECT * FROM Sailors INTERSECT SELECT * FROM Sailors1 Output:

A nested query is a query that has another query embedded within it Example (minus) SELECT * FROM Sailors MINUS SELECT * FROM Sailors1 Output: no rows selected Nested Queries A nested query is a query that has another query embedded within it The embedded query is called a subquery A subquery typically appears within the WHERE clause of a query Subqueries can sometimes appear in the FROM clause or the HAVING clause

SNAME Dustin Lubber Horatio Introduction to Nested Queries Question: Find the names of sailors who have reserved boat 103 Query: SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid=103) Output: Question: Find the names of sailors who have reserved a blue boat Query: SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid IN (SELECT B.bid FROM Boats B WHERE B.color='blue')) SNAME Dustin Lubber Horatio

Output: Correlated Nested Queries In the nested queries, the inner subquery has been completely independent of the outer query In general the inner subquery could depend on the row that is currently being examined in the outer query Question: Find the names of sailors who have reserved boat 103 Query: SELECT S.sname FROM Sailors S WHERE EXISTS ( SELECT * FROM Reserves R WHERE R.bid = 103 AND R.sid = S.sid ) SNAME Dustin Horatio

The EXISTS operator is another set comparison operator, such as IN SNAME Dustin Lubber Horatio Output: The EXISTS operator is another set comparison operator, such as IN It allows us to test whether a set is nonempty. Thus, for each Sailor row S, we test whether the set of Reserves rows R such that R.bid = 103 AND S.sid = R.sid is nonempty. If so, sailor S has reserved boat 103, and we retrieve the name The subquery clearly depends on the current row S and must be re-evaluated for each row in Sailors The occurrence of S in the subquery (in the form of the literal S.sid) is called a correlation, and such queries are called correlated queries.

Greater than or equal to COMPARISION OPERATORS The following operators can be used in ‘WHERE’ clause Example Question: Find sailors whose rating is better than some sailor called Horatio Query: SELECT S1.sname, S1.rating FROM Sailors S1 WHERE S1.rating > ANY ( SELECT S2.rating FROM SYMBOL MEANING = Equal to < Less than <= Less than or equal to > Grater than >= Greater than or equal to <> or != or ^= Not equal to

Sailors S2 WHERE S2.sname='Horatio' ) Output: If there are several sailors called Horatio, this query finds all sailors whose rating is better than that of some sailor called Horatio Question: Find sailors whose rating is better than some sailor called Horatio Query: SELECT S1.sname, S1.rating FROM Sailors S1 WHERE S1.rating > ALL ( SELECT S2.rating FROM SNAME RATING Rusty 10 Zorba Horatio 9 Lubber 8 Andy

Sailors S2 WHERE S2.sname='Horatio' ) Output: AGGREGATE OPERATORS In addition to simply retrieving data, we often want to perform some computation or summarization SQL supports the following aggregate operations, which can be applied on any column, say A, of a relation(table): 1. COUNT ([DISTINCT] A): The number of (unique) values in the A column 2. SUM ([DISTINCT] A): The sum of all (unique) values in the A column 3. AVG ([DISTINCT] A): The average of all (unique) values in the A column SNAME RATING Rusty 10 Zorba

4. MAX (A): The maximum value in the A column 5. MIN (A): The minimum value in the A column Note: not specify DISTINCT in conjunction with MIN or MAX Examples: Question: Find the average age of all sailors Query: SELECT AVG (age) FROM Sailors Output: Question: Find the name and age of the oldest sailor Query: SELECT S1.sname, S1.age FROM Sailors S1 WHERE S1.age = ( SELECT MAX (S2.age) FROM Sailors S2 ) AVG(AGE) 36.9 SNAME AGE Bob 63.5

SELECT [ DISTINCT ] select-list FROM from-list WHERE qualification Question: Count the number of sailors Query: SELECT COUNT (*) FROM Sailors Output: The GROUP BY and HAVING Clauses We have applied aggregate operations to all (qualifying) rows in a relation(table) GROUP BY used to apply aggregate operations to each of a number of groups of rows in a relation HAVING is used to place a condition, which is applied on the groups of rows general form: SELECT [ DISTINCT ] select-list FROM from-list WHERE qualification GROUP BY grouping-list HAVING group-qualification COUNT(*) 10

RATING COUNT(RATING) 1 3 2 7 8 9 10 Examples Question: Find the number of sailors belongs to each rating level Query: SELECT rating, COUNT(rating) FROM Sailors GROUP BY rating Output: RATING COUNT(RATING) 1 3 2 7 8 9 10

Question: Find the age of the youngest sailor for each rating level Query: SELECT rating, MIN (age) FROM Sailors GROUP BY rating Output: RATING MIN(AGE) 1 33 3 25.5 7 35 8 9 10 16

Question: Find the age of the youngest sailor for each rating level, which is greater than 7 Query: SELECT rating, MIN (age) FROM Sailors GROUP BY rating HAVING rating>7 Output: ORDER BY The order by clause is used to sort the tuples in a query result based on the values of some attributes Example Question: display the sailors table in the ascending order of sname RATING MIN(AGE) 8 25.5 9 35 10 16

SID SNAME RATING AGE 32 Andy 8 25.5 85 Art 3 95 Bob 63.5 29 Brutus 1 Query: SELECT * FROM Sailors ORDER BY sname Output: SID SNAME RATING AGE 32 Andy 8 25.5 85 Art 3 95 Bob 63.5 29 Brutus 1 33 22 Dustin 7 45 64 Horatio 35 74 9 31 Lubber 55.5 58 Rusty 10 71 Zorba 16

SID SNAME RATING AGE 71 Zorba 10 16 58 Rusty 35 31 Lubber 8 55.5 64 Question: display the sailors table in the descending order of sname Query: SELECT * FROM Sailors ORDER BY sname DESC Output: SID SNAME RATING AGE 71 Zorba 10 16 58 Rusty 35 31 Lubber 8 55.5 64 Horatio 7 74 9 22 Dustin 45 29 Brutus 1 33 95 Bob 3 63.5 85 Art 25.5 32 Andy

We use null when the column value is either unknown NULL VALUES Thus far, we have assumed that column values in a row are always known. In practice column values can be unknown We use null when the column value is either unknown Example Insert the row (98,Dan,null,39) to represent Dan into sailors table Query: INSERT INTO Sailors VALUES(98,'Dan',null,39) Query: SELECT * FROM Sailors

SID SNAME RATING AGE 22 Dustin 7 45 29 Brutus 1 33 31 Lubber 8 55.5 32 Output: SID SNAME RATING AGE 22 Dustin 7 45 29 Brutus 1 33 31 Lubber 8 55.5 32 Andy 25.5 58 Rusty 10 35 64 Horatio 71 Zorba 16 74 9 85 Art 3 95 Bob 63.5 98 Dan   39

Consider a comparison such as rating = 8 Comparisons Using Null Values Consider a comparison such as rating = 8 If this is applied to the row for Dan, is this condition true or false? Since Dan's rating is unknown, it is reasonable to say that this comparison should evaluate to the value unknown This is the case for the comparisons rating > 8 and rating < 8 as well SQL also provides a special comparison operator IS NULL to test whether a column value is null for example, we can say rating IS NULL, which would evaluate to true on the row representing Dan We can also say rating IS NOT NULL, which would evaluate to false on the row for Dan Example Query: SELECT * FROM sailors WHERE rating IS NULL

what about boolean expressions such as rating = 8 OR age < 40 Output: Logical Connectives AND, OR, and NOT what about boolean expressions such as rating = 8 OR age < 40 rating = 8 AND age < 40? Considering the row for Dan again, because age < 40, the first expression evaluates to true regardless of the value of rating, but what about the second? We can only say unknown The expression NOT unknown is defined to be unknown SID SNAME RATING AGE 98 Dan   39

OR of two arguments evaluates to true if either argument evaluates to true, and to unknown if one argument evaluates to false and the other evaluates to unknown AND of two arguments evaluates to false if either argument evaluates to false, and to unknown if one argument evaluates to unknown and the other evaluates to true or unknown Impact on SQL Constructs In the presence of null values, any row that evaluates to false or to unknown is eliminated Eliminating rows that evaluate to unknown has a subtle but significant impact on queries, especially nested queries involving EXISTS or UNIQUE If we compare two null values using =, the result is unknown! In the context of duplicates, this comparison is implicitly treated as true, which is an anomaly

The arithmetic operations +, - , The arithmetic operations +, - ,* , / and = all return null if one of their arguments is null nulls can cause some unexpected behavior with aggregate operations COUNT(*) handles null values just like other values, that is, they get counted All the other aggregate operations (COUNT, SUM, AVG, MIN, MAX, and variations using DISTINCT) simply discard null values

join operation that rely on null values, called outer joins Consider the join of two tables, say Sailors Reserves In a full outer join, ‘matching rows’ plus ‘Sailors rows without a matching Reserves rows’ (columns inherited from Reserves assigned null values) plus ‘Reserves rows without a matching Sailors rows’ (columns inherited from Sailors assigned null values) appear in the result In a left outer join, ‘matching rows’ plus ‘Sailors rows without a matching Reserves rows’ (columns inherited from Reserves assigned null values) appear in the result

LEFT OUTER JOIN Reserves R ON S.sid=R.sid In a right outer join, ‘matching rows’ plus ‘Reserves rows without a matching Sailors rows’ (columns inherited from Sailors assigned null values) appear in the result Note: In inner join only matching rows appear in the result Example Query: SELECT S.sid,S.sname, R.bid,R.day FROM Sailors S LEFT OUTER JOIN Reserves R ON S.sid=R.sid Disallowing Null Values We can disallow null values by specifying NOT NULL as part of the field definition, for example, sname CHAR(20) NOT NULL The fields in a primary key are not allowed to take on null values There is an implicit NOT NULL constraint for every field listed in a PRIMARY KEY constraint

CHECK conditional-expression COMPLEX INTEGRITY CONSTRAINTS IN SQL Constraints over a Single Table We can specify complex constraints over a single table using table constraints, which have the form CHECK conditional-expression For example, to ensure that rating must be an integer in the range 1 to 10, we could use: CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10),rating INTEGER,age REAL, PRIMARY KEY (sid), CHECK ( rating >= 1 AND rating <= 10 )) To enforce the constraint that Interlake boats cannot be reserved, we could use: CREATE TABLE Reserves ( sid INTEGER,bid INTEGER, day DATE, FOREIGN KEY (sid) REFERENCES Sailors

FOREIGN KEY (bid) REFERENCES Boats CONSTRAINT noInterlakeRes CHECK ( `Interlake' <> ( SELECT B.bname FROM Boats B WHERE B.bid = Reserves.bid ))) When a row is inserted into Reserves or an existing row is modied, the conditional expression in the CHECK constraint is evaluated. If it evaluates to false, the command is rejected Domain Constraints A user can dene a new domain using the CREATE DOMAIN statement, which makes use of CHECK constraints CREATE DOMAIN ratingval INTEGER DEFAULT 0 CHECK ( VALUE >= 1 AND VALUE <= 10 )

Assertions, which are constraints not associated with any one table INTEGER is the base type for the domain ratingval, and every ratingval value must be of this type. Values in ratingval are further restricted by using a CHECK constraint Once a domain is defined, the name of the domain can be used to restrict column values in a table rating ratingval Assertions: ICs over Several Tables Table constraints are associated with a single table, although the conditional expression in the CHECK clause can refer to other tables Table constraints are required to hold only if the associated table is nonempty Assertions, which are constraints not associated with any one table

As an example, suppose that we wish to enforce the constraint that the number of boats plus the number of sailors should be less than 100. (This condition might be required, say, to qualify as a `small' sailing club.) We could try the following table constraint: CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10),rating INTEGER,age REAL,PRIMARY KEY (sid), CHECK ( rating >= 1 AND rating <= 10) CHECK ( ( SELECT COUNT (S.sid) FROM Sailors S ) + ( SELECT COUNT (B.bid) FROM Boats B ) < 100 )) CREATE ASSERTION smallClub + ( SELECT COUNT (B.bid) FROM Boats B) < 100 )

A trigger description contains three parts: TRIGGERS AND ACTIVE DATABASES A trigger is a procedure that is automatically invoked by the DBMS in response to specified changes to the database, and is typically specified by the DBA A database that has a set of associated triggers is called an active database A trigger description contains three parts: Event: A change to the database that activates the trigger Condition: A query or test that is run when the trigger is activated Action: A procedure that is executed when the trigger is activated and its condition is true Examples of Triggers in SQL CREATE TRIGGER init count BEFORE INSERT ON Students /* Event */ DECLARE count INTEGER;

BEGIN /* Action */ count := 0; END CREATE TRIGGER incr count AFTER INSERT ON Students /* Event */ WHEN (new.age < 18) /* Condition; `new' is just-inserted tuple */ FOR EACH ROW BEGIN /* Action */ count := count + 1;

Assignment - 3 1. (a) Consider the following schema given. The primary keys are underlined. Sailors(sailor-id, sailor-name, sailor-rating, sailor-age) Boats(boat-id, boat-name, boat-color) Reserves(sailor-id, boat-id, day) Write the Nested queries in SQL. i. Find the names of sailors who have reserved boat number 120 ii. Find the names of sailors who have reserved a green boat iii. Find the names of sailors who have not reserved a green boat iv. Find the names of sailors with the highest rating (b) Explain the GROUP BY and HAVING clauses. (a) Consider the following scheme for the COMPANY database. The primary keys are underlined. Employee (SSN, Fname, Lname, Birthdate, Address, Salary, Dnumber) Department (Dnumber, Dname, Dlocation) Perform the following operations using SQL. Assume the data: i. Insert a record into employee table ii. Delete an employee with SSN equal to 10. iii. Update the Dnumber of the employee tuple having salary greater than Rs 10,000. iv. Retrieve the name and address of all employees who work for the ”XYZ” department.

3 (a) Explain with an example in SQL i. Unspecified where-clause and use of Asterisk ii. Exist and not exists iii. Explicit sets and NULLS iv. Renaming attributes and joined tables. (b) Why are null values not preferred in a relation? 4 (a) Explain the general syntax of SELECT command (b) What are the steps followed while creating a table in SQL? (a) What are the various salient features of the SQL? (b) Write short notes on: i) Key constraints ii) General constraints 6 (a) What is the role of SQL in a database architecture? (b) What are the notations used in SQL commands? 7 (a) Explain the various types of aggregate functions with suitable examples in SQL (b) Explain the following (i) Key constraints (ii) Foreign key constraints (iii) General constraints

8 a) Consider the following schema: Suppliers(sid, sname, saddress) Parts(pid, pname, color) Catalog(sid, pid, cost) The key fields are underlined. Write the following queries in relational algebra (i) Find the names of suppliers who supply blue part (ii) Find the sids of suppliers who supply every red part (iii) Find the pids of parts that are supplied by at least two different suppliers (iv) Find all the pids of parts supplied by supplier with sid=200 (v) Find the pids of parts supplied by every supplier at less than Rs 500 9 Distinguish between DDL and DML 10 Explain various integrity constraints.