BKM_DATS: Databázové systémy 2. SQL Vlastislav Dohnal BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 2 Contents History of the SQL Query Language Basic Query Structure Additional Basic Operations Set Operations Null Values Aggregate Functions Nested Subqueries Join Expressions Views Modification of the Database Data Definition Language SQL Data Types and Schemas Integrity Constraints BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 3 History IBM Sequel language developed as part of System R project at the IBM San Jose Research Laboratory Renamed to Structured Query Language (SQL) ANSI and ISO standard SQL: SQL-86; SQL-89 SQL-92 SQL:1999 (language name became Y2K compliant!) SQL:2003 SQL:2006 (adds XML support) SQL:2008 SQL:2011 (adds support for temporal databases) Commercial systems offer most, if not all, SQL-99 features plus varying feature sets from later standards and special proprietary features Not all examples here may work on your particular system. BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 4 Basic Query Structure A typical SQL query has the form: select A1, A2, ..., An from r1, r2, ..., rm where C Ai represents an attribute Ri represents a relation C is a condition. The result of an SQL query is a relation. BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 5 The select Clause The select clause lists the attributes desired in the result of a query corresponds to the projection operation of the relational algebra Example: Relation instructor (id, name, dept_name, salary) Find the names of all instructors: select name from instructor NOTE: SQL names are case insensitive (i.e., you may use upper- or lower-case letters.) E.g., Name ≡ NAME ≡ name Some people use upper case wherever we use bold font. BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 6 The select Clause (Cont.) SQL allows duplicates in relations as well as in query results. To force the elimination of duplicates, insert the keyword distinct after select. Find the names of all departments of instructors, and remove duplicates select distinct dept_name from instructor The keyword all specifies that duplicates not to be removed. select all dept_name from instructor It is also an implicit behavior when the keyword all is omitted. BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 7 The select Clause (Cont.) Relation instructor (id, name, dept_name, salary) An asterisk in the select clause denotes “all attributes” select * from instructor The select clause can contain arithmetic expressions Involving the operations: +, –, , and /, Operating on constants or attributes of tuples. Also, function can be used (nullif(), upper(), to_char(), …) The query: select id, name, dept_name, salary/12 from instructor would return a relation that is the same as the instructor relation, except that the value of the attribute salary is divided by 12. BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 8 The where Clause The where clause specifies conditions that the result must satisfy Corresponds to the selection predicate of the relational algebra. To find all instructors in 'Comp. Sci.' department with salary > 80000 select name from instructor where dept_name = 'Comp. Sci.' and salary > 80000 Comparison results can be combined using the logical connectives and, or, not Comparisons can be applied to results of arithmetic expressions. select name from instructor where salary / 12 > 6000 BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 9 The from Clause The from clause lists the relations involved in the query Corresponds to the Cartesian product operation of the relational algebra. Find the Cartesian product instructor  teaches select  from instructor, teaches Generates every possible instructor-teaches pair, with all attributes from both relations. Cartesian product not very useful directly, but useful when combined with a where-clause condition (selection operation in relational algebra). BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 10 Cartesian Product instructor teaches instructor  teaches BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 11 Joins Relations: instructor (id, name, dept_name, salary) course (course_id, title, dept_name) section (sec_id, semestr, year) teaches (id, course_id, sec_id) For all instructors who teach courses, find their names and the course id of the courses they teach. select name, course_id from instructor, teaches where instructor.id = teaches.id Find the course id, title, semester and year of each course offered by the “Comp. Sci.” department select course.course_id, title, semester, year from course, teaches, section where course.course_id = teaches.course_id and teaches.sec_id = section.sec_id and dept_name = 'Comp. Sci.' BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 12 Natural Join Natural join matches tuples with the same values for all common attributes, and retains only one copy of each common column For relations: instructor (id, name, dept_name, salary) teaches (id, course_id, sec_id, semestr, year) select * from instructor natural join teaches; BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 13 Natural Join (Cont.) Danger in natural join: beware of unrelated attributes with same name which get equated incorrectly Relations: instructor (id, name, dept_name, salary) course (course_id, title, dept_name) section (sec_id, semester, year) teaches (id, course_id, sec_id) List the names of instructors along with the titles of courses that they teach. Incorrect version (equates course.dept_name with instructor.dept_name) select name, title from (instructor natural join teaches) natural join course; Correct version select name, title from (instructor natural join teaches), course where teaches.course_id= course.course_id; Another correct version select name, title from (instructor natural join teaches) join course using(course_id); BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 14 The Rename Operation The SQL allows renaming relations and attributes using the as clause: old-name as new-name E.g., select id, name, salary/12 as monthly_salary from instructor Find the names of all instructors who have a salary higher than some instructor in ‘Comp. Sci.’ select distinct T.name from instructor as T, instructor as S where T.salary > S.salary and S.dept_name = ‘Comp. Sci.’ Keyword as is optional and may be omitted in renaming relations instructor as T ≡ instructor T BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 15 Ordering the Display of Tuples List in alphabetic order the names of all instructors select name from instructor order by name We may specify desc for descending order or asc for ascending order, for each attribute. Ascending order is the default. Example: … order by name desc Can sort on multiple attributes Example: … order by dept_name, name or … order by dept_name desc, name asc BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 16 Where Clause Predicates SQL includes a between comparison operator Example: Find the names of all instructors with salary between $90,000 and $100,000 (that is,  $90,000 and  $100,000) select name from instructor where salary between 90000 and 100000 Tuple comparison select name, course_id from instructor, teaches where (instructor.ID, dept_name) = (teaches.ID, ’Biology’); BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 17 String Operations SQL includes a string-matching operator for comparisons on character strings. The operator “like” uses patterns that are described using two special characters: percent (%). The % character matches any substring. underscore (_). The _ character matches any character. Find the names of all instructors whose name includes the substring “dar”. select name from instructor where name like '%dar%' Match the string containing “100 %” … like ‘%100 \%%' escape '\' SQL supports a variety of string operations such as concatenation (using “||”) converting from upper to lower case (and vice versa) functions upper() and lower() finding string length, extracting substrings, etc. BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 18 Null Values It is possible for tuples to have a null value, denoted by null, for some of their attributes null signifies an unknown value or that a value does not exist. The result of any arithmetic expression involving null is null Example: 5 + null returns null The predicate is null can be used to check for null values. Example: Find all instructors whose salary is null. select name from instructor where salary is null BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 19 Null Values and Three-valued Logic Any comparison with null returns null Example: 5 < null or null <> null or null = null Three-valued logic using the truth value null: OR: (null or true) = true (null or false) = null (null or null) = null AND: (true and null) = null (false and null) = false (null and null) = null NOT: (not null) = null Result of where clause predicate is treated as false if it evaluates to null BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 20 Set Operations (union, intersect, except) Relation: teaches (id, course_id, sec_id, semester, year) Find courses that ran in Fall 2009 or in Spring 2010 Find courses that ran in Fall 2009 and in Spring 2010 Find courses that ran in Fall 2009 but not in Spring 2010 (select course_id from teaches where semester = ‘Fall’ and year = 2009) union (select course_id from teaches where semester = ‘Spring’ and year = 2010) (select course_id from teaches where semester = ‘Fall’ and year = 2009) intersect (select course_id from teaches where semester = ‘Spring’ and year = 2010) (select course_id from teaches where semester = ‘Fall’ and year = 2009) except (select course_id from teaches where semester = ‘Spring’ and year = 2010) BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 21 Set Operations Set operations union, intersect, and except Each of the above operations automatically eliminates duplicates To retain all duplicates use the corresponding multiset versions union all, intersect all and except all. Suppose a tuple occurs m times in r and n times in s, then, it occurs: m + n times in r union all s min(m, n) times in r intersect all s max(0, m – n) times in r except all s BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 22 Praktické cvičení Cvičení SQL, první část BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 23 Aggregate Functions These functions operate on the multiset of values of a column of a relation, and return a value avg: average value min: minimum value max: maximum value sum: sum of values count: number of values BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 24 Aggregate Functions (Cont.) Relations: instructor (id, name, dept_name, salary) teaches (id, course_id, sec_id, semestr, year) Find the average salary of instructors in the Computer Science department select avg (salary) from instructor where dept_name= ’Comp. Sci.’; Find the total number of instructors who teach a course in the Spring 2010 semester select count (distinct id) from teaches where semester = ’Spring’ and year = 2010 Find the number of tuples in the course relation select count (*) from course; BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 25 Aggregate Functions – Group By Find the average salary of instructors in each department select dept_name, avg (salary) from instructor group by dept_name; avg BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 26 Aggregation (Cont.) Attributes in select clause outside of aggregate functions must appear in group by list Erroneous query: select dept_name, id, avg (salary) from instructor group by dept_name; BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 27 Aggregate Functions – Having Clause Relations: instructor (id, name, dept_name, salary) Find the names and average salaries of all departments whose average salary is greater than 42,000 Note: predicates in the having clause are applied after the formation of groups whereas predicates in the where clause are applied before forming groups. Note2: so aggregate functions cannot be used in where clause. select dept_name, avg (salary) from instructor group by dept_name having avg (salary) > 42000; BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 28 Null Values and Aggregates Total all salaries select sum (salary ) from instructor Above statement ignores null amounts Result is null if there is no non-null amount All aggregate operations except count(*) ignore tuples with null values on the aggregated attributes What if collection has only null values? count returns 0 all other aggregates return null BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 29 Nested Subqueries SQL provides a mechanism for the nesting of subqueries. A subquery is a select-from-where expression that is nested within another query. A common use of subqueries is to perform tests for set membership, set comparisons, and set cardinality. BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 30 Example Query: set membership Operators: IN NOT IN Relations: teaches (id, course_id, sec_id, semester, year) Find courses offered in Fall 2009 and in Spring 2010 Find courses offered in Fall 2009 but not in Spring 2010 select distinct course_id from section where semester = ’Fall’ and year = 2009 and course_id in (select course_id from section where semester = ’Spring’ and year = 2010); select distinct course_id from section where semester = ’Fall’ and year = 2009 and course_id not in (select course_id from section where semester = ’Spring’ and year = 2010); BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 31 Example Query: set membership (cont.) Relations: instructor (id, name, dept_name, salary) teaches (id, course_id, sec_id, semester, year) takes (id, course_id, sec_id, semester, year) student (id, name) Find the total number of (distinct) students who have taken course sections taught by the instructor with ID 10101 Note: Above query can be written in a much simpler manner. The formulation above is simply to illustrate SQL features. (select course_id, sec_id, semester, year from teaches where teaches.ID = 10101); select count (distinct ID) from takes where (course_id, sec_id, semester, year) in Attribute ID is a reference to student, here. BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 32 Set Comparison Relations: instructor (id, name, dept_name, salary) Find names of instructors with salary greater than that of some (at least one) instructor in the Biology department. Same query using > some clause select name from instructor where salary > some (select salary from instructor where dept name = ’Biology’); select distinct T.name from instructor as T, instructor as S where T.salary > S.salary and S.dept name = ’Biology’; BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 33 Definition of some Clause F some r   t  r such that (F t ) Where can be: < <=, >= > = <>, != 0 5 6 (5 < some ) = true 0 5 0 ) = false 5 0 5(5 != some ) = true (since 0  5) Read: 5 < some tuple in the relation (5 < some ) = true(5 = some (= some)  in However, (!= some)  not in any is an equivalent of some BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 34 Definition of all Clause F all r   t  r (F t) 0 5 6 (5 < all ) = false 6 10 4 ) = true 5 4 6(5 != all ) = true (since 5  4 and 5  6) (5 < all ) = false(5 = all (!= all)  not in However, (= all)  in Set comparison and NULL values BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 35 0 5 null (5 in ) = true 0 5 6 (null in ) = false 0 5 null (2 not in ) = false!!! 0 5 null (null in ) = false!!! 0 5 null (2 in ) = false BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 36 Example Query Relations: instructor (id, name, dept_name, salary) Find the names of instructors whose salary is greater than the salary of all instructors in the Biology department. select name from instructor where salary > all (select salary from instructor where dept name = ’Biology’); BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 37 Test for Empty Relations The exists construct returns the value true if the argument subquery is nonempty. exists ( r )  r  Ø not exists ( r )  r = Ø BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 38 Correlation Variables Relations: section (sec_id, semestr, year) Yet another way of specifying the query “Find all courses taught in both the Fall 2009 semester and in the Spring 2010 semester” select course_id from section as S where semester = ’Fall’ and year = 2009 and exists (select * from section as T where semester = ’Spring’ and year = 2010 and S.course_id = T.course_id); Correlated subquery Correlation name or correlation variable BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 39 Not Exists Relations: student (id, name) takes (id, course_id, sec_id, semester, year) course (course_id, title, dept_name) Find students who have taken all courses offered in the Biology department. Remark that X – Y = Ø  X  Y Note: Cannot write this query using = all and its variants select distinct S.ID, S.name from student as S where not exists ( (select course_id from course where dept_name = ’Biology’) except (select T.course_id from takes as T where S.ID = T.ID)); BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 40 Derived Relations SQL allows a subquery expression to be used in the from clause Find the departments where the average salary is greater than $42,000. Print the average salary too. select dept_name, avg_salary from (select dept_name, avg (salary) as avg_salary from instructor group by dept_name) as dept_avg where avg_salary > 42000; Note that we do not need to use the having clause Another way to write above query select dept_name, avg_salary from (select dept_name, avg (salary) from instructor group by dept_name) as dept_avg (dept_name, avg_salary) where avg_salary > 42000; BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 41 Scalar Subquery Relations: instructor (id, name, dept_name, salary) department (dept_name, building, budget) select dept_name, (select count(*) from instructor where department.dept_name = instructor.dept_name) as num_instructors from department; BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 42 Joined Relations Join operations take two relations and return as a result another relation. A join operation is a Cartesian product which requires that tuples in the two relations match (under some condition). It also specifies the attributes that are present in the result of the join. The join operations are typically used as sub-query expressions in the from clause. BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 43 Outer Join An extension of the join operation that avoids loss of information. Computes the join and then adds tuples form one relation that does not match tuples in the other relation to the result of the join. Uses null values. BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 44 Left Outer Join course prereq course natural left outer join prereq prereq_id BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 45 Right Outer Join course prereq course natural right outer join prereq prereq_id BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 46 Full Outer Join course prereq course natural full outer join prereq prereq_id BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 47 Joined Relations Join operations take two relations and return as a result another relation. These additional operations are typically used as subquery expressions in the from clause Join condition – defines which tuples in the two relations match, and what attributes are present in the result of the join. Join type – defines how tuples in each relation that do not match any tuple in the other relation (based on the join condition) are treated. Join type inner join left outer join right outer join full outer join Join condition Usage natural r1 natural r2 on r1 r2 on using (A1,A2,…An) r1 r2 using (A1,A2,…,An) BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 48 Joined Relations – Examples course inner join prereq on course.course_id = prereq.course_id course left outer join prereq on course.course_id = prereq.course_id prereq_id prereq_idcourse_id course_id BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 49 Joined Relations – Examples course natural right outer join prereq course full outer join prereq using (course_id) prereq_id prereq_id BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 50 Praktické cvičení Cvičení SQL, druhá část BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 51 Views In some cases, it is not desirable for all users to see the entire logical model (that is, all the actual relations stored in the database.) Consider a person who needs to know an instructor’s name and department, but not the salary. This person should see a relation described, in SQL, by select id, name, dept_name from instructor A view provides a mechanism to hide certain data from the view of certain users. Any relation that is not of the conceptual model but is made visible to a user as a “virtual relation” is called a view. BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 52 View Definition A view is defined using the create view statement which has the form create view v as < query expression > where is any legal SQL expression. The view name is represented by v. Once a view is defined, the view name can be used to refer to the virtual relation that the view generates. View definition is not the same as creating a new relation by evaluating the query expression Rather, a view definition causes the saving of an expression; the expression is substituted into queries using the view. BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 53 Example Views A view of instructors without their salary create view faculty as select ID, name, dept_name from instructor Find all instructors in the Biology department select name from faculty where dept_name = ‘Biology’ Create a view of department salary totals create view departments_total_salary(dept_name, total_salary) as select dept_name, sum (salary) from instructor group by dept_name; BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 54 Views Defined Using Other Views create view physics_fall_2009 as select course.course_id, sec_id, building, room_number from course, section where course.course_id = section.course_id and course.dept_name = ’Physics’ and section.semester = ’Fall’ and section.year = ’2009’; create view physics_fall_2009_watson as select course_id, room_number from physics_fall_2009 where building = ’Watson’; BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 55 View Expansion Expand use of a view in a query/another view create view physics_fall_2009_watson as select course_id, room_number from (select course.course_id, building, room_number from course, section where course.course_id = section.course_id and course.dept_name = ’Physics’ and section.semester = ’Fall’ and section.year = ’2009’) where building = ’Watson’; BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 56 Views Defined Using Other Views One view may be used in the expression defining another view, A view relation v1 is said to depend directly on a view relation v2 if v2 is used in the expression defining v1 A view relation v1 is said to depend on view relation v2 if either v1 depends directly to v2 or there is a path of dependencies from v1 to v2 A view relation v is said to be recursive if it depends on itself. BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 57 View Expansion A way to define the meaning of views defined in terms of other views. Let view v1 be defined by an expression e1 that may itself contain uses of view relations. View expansion of an expression repeats the following replacement step: repeat Find any view relation vi in e1 Replace the view relation vi by the expression defining vi until no more view relations are present in e1 As long as the view definitions are not recursive, this loop will terminate. Recursive views/queries are typically limited to the construct: WITH RECURSIVE myquery (A, B, …) AS ( SELECT A, B, … FROM table WHERE … UNION SELECT A, B, … FROM myquery, table, … ) SELECT * FROM myquery Non-recursive part of query Recursive part BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 58 Modification of the Database – Deletion Relations: instructor (id, name, dept_name, salary) department (dept_name, building, budget) Delete all instructors delete from instructor ; Delete all instructors from the Finance department delete from instructor where dept_name= ’Finance’; Delete all tuples in the instructor relation for those instructors associated with a department located in the Watson building. delete from instructor where dept name in (select dept name from department where building = ’Watson’); BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 59 Example Query Relations: instructor (id, name, dept_name, salary) Delete all instructors whose salary is less than the average salary of instructors Problem: as we delete tuples from instructor, the average salary changes Solution used in SQL: First, compute avg salary and find all tuples to delete Next, delete all tuples found above (without recomputing avg or retesting the tuples) delete from instructor where salary < (select avg (salary) from instructor); BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 60 Modification of the Database – Insertion Relations: course (course_id, title, dept_name, credits) Add a new tuple to course insert into course values (’CS-437’, ’Database Systems’, ’Comp. Sci.’, 4); or equivalently (this is a recommended variant!) insert into course (course_id, title, dept_name, credits) values (’CS-437’, ’Database Systems’, ’Comp. Sci.’, 4); BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 61 Modification of the Database – Insertion Relations: student (id, name, dept_name, tot_credits) Add a new tuple to student with tot_credits set to null insert into student values (’3003’, ’Green’, ’Finance’, null); or equivalently insert into student (id, name, dept_name) values (’3003’, ’Green’, ’Finance’); The value for the unspecified attribute is automatically set to null or the default value assigned to the attribute is used instead. BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 62 Modification of the Database – Insertion Add all instructors to the student relation with tot_credits set to 0 insert into student select ID, name, dept_name, 0 from instructor The select-from-where statement is evaluated fully before any of its results are inserted into the relation Otherwise queries like this would cause problems insert into table1 select * from table1 BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 63 Modification of the Database – Updates Increase salaries of instructors whose salary is over $100,000 by 3%, and all others receive a 5% raise Write two update statements: update instructor set salary = salary * 1.03 where salary > 100000; update instructor set salary = salary * 1.05 where salary <= 100000; The order is important Can be done better using the case statement (next slide) BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 64 Case Statement for Conditional Updates Same query as before but with case statement update instructor set salary = case when salary <= 100000 then salary * 1.05 else salary * 1.03 end BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 65 Updates with Scalar Subqueries Re-compute and update tot_credits value for all students update student set tot_credits = ( select sum(credits) from takes natural join course where student.ID= takes.ID and takes.grade <> ’F’ and takes.grade is not null ); Sets tot_credits to null for students who have not taken any course So, instead of sum(credits), use: case when sum(credits) is not null then sum(credits) else 0 end Or, use the function COALESCE … (select coalesce( sum(credits), 0 ) from … Modification of the Database – Views Modifications of views must be translated to modifications of the actual relations in the database. Consider the view faculty where instructors’ salary is hidden: create view faculty as select ID, name, dept_name from instructor Since we allow a view name to appear wherever a relation name is allowed, the user may write: insert into faculty values (’3003’, ’Green’, ’Finance’); BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 66 Recall: instructor (id, name, dept_name, salary) Modification of the Database – Views (cont.) The previous insertion must be represented by an insertion into the actual relation instructor from which the view faculty is constructed. An insertion into instructor requires a value for salary. The insertion can be dealt with by either rejecting the insertion and returning an error message to the user; or inserting the tuple (’3003’, ’Green’, ’Finance’, null) into the instructor relation. BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 67 BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 68 Praktické cvičení Cvičení SQL, třetí část BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 69 Data Definition Language Allows the specification of not only a set of relations but also information about each relation, including: The schema for each relation. The domain of values associated with each attribute. Integrity constraints The set of indices to be maintained for each relation. Security and authorization information for each relation. The physical storage structure of each relation on disk. BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 70 Create Table Construct An SQL relation is defined using the create table command: create table r (A1 D1, A2 D2, ..., An Dn, integrity-constraint1, ..., integrity-constraintk) r is the name of the relation each Ai is an attribute name in the schema of relation r Di is the data type of values in the domain of attribute Ai Example: create table instructor ( ID char(5), name varchar(20), dept_name varchar(20), salary numeric(8,2), primary key (id) ) insert into instructor values (‘10211’, ’Smith’, ’Biology’, 66000); insert into instructor values (‘10211’, null, ’Biology’, 66000); BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 71 Domain Types in SQL char(n). Fixed length character string, with user-specified length n. varchar(n). Variable length character strings, with user-specified maximum length n. int. Integer (a finite subset of the integers that is machine-dependent). smallint. Small integer (a machine-dependent subset of the integer domain type). numeric(p,d). Fixed point number, with user-specified precision of p digits, with d digits to the right of decimal point. real, double precision. Floating point and double-precision floating point numbers, with machine-dependent precision. float(n). Floating point number, with user-specified precision of at least n digits. BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 72 Domain Types in SQL (cont.) date: Dates, containing a (4 digit) year, month and date Example: date ‘2005-07-27’ time: Time of day, in hours, minutes and seconds. Example: time ‘09:00:30’ time ‘09:00:30.75’ timestamp: date plus time of day Example: timestamp ‘2005-07-27 09:00:30.75’ interval: period of time Example: interval ‘1’ day Subtracting a date/time/timestamp value from another gives an interval value Interval values can be added to date/time/timestamp values BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 73 Integrity Constraints Integrity constraints guard against accidental damage to the database, by ensuring that authorized changes to the database do not result in a loss of data consistency. A checking account must have a balance greater than $10,000.00. A salary of a bank employee must be at least $4.00 an hour. A customer must have a (non-null) phone number. BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 74 Not Null and Unique Constraints not null Declare name and budget to be not null name varchar(20) not null budget numeric(12,2) not null primary key ( A1, A2, …, Am) Attributes A1, A2, … Am forms the relation’s primary key. Equals to unique and not null. unique ( A1, A2, …, Am) The unique specification states that the values in attributes A1, A2, … Am cannot repeat within the relation. BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 75 The Check Constraint check (P) where P is a predicate Example: Ensure that semester is one of fall or spring: create table section ( course_id varchar (8), sec_id varchar (8), semester varchar (6), year numeric (4,0), building varchar (15), room_number varchar (7), time slot id varchar (4), primary key (course_id, sec_id, semester, year), check (semester in (’Fall’, ’Spring’)) ); BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 76 Referential Integrity Ensures that a value that appears in one relation for a given set of attributes also appears for a certain set of attributes in another relation. Example: If “Biology” is a department name appearing in one of the tuples in the instructor relation, then there exists a tuple in the department relation for “Biology”. Let A be a set of attributes. Let R and S be two relations that contain attributes A and where A is the primary key of S. E.g.: S(A,…) R(X, …, A, …) A is said to be a foreign key of R if for any value of A appearing in R it also appears in S. Π 𝐴 𝑅 ⊆ Π 𝐴 𝑆 BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 77 Referential Integrity in Create Table foreign key (Am, ..., An ) references r Example: Declare dept_name as the foreign key referencing department relation create table instructor ( ID char(5), name varchar(20) not null, dept_name varchar(20), salary numeric(8,2), primary key (ID), foreign key (dept_name) references department ); Notice: Schema of department is (dept_name, building). BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 78 Cascading Actions in Referential Integrity create table course ( course_id char(5) primary key, title varchar(20), dept_name varchar(20) references department ) create table course ( … dept_name varchar(20), foreign key (dept_name) references department on delete cascade on update cascade, . . . ) Alternative actions to cascade: set null, set default E.g. … ON DELETE CASCADE SET NULL BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 79 Complex Check Clauses Assume table section(course_id, sec_id, semester, year, time_slot_id, building, room_number) We define this constraint: check (time_slot_id in (select time_slot_id from time_slot)) Why not use a foreign key here? If time_slot_id is not the primary key in time_slot Every section has at least one instructor teaching the section. How to write this? By a subquery… Unfortunately: subquery in check clause not supported by pretty much any database Alternative: triggers BKM_DATS, Vlastislav Dohnal, FI MUNI, 2022 80 Drop and Alter Table Constructs drop table r DROP TABLE instructor; alter table r … alter table r add A D where A is the name of the attribute to be added to relation r and D is the domain of A. All tuples in the relation are assigned null as the value for the new attribute. ALTER TABLE instructor ADD rating CHAR(1); alter table r drop A where A is the name of an attribute of relation r Dropping of attributes not supported by many databases. ALTER TABLE instructor DROP rating;