UNIT 6. Structured Query Language (SQL) Text: Chapter 5

Size: px
Start display at page:

Download "UNIT 6. Structured Query Language (SQL) Text: Chapter 5"

Transcription

1 UNIT 6 Structured Query Language (SQL) Text: Chapter 5

2 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 rewrite SQL queries in one style (with one set of operators) with queries in a different style (using another set of operators) show that two SQL queries (with or without null values) are/aren t equivalent translate RA (or Datalog) queries to SQL queries and vice versa write SQL statements to insert, delete, update the database and define views write SQL statements to set certain constraints (in the project) use JDBC and Java to design DB transactions for the database users Unit 6 2

3 Outline Data Definition Language Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries Modification of the Database Views Integrity Constraints Embedded SQL, JDBC Unit 6 3

4 The SQL Query Language Developed by IBM (System R) in the 1970s Often pronounced as SEQUEL! Need for a standard since relational queries are used by many vendors Standards: SQL-86 SQL-89 (minor revision) SQL-92 (major revision, current standard) SQL-99 (major extensions) Consists of several parts: Data Definition Language (DDL) Data Manipulation Language (DML) o o Data Query Data Modification Unit 6 4

5 Creating Tables in SQL(DDL) A SQL relation schema is defined using the create table command: create table r (A 1 D 1, A 2 D 2,..., A n D n, (integrity-constraint 1 ),..., (integrity-constraint k )) Integrity constraints (ICs) can be: primary and candidate keys foreign keys general assertions o e.g., check (grade between 0 and 100) Example: CREATE TABLE Student CHAR(20) not null, (cid name CHAR(20), address CHAR(20), phone CHAR(8), major CHAR(4), primary key (cid)) Unit 6 5

6 Domain Types in SQL char(n). Fixed length character string with length n. varchar(n). Variable length character strings, with maximum length n. int. Integer (machine-dependent). smallint. Small integer (machine-dependent). 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. Null values are allowed in all the domain types. To preclude null values declare attribute to be not null create domain in SQL-92 and 99 creates user-defined domain types e.g., create domain person-name char(20) not null Unit 6 6

7 Date/Time Types in SQL date. Dates, containing a (4 digit) year, month and date E.g. date time. Time of day, in hours, minutes and seconds. E.g. time 09:00:30 time 09:00:30.75 timestamp: date plus time of day E.g. timestamp :00:30.75 Interval: period of time E.g. 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 Relational DBMS offer a variety of functions to extract values of individual fields from date/time/timestamp convert strings to dates and vice versa For instance in Oracle (date is a timestamp): o TO_CHAR( date, format) o TO_DATE( string, format) o format looks like: DD-Mon-YY HH:MI.SS Unit 6 7

8 Running Examples Customer Database Customer(cid: integer, cname: string, rating: integer, salary: real) Item(iid: integer, iname: string, type: string) Order(cid: integer, iid: integer, day:date, qty:real) Student Database Student (sid, name, address, phone, major) Course (dept, cno, title, credits) Instructor( iname, degree) Section (dept, cno, secno, term, ins_name) Enrolled (sid, dept, cno, secno, term, mark) Prerequisite (dept, cno, pre_dept, pre_cno) Unit 6 8

9 Basic SQL Query SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select [distinct] A 1, A 2,..., A n from r 1, r 2,..., r m where P A i s represent attributes r i s represent relations P is a predicate. This query is nearly equivalent to the relational algebra expression. A1, A2,..., An ( P (r 1 x r 2 x... x r m )) The result of a SQL query is a table (relation). If distinct is used, duplicates are eliminated. By default duplicates are not eliminated! RA s projection RA s selection RA s join is done explicitly in P Dup-elim is expensive. How would you implement it? Unit 6 9

10 Basic SQL Query Called conjunctive query. Equivalent RA expression involves select, project, and join. So, also called SPJ query. SPJ/conjunctive queries correspond in Datalog to: p X r 1 Y,, r k Z, V i op U j,, V l op c, Union of SPJ queries (i.e., SPJU) queries => set of Datalog rules with the same head p X. SPJ with aggregation => SPJA queries; no counterpart in (pure datalog), although researchers have extended Datalog with aggregation (we won t cover this). Unit 6 10

11 Conceptual Evaluation Strategy Typical SQL query: SELECT FROM relation-list WHERE qualification [DISTINCT] attr-list Semantics of a SQL query defined in terms of the following conceptual evaluation strategy (in order): Compute the cross-product of relation-list. Discard any resulting tuples that fail qualifications. Drop attributes that are not in attr-list. If DISTINCT is specified, eliminate duplicate rows. This strategy is not a super efficient way to compute a query! An optimizer will find more efficient strategies to compute the same answers. Unit 6 11

12 Example Instances We will use these instances of the Customer, Item and Order relations in our examples. Customer cid cname rating salary 40 J. Justin G. Grumpy R. Rusty Order cid iid day qty /10/ /12/06 5 Item iid iname type 100 Inspiron6400 laptop 102 LatituteD520 laptop 105 DimensionE520 desktop 110 CanonMP830 printer Unit 6 12

13 Example of Conceptual Evaluation SELECT cname FROM Customer, Order WHERE Customer.cid=Order.cid AND iid=105 Customer X Order (cid) cname rating salary (cid) iid day qty 40 J. Justin /10/ J. Justin /12/ G. Grumpy /10/ G. Grumpy /12/ R. Rusty /10/ R. Rusty /12/06 5 Unit

14 Renaming Attributes in Result SQL allows renaming relations and attributes using the as clause: old-name as new-name Example: Find the name of customers who have ordered item 105 and the day they placed the order; rename cname to customer_name : SELECT cname AS customer_name, day FROM Customer, Order WHERE Customer.cid=Order.cid AND iid=105 Unit 6 14

15 Range Variables We can use variables to name relations in the FROM clause Usually used when same relation appears twice. The previous query can also be written as: OR SELECT cname, day FROM Customer C, Order R WHERE C.cid=R.cid AND iid=105 SELECT C.cname, R.day FROM Customer C, Order R WHERE C.cid=R.cid AND R.iid=105 Nothing but ans N, D customer I, N, R, S, order I, `105, D, Q. Unit 6 15

16 Using DISTINCT Find customers (id s) who ve ordered at least one item : SELECT C.cid FROM Customer C, Order R WHERE C.cid=R.cid Would adding DISTINCT to this query make a difference? Suppose we replace C.cid by C.cname in the SELECT clause. Would adding DISTINCT to this variant of the query make a difference? What if we use * in SELECT (* selects whole tuples)? SELECT * FROM Customer C, Order R WHERE C.cid=R.cid Unit 6 16

17 Expressions and Strings SELECT C.salary, tax=(c.salary-10)*0.3, C.salary*0.01 AS prof_ fees FROM Customer C WHERE C.cname LIKE B_%B Illustrates use of arithmetic expressions and string pattern matching: Returns triples of values, each consisting of the salary the income tax deducted (30% of salary minus 10K) and the professional fees (1% of the salary) for customers whose names begin and end with B and contain at least three characters. Unit 6 17 AS and = are two ways to name fields in result.

18 More on Strings LIKE is used for string matching: `_ stands for any one character and `% stands for 0 or more arbitrary characters. To match the name Strange%, need to use an escape character: like Strange\% escape \ SQL supports a variety of string operations such as concatenation (using ) converting from upper to lower case (and vice versa) finding string length, extracting substrings, etc. Unit

19 Ordering of Tuples List in alphabetic order the names of the customers who have ordered a laptop select cname from Customer, Item, Order where Customer.cid = Order.cid and Item.iid= Order.iid and type= laptop order by cname Order is specified by: Ordering goes beyond RA and Datalog! desc for descending order or asc for ascending order; ascending order is the default. E.g., order by cname desc Unit 6 19

20 Set Operations union, intersect, and except operate on tables (relations) and correspond to the RA operations 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 Unit 6 20

21 Set Operations : UNION What do we get If we replace OR by AND here? Example: Find cid s of customers who ve ordered a laptop or a desktop SELECT C.cid FROM Customer C, Item I, Order R WHERE C.cid=R.cid AND R.iid=I.iid AND (I.type= laptop OR I.type= desktop ) UNION can be used to compute the union of any two compatible (corresponding attributes have same domains) sets of tuples (which are themselves the result of SQL queries): SELECT C.cid FROM Customer C, Item I, Order R WHERE C.cid=R.cid AND R.iid=I.iid AND I.type= laptop UNION SELECT C.cid FROM Customer C, Item I, Order R WHERE C.cid=R.cid AND R.iid=I.iid AND I.type= desktop Unit 6 21

22 Set Operations : EXCEPT EXCEPT can be used to compute the difference of two compatible sets of tuples Some systems use MINUS instead of EXCEPT What does the following query return? SELECT C.cid FROM Customer C, Item I, Order R WHERE C.cid=R.cid AND R.iid=I.iid AND I.type= laptop EXCEPT SELECT C.cid FROM Customer C, Item I, Order R WHERE C.cid=R.cid AND R.iid=I.iid AND I.type= desktop What does EXCEPT remind you of in Datalog? Unit 6 22

23 Set Operations: INTERSECT Example: Find cid s of customers who ve ordered a laptop and a desktop item : SELECT C.cid FROM Customer C, Item I1, Order R1, Item I2, Order R2 WHERE C.cid=R1.cid AND R1.iid=I1.iid AND C.cid=R2.cid AND R2.iid=I2.iid AND I1.type= laptop AND I2.type= desktop ) INTERSECT can be used to compute the intersection of two compatible sets of tuples (included in SQL/92, but some systems may not support it). SELECT C.cid FROM Customer C, Item I, Order R WHERE C.cid=R.cid AND R.iid=I.iid AND I.type= laptop INTERSECT SELECT S.cid FROM Customer S, Item I, Order R WHERE C.cid=R.cid AND R.iid=I.iid AND I.type= desktop Important to include the Key! Unit 6 23

24 Nested Queries Find names of customers who ve ordered item #105: SELECT C.cname FROM Customer C WHERE C.cid IN (SELECT R.cid FROM Order R WHERE iid=105) A very powerful feature of SQL: a WHERE clause can itself contain a SQL query! (Actually, so can FROM and HAVING clauses.) To find customers who ve not ordered item #105, use NOT IN. To understand semantics of nested queries, think of a nested loops evaluation: For each Customer tuple, check the qualification by computing the subquery. Unit

25 Nested Queries with Correlation Find names of customers who ve ordered item #105: SELECT C.cname FROM Customer C WHERE EXISTS (SELECT * FROM Order R WHERE iid=105 AND C.cid=R.cid) EXISTS is another set operator: returns true if the set is not empty. UNIQUE checks for duplicate tuples: returns true if there are no duplicates. If UNIQUE is used above, and * is replaced by iid, finds customers with at most one order for item #105. (* denotes all attributes. Why do we have to replace * by iid?) Ilustrates why, in general, subquery must be re-computed for each Customer tuple. Unit 6 25

26 More on Set-Comparison Operators We ve already seen IN, EXISTS and UNIQUE. Can also use NOT IN, NOT EXISTS and NOT UNIQUE. Also available: op ANY, op ALL, where op is one of: >, <, =, <=, >=, <> Find customers whose salary is greater than that of every customer with last name Rusty : SELECT * FROM Customer C How did we write such queries in RA? How about Datalog? WHERE C.salary > ALL (SELECT C2.salary FROM Customer C2 WHERE C2.cname LIKE % Rusty ) Unit a

27 Rewriting INTERSECT Queries Using IN Find cid s of customers who ve ordered both a laptop and a desktop: SELECT C.cid FROM Customer C, Item I, Order R WHERE C.cid=R.cid AND R.iid=I.iid AND I.type= laptop AND C.cid IN (SELECT C2.cid FROM Customer C2, Item I2, Order R2 WHERE C2.cid=R2.cid AND R2.iid=I2.iid AND I2.type= desktop ) Similarly, EXCEPT queries can be re-written using NOT IN. To find names (not cid s) of customers who ve ordered both laptops and desktops, just replace C.cid by C.cname in SELECT clause. Could we replace cid by cname throughout? Unit (What 6 about INTERSECT query?) 27

28 Division in SQL Find customers who ve ordered all items. Let s do it the hard way without EXCEPT: (1) select customer C such that... there is no item I (2) which is not ordered by C SELECT cname FROM Customer C WHERE NOT EXISTS ((SELECT I.iid FROM Item I) EXCEPT (SELECT R.iid FROM Order R WHERE R.cid=C.cid)) SELECT cname FROM Customer C WHERE NOT EXISTS (SELECT * FROM Item I WHERE NOT EXISTS (SELECT * FROM Order R WHERE R.iid=I.iid AND R.cid=C.cid)) How does it compare with RA/Datalog? Unit

29 Aggregate Operators 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 The following versions eliminate duplicates before apply the operation to attribute A: COUNT ( DISTINCT A) SUM ( DISTINCT A) AVG ( DISTINCT A) Unit 6 29

30 Aggregate Operators: Examples SELECT COUNT (*) FROM Customer SELECT AVG (salary) FROM Customer WHERE rating=10 SELECT cname FROM Customer C WHERE C.rating= (SELECT MAX(C2.rating) FROM Customer C2) SELECT AVG ( DISTINCT salary) FROM Customer WHERE rating=10 SELECT COUNT (DISTINCT rating) FROM Customer WHERE salary BETWEEN 50 AND 100 Unit 6 30

31 Aggregate Operators: Examples(cont) Find name and salary of the richest customer(s) The first query is wrong! WHY? Second query is fine: can use value = subquery only if subquery returns single value. The third query is equivalent to the second query, and is allowed in the SQL/92 standard, but is not supported in some systems. SELECT cname, MAX (salary) FROM Customer SELECT cname, salary FROM Customer WHERE salary = (SELECT MAX (salary) FROM Customer) SELECT cname, salary FROM Customer WHERE (SELECT MAX (salary) FROM Customer) = salary Unit 6 31

32 GROUP BY and HAVING Often, we want to divide tuples into groups and apply aggregate operations to each group. Example: Find the average salary of the customers in each rating level. Suppose we know that rating values go from 1 to 10; we can write 10 queries that look like this (!): Problem: For i = 1, 2,..., 10: SELECT AVG (salary) FROM Customer WHERE rating = i We don t know how many rating levels exist, and what the rating values for these levels are! Solution: Use GROUP BY and/or HAVING clauses Unit 6 32

33 GROUP BY and HAVING (cont) SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list HAVING group-qualification ORDER BY target-list The target-list contains (i) attribute names (ii) terms with aggregate operations (e.g., AVG (salary)). Attributes in (i) must also be in grouping-list. each answer tuple corresponds to a group, a group is a set of tuples that have the same value for all attributes in grouping-list selected attributes in (i) must have a single value per group. Attributes in group-qualification are either in grouping-list or are arguments to an aggregate operator. Unit 6 33

34 Conceptual Evaluation of a Query 1. compute the cross-product of relation-list 2. keep only tuples that satisfy qualification 3. partition selected tuples into groups by the value of attributes in grouping-list 4. keep only the groups that satisfy group-qualification ( expressions in group-qualification must have a single value per group!) 5. keep only the fields that are in target-list 6. generate one answer tuple per qualifying group. Unit 6 34

35 GROUP BY Example Example1: For each item, find the salary of the poorest customer who has ordered this item: SELECT FROM WHERE GROUP BY iid iid, MIN (salary) Customer C, Order R C.cid= R.cid Unit 6 35

36 GROUP BY Example: Default Evaluation Customer cid cname rating salary 40 J. Justin G. Grumpy R. Rusty Order cid iid day qty /10/ /12/ /06/ /06/07 5 C.cid=R.cid (Customer X Order) (cid) cname rating salary (cid) iid day qty 40 J. Justin /10/ J. Justin /06/ G. Grumpy /06/ R. Rusty /12/06 5 Unit 6 36

37 GROUP BY Example (cont ) C.cid=R.cid (Customer X Order) and grouped by iid (cid) cname rating salary (cid) iid day qty 40 J. Justin /10/ J. Justin /06/ G. Grumpy /06/ R. Rusty /12/06 5 Result iid Min(salary) Unit , 5a

38 GROUP BY and HAVING Example Example2: For each item that has more than 2 orders, find the salary of the poorest customer who has ordered this item: SELECT iid, MIN (salary) FROM Customer C, Order R WHERE C.cid= R.cid GROUP BY iid HAVING COUNT (*) > 2 Unit

39 Grouping Examples (contd.) For each laptop item, find the number of (distinct) customers who ordered this item SELECT I.iid, COUNT (DISTINCT R.cid) AS scount FROM Item I, Order R WHERE R.iid=I.iid AND I.type= laptop GROUP BY I.iid Grouping over a join of two relations. What do we get if we: (a) remove I.type= laptop from the WHERE clause, and then (b) add a HAVING clause with this dropped condition? What if we replace COUNT (DISTINCT R.cid) with COUNT (*)? Unit 6 39

40 More Grouping Examples For each rating that has at least 2 customers whose salary is at least 50K, find the average salary of these customers for that rating SELECT rating, AVG (salary) FROM Customer WHERE salary >= 50 GROUP BY rating HAVING COUNT (*) > 1 Only rating can appear alone in the SELECT and/or HAVING clauses. 2nd column of result is unnamed. (Use AS to name it.) Customer cid cname rating salary 22 J. Justin R. Rubber Z. Zorba H. Hasty B. Brutus R. Rusty rating salary rating Answer relation Unit

41 Grouping Examples (cont ) For each rating that has at least 2 customers (of any salary), find the average salary among the customers with that rating whose salary is at least 50K. SELECT C.rating, AVG (C.salary) FROM Customer C WHERE C.salary >= 50 GROUP BY C.rating HAVING 1 < (SELECT COUNT (*) FROM Customer C2 WHERE C2.rating=C.rating) Shows HAVING clause can also contain a subquery. Compare this with the query where we concidered only ratings with at least 2 customers with salary at least 50K! What if HAVING clause is replaced by: HAVING COUNT(*) >1 Unit

42 Grouping Examples (contd.) Find those ratings for which their average salary is the minimum over all ratings SELECT C.rating FROM Customer C WHERE C.salary = (SELECT MIN (AVG (C2.salary)) FROM Customer C2) WRONG! Aggregate operations cannot be nested! Correct solution (in SQL/92 and SQL/99 ) SELECT Temp.rating, Temp.avgsalary FROM (SELECT C.rating, AVG (C.salary) AS avgsalary FROM Customer C GROUP BY C.rating) AS Temp WHERE Temp.avgsalary = (SELECT MIN (Temp.avgsalary) FROM Temp) Unit 6 42

43 Null Values Tuples may have a null value, denoted by null, for some of their attributes Value null signifies an unknown value or that a value does not exist. The predicate IS NULL ( IS NOT NULL ) can be used to check for null values. E.g., Find the names of the customers whose salary is not known. SELECT cname FROM Customer WHERE salary IS NULL The result of any arithmetic expression involving null is null E.g., 5 + null returns null. Unit 6 43

44 Null Values and Three-Valued Logic To deal with null values we need a three-valued logic using the truth value unknown: OR False Unknown True False F U T Unknown U U T T True T T T U Classical Logic is 2-valued. F Lattice of truth values in Kleene s 3-valued logic. Unit 6 44

45 Null Values and Three-Valued Logic To deal with null values we need a three-valued logic using the truth value unknown: AND False Unknown True False F F F Unknown F U U T True F U T U Classical Logic is 2-valued. F Lattice of truth values in Kleene s 3-valued logic. Unit 6 45

46 Null Values and Three-Valued Logic To deal with null values we need a three-valued logic using the truth value unknown: False Unknown True NOT T U F T U Classical Logic is 2-valued. F Lattice of truth values in Kleene s 3-valued logic. Unit 6 46

47 Null Values and Three-Valued Logic In particular, note: OR: (unknown or true) = true, (unknown or false) = unknown (unknown or unknown) = unknown AND: (true and unknown) = unknown, (false and unknown) = false, (unknown and unknown) = unknown NOT: (not unknown) = unknown P is unknown evaluates to true if predicate P evaluates to unknown Any comparison with null returns unknown E.g. 5 < null or null <> null or null = null Result of where clause predicate is treated as false if predicate evaluates to unknown All aggregate operations except count(*) ignore tuples with null values on the aggregated attributes. Unit 6 47

48 Summary of impact of null values Represent unknown or inapplicable. Make arithmetic expressions evaluate to unknown (U). Make logical expressions evaluate to T, U, of F. SQL treats truth value U in where clause as F. SQL ignores tuples with nulls on aggregated attr when aggregating using any function save Count. SQL counts all tuples regardless of presence of nulls. SQL lets you test if a value is null via IS NULL and IS NOT NULL. Unit 6 48

49 Database Modification Insertion Can insert a single tuple using: INSERT INTO Student VALUES (53688, Smith, 222 W.15 th ave, , MATH) or INSERT INTO Student (sid, name, address, phone, major) VALUES (53688, Smith, 222 W.15 th ave, , MATH) Add a tuple to student with null address and phone: INSERT INTO Student (sid, name, address, phone, major) VALUES (33388, Chan, null, null, CPSC) What if there was a not null constraint on address or phone? Unit 6 49

50 Database Modification Insertion (contd.) Can add values selected from another table? Add an order for customer 222 for every laptop item with date 1/1/07 and quantity 5 INSERT INTO Order SELECT 222, iid, 1/1/07, 5 FROM Item WHERE type = laptop Query-driven insert. The select-from-where statement is fully evaluated before any of its results are inserted So, statements like INSERT INTO table1 SELECT... FROM table1 are ok. How would you say add orders for a specific item (iid) from every customer rated at 6 or above? Unit 6 50

51 Database Modification Deletion Note that only whole tuples are deleted. Can delete all tuples satisfying some condition (e.g., name = Smith): DELETE FROM Student WHERE name = Smith Delete all Customers whose salary is above the average sailor salary: DELETE FROM Customer WHERE salary > (SELECT avg(salary) FROM Customer) Do you see any problem with this? Unit 6 51

52 Database Modification Updates Increase the rating of all customers by 2 (should not be more than 10) Need to write two updates: UPDATE Customer SET rating = 10 WHERE rating >= 8 UPDATE Customer SET rating = rating + 2 WHERE rating < 8 Is the order important? How would you raise by 10% the salary of every customer rated at 8 or above? Alternatively, use case statement: UPDATE Customer SET rating = CASE WHEN rating< 8 THEN rating+2 WHEN rating >= 8 THEN 10 END ELSE rating Unit 6 52

53 Views Provide a mechanism to hide certain data from certain users. To create a view we use the command: where: CREATE VIEW vname AS <query expression> <query expression> is any legal SQL expression vname is the view name Example: CREATE VIEW LDOrder AS SELECT cid, iid, type, date FROM Item I, Order R WHERE I.iid = R.iid AND (type = laptop OR type = desktop ) Unit

54 With Clause Allows views to be defined locally to a query, rather than globally. Example: WITH LDOrder(cid, iid, type, date) AS SELECT cid, iid, type, date FROM Item I, Order R WHERE I.iid = R.iid AND (type = laptop OR type = desktop ) SELECT cid, cname FROM Customer C, LDOrder R WHERE C.cid = R.cid AND date > 1/1/07 Unit 6 54

55 SQL s Join Operators 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. Example: Join Types inner join left outer join right outer join full outer join Join Conditions natural on <predicate> SELECT S.name, E.dept, E.cno FROM Student S NATURAL LEFT OUTER JOIN Enrolled E Unit

56 Integrity Constraints (Review) An IC describes conditions that every legal instance of a relation must satisfy. Inserts/deletes/updates that violate IC s are disallowed. Can be used to ensure application semantics (e.g., cid is a key), or prevent inconsistencies (e.g., cname has to be a string, salary must be < 200) Types of IC s: domain constraints, primary key constraints, foreign key constraints, general constraints Unit 6 56

57 General Constraints CREATE TABLE Customer Create with a CHECK clause. cid INTEGER, Constraints can be named cname CHAR(10), Can use subqueries to express constraint rating INTEGER, salary REAL, CREATE TABLE Order1 PRIMARY KEY (cid), ( cid INTEGER, CHECK ( rating >= 1 iid INTEGER, AND rating <= 10 ); day DATE, qty REAL, PRIMARY KEY (cid, iid, day), CHECK (`Printer <> ( SELECT I.type FROM Item I WHERE I.iid=iid))); Check constraints are checked when tuples are inserted or modified Unit 6 57

58 Domain Constraints User can create a new domain and set constrains for it. Example: CREATE DOMAIN agedomain INTEGER DEFAULT 21 CHECK ( VALUE >= 1 AND VALUE <= 110 ) In current systems distinct domains are not distinct types. New systems support distinct types Unit 6 58

59 Constraints Over Multiple Relations Cannot be defined in one table. Are defined as ASSERTIONs which are not associated with any one table. Example: Every student has taken at least one course. CREATE ASSERTION totalenrolment CHECK ( NOT EXISTS ((SELECT sid FROM student) EXCEPT (SELECT sid FROM Enrolled))); What would this IC look like in Datalog-like syntax? Unit 6 59

60 Transactions A transaction is a sequence of queries and update statements executed as a single unit Transactions are started implicitly and terminated by one of o commit work: makes all updates of the transaction permanent in the database o rollback work: undoes all updates performed by the transaction. Example Transfer of money from account A to account B involves two steps: o deduct from A and add to B If one step succeeds and the other fails, database is in an inconsistent state Therefore, either both steps should succeed or neither should If any step of a transaction fails, all work done by the transaction can be undone by rollback work. Rollback of incomplete transactions is done automatically, in case of system failures Unit 6 71

61 Transactions (Contd.) In most database systems, each SQL statement that executes successfully is automatically committed. Each transaction consists of only a single statement Automatic commit can usually be turned off, but how to do so depends on the database system Another option in SQL:1999: enclose statements within begin atomic end Unit 6 72

62 Summary SQL was an important factor in the early acceptance of the relational model; more natural than earlier, procedural query languages. Relationally complete; in fact, significantly more expressive power than relational algebra. Consists of a data definition, data manipulation and query language. Many alternative ways to write a query; optimizer looks for most efficient evaluation plan. Holy Grail: users don t have to care about efficiency, and relegate finding an efficient plan to QOzer. In practice, users need to be aware of how queries are optimized and evaluated for best results. Unit 6 73

63 Summary (Contd.) NULL for unknown field values brings many complications SQL allows specification of rich integrity constraints (and triggers) Embedded SQL allows execution within a host language; cursor mechanism allows retrieval of one record at a time APIs such as ODBC and JDBC introduce a layer of abstraction between application and DBMS Unit 6 74

SQL: Queries, Programming, Triggers

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

More information

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

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

More information

SQL: Queries, Programming, Triggers

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

More information

Chapter 5. SQL: Queries, Constraints, Triggers

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,

More information

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

More information

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

More information

Relational Algebra and SQL

Relational Algebra and SQL Relational Algebra and SQL Johannes Gehrke johannes@cs.cornell.edu http://www.cs.cornell.edu/johannes Slides from Database Management Systems, 3 rd Edition, Ramakrishnan and Gehrke. Database Management

More information

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

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

More information

Oracle Database: SQL and PL/SQL Fundamentals

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

More information

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL. Basic SQL Query. Primary and Candidate Keys in SQL

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL. Basic SQL Query. Primary and Candidate Keys in SQL COS 597A: Principles of Database and Information Systems SQL: Overview and highlights The SQL Query Language Structured Query Language Developed by IBM (system R) in the 1970s Need for a standard since

More information

Oracle SQL. Course Summary. Duration. Objectives

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

More information

Advance DBMS. Structured Query Language (SQL)

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

More information

Oracle Database: SQL and PL/SQL Fundamentals NEW

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

More information

Oracle Database: SQL and PL/SQL Fundamentals

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

More information

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves Tutorial 5: SQL By Chaofa Gao Tables used in this note: Sailors(sid: integer, sname: string, rating: integer, age: real); Boats(bid: integer, bname: string, color: string); Reserves(sid: integer, bid:

More information

Instant SQL Programming

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

More information

Information Systems SQL. Nikolaj Popov

Information Systems SQL. Nikolaj Popov Information Systems SQL Nikolaj Popov Research Institute for Symbolic Computation Johannes Kepler University of Linz, Austria popov@risc.uni-linz.ac.at Outline SQL Table Creation Populating and Modifying

More information

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

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

More information

Chapter 4: SQL. Schema Used in Examples

Chapter 4: SQL. Schema Used in Examples Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations Views Modification of the Database Joined Relations Data Definition Language Embedded SQL,

More information

How To Create A Table In Sql 2.5.2.2 (Ahem)

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

More information

Introduction to SQL: Data Retrieving

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

More information

Oracle Database 11g SQL

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

More information

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

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

More information

Introduction to Microsoft Jet SQL

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

More information

IT2304: Database Systems 1 (DBS 1)

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 information

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

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:

More information

Θεµελίωση Βάσεων εδοµένων

Θεµελίωση Βάσεων εδοµένων Θεµελίωση Βάσεων εδοµένων Βασίλης Βασσάλος 1 What do we need to produce good software? Good programmers, software engineers, project managers, business experts, etc. (People) Good software development

More information

Comp 5311 Database Management Systems. 3. Structured Query Language 1

Comp 5311 Database Management Systems. 3. Structured Query Language 1 Comp 5311 Database Management Systems 3. Structured Query Language 1 1 Aspects of SQL Most common Query Language used in all commercial systems Discussion is based on the SQL92 Standard. Commercial products

More information

IT2305 Database Systems I (Compulsory)

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

More information

Oracle Database 12c: Introduction to SQL Ed 1.1

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,

More information

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

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

More information

EECS 647: Introduction to Database Systems

EECS 647: Introduction to Database Systems EECS 647: Introduction to Database Systems Instructor: Luke Huan Spring 2013 Administrative Take home background survey is due this coming Friday The grader of this course is Ms. Xiaoli Li and her email

More information

The Relational Model. Why Study the Relational Model?

The Relational Model. Why Study the Relational Model? The Relational Model Chapter 3 Instructor: Vladimir Zadorozhny vladimir@sis.pitt.edu Information Science Program School of Information Sciences, University of Pittsburgh 1 Why Study the Relational Model?

More information

Lecture 6. SQL, Logical DB Design

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

More information

Relational Databases

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

More information

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA www.sybex.com

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

More information

A Brief Introduction to MySQL

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

More information

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

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

More information

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

The Relational Model. Why Study the Relational Model? Relational Database: Definitions The Relational Model Database Management Systems, R. Ramakrishnan and J. Gehrke 1 Why Study the Relational Model? Most widely used model. Vendors: IBM, Microsoft, Oracle, Sybase, etc. Legacy systems in

More information

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

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

More information

RNDr. Michal Kopecký, Ph.D. Department of Software Engineering, Faculty of Mathematics and Physics, Charles University in Prague

RNDr. Michal Kopecký, Ph.D. Department of Software Engineering, Faculty of Mathematics and Physics, Charles University in Prague course: Database Applications (NDBI026) WS2015/16 RNDr. Michal Kopecký, Ph.D. Department of Software Engineering, Faculty of Mathematics and Physics, Charles University in Prague student duties final DB

More information

Oracle Database: Introduction to SQL

Oracle Database: Introduction to SQL Oracle University Contact Us: +381 11 2016811 Oracle Database: Introduction to SQL Duration: 5 Days What you will learn Understanding the basic concepts of relational databases ensure refined code by developers.

More information

Netezza SQL Class Outline

Netezza SQL Class Outline Netezza SQL Class Outline CoffingDW education has been customized for every customer for the past 20 years. Our classes can be taught either on site or remotely via the internet. Education Contact: John

More information

Oracle Database: Introduction to SQL

Oracle Database: Introduction to SQL Oracle University Contact Us: 1.800.529.0165 Oracle Database: Introduction to SQL Duration: 5 Days What you will learn View a newer version of this course This Oracle Database: Introduction to SQL training

More information

www.gr8ambitionz.com

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

More information

Oracle Database: Introduction to SQL

Oracle Database: Introduction to SQL Oracle University Contact Us: 1.800.529.0165 Oracle Database: Introduction to SQL Duration: 5 Days What you will learn This Oracle Database: Introduction to SQL training teaches you how to write subqueries,

More information

5.1 Database Schema. 5.1.1 Schema Generation in SQL

5.1 Database Schema. 5.1.1 Schema Generation in SQL 5.1 Database Schema The database schema is the complete model of the structure of the application domain (here: relational schema): relations names of attributes domains of attributes keys additional constraints

More information

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: SQL and PL/SQL Fundamentals NEW Oracle University Contact Us: 001-855-844-3881 & 001-800-514-06-97 Oracle Database: SQL and PL/SQL Fundamentals NEW Duration: 5 Days What you will learn This Oracle Database: SQL and PL/SQL Fundamentals

More information

Programming with SQL

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

More information

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model CS2Bh: Current Technologies Introduction to XML and Relational Databases Spring 2005 The Relational Model CS2 Spring 2005 (LN6) 1 The relational model Proposed by Codd in 1970. It is the dominant data

More information

Oracle Database 10g: Introduction to SQL

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.

More information

Relational Database: Additional Operations on Relations; SQL

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

More information

Oracle 10g PL/SQL Training

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

More information

Databases 2011 The Relational Model and SQL

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

More information

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

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,

More information

SQL NULL s, Constraints, Triggers

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

More information

4. SQL. Contents. Example Database. CUSTOMERS(FName, LName, CAddress, Account) PRODUCTS(Prodname, Category) SUPPLIERS(SName, SAddress, Chain)

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,

More information

BCA. Database Management System

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

More information

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Active database systems. Triggers. Triggers. Active database systems.

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Active database systems. Triggers. Triggers. Active database systems. Active database systems Database Management Systems Traditional DBMS operation is passive Queries and updates are explicitly requested by users The knowledge of processes operating on data is typically

More information

CSC 443 Data Base Management Systems. Basic SQL

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

More information

MOC 20461C: Querying Microsoft SQL Server. Course Overview

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

More information

Microsoft Access 3: Understanding and Creating Queries

Microsoft Access 3: Understanding and Creating Queries Microsoft Access 3: Understanding and Creating Queries In Access Level 2, we learned how to perform basic data retrievals by using Search & Replace functions and Sort & Filter functions. For more complex

More information

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

More information

4 Logical Design : RDM Schema Definition with SQL / DDL

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

More information

Financial Data Access with SQL, Excel & VBA

Financial Data Access with SQL, Excel & VBA Computational Finance and Risk Management Financial Data Access with SQL, Excel & VBA Guy Yollin Instructor, Applied Mathematics University of Washington Guy Yollin (Copyright 2012) Data Access with SQL,

More information

Structured Query Language (SQL)

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

More information

Introduction to SQL for Data Scientists

Introduction to SQL for Data Scientists Introduction to SQL for Data Scientists Ben O. Smith College of Business Administration University of Nebraska at Omaha Learning Objectives By the end of this document you will learn: 1. How to perform

More information

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically Normalization of databases Database normalization is a technique of organizing the data in the database. Normalization is a systematic approach of decomposing tables to eliminate data redundancy and undesirable

More information

Database Programming with PL/SQL: Learning Objectives

Database Programming with PL/SQL: Learning Objectives Database Programming with PL/SQL: Learning Objectives This course covers PL/SQL, a procedural language extension to SQL. Through an innovative project-based approach, students learn procedural logic constructs

More information

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina

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

More information

DBMS Questions. 3.) For which two constraints are indexes created when the constraint is added?

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

More information

T-SQL STANDARD ELEMENTS

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

More information

Summary on Chapter 4 Basic SQL

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

More information

Database Query 1: SQL Basics

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

More information

1 Structured Query Language: Again. 2 Joining Tables

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

More information

SQL Tables, Keys, Views, Indexes

SQL Tables, Keys, Views, Indexes CS145 Lecture Notes #8 SQL Tables, Keys, Views, Indexes Creating & Dropping Tables Basic syntax: CREATE TABLE ( DROP TABLE ;,,..., ); Types available: INT or INTEGER REAL or FLOAT CHAR( ), VARCHAR( ) DATE,

More information

The Relational Data Model and Relational Database Constraints

The Relational Data Model and Relational Database Constraints The Relational Data Model and Relational Database Constraints Chapter Outline Relational Model Concepts Relational Model Constraints and Relational Database Schemas Update Operations and Dealing with Constraint

More information

3.GETTING STARTED WITH ORACLE8i

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

More information

3. Relational Model and Relational Algebra

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

More information

Guide to SQL Programming: SQL:1999 and Oracle Rdb V7.1

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

More information

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

More information

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013 CSE 530A Database Management Systems Introduction Washington University Fall 2013 Overview Time: Mon/Wed 7:00-8:30 PM Location: Crow 206 Instructor: Michael Plezbert TA: Gene Lee Websites: http://classes.engineering.wustl.edu/cse530/

More information

SQL Server 2008 Core Skills. Gary Young 2011

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

More information

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. Introduction to Databases. Why databases? Why not use XML?

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. Introduction to Databases. Why databases? Why not use XML? CS2Bh: Current Technologies Introduction to XML and Relational Databases Spring 2005 Introduction to Databases CS2 Spring 2005 (LN5) 1 Why databases? Why not use XML? What is missing from XML: Consistency

More information

Chapter 13: Query Optimization

Chapter 13: Query Optimization Chapter 13: Query Optimization Database System Concepts, 6 th Ed. See www.db-book.com for conditions on re-use Chapter 13: Query Optimization Introduction Transformation of Relational Expressions Catalog

More information

Part 4: Database Language - SQL

Part 4: Database Language - SQL 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

More information

Programming Database lectures for mathema

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

More information

Week 4 & 5: SQL. SQL as a Query Language

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

More information

SQL Server. 2012 for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

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 murachbooks@murach.com Expanded

More information

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query Objectives The objective of this lab is to learn the query language of SQL. Outcomes After completing this Lab,

More information

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

SQL - QUICK GUIDE. Allows users to access data in relational database management systems. http://www.tutorialspoint.com/sql/sql-quick-guide.htm SQL - QUICK GUIDE Copyright tutorialspoint.com What is SQL? SQL is Structured Query Language, which is a computer language for storing, manipulating

More information

MySQL Command Syntax

MySQL Command Syntax Get It Done With MySQL 5&6, Chapter 6. Copyright Peter Brawley and Arthur Fuller 2015. All rights reserved. TOC Previous Next MySQL Command Syntax Structured Query Language MySQL and SQL MySQL Identifiers

More information

ICAB4136B Use structured query language to create database structures and manipulate data

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

More information

Database Administration with MySQL

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

More information

Chapter 6: Physical Database Design and Performance. Database Development Process. Physical Design Process. Physical Database Design

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

More information

Mini User's Guide for SQL*Plus T. J. Teorey

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

More information

DBMS / Business Intelligence, SQL Server

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.

More information

Advanced SQL. Jim Mason. www.ebt-now.com Web solutions for iseries engineer, build, deploy, support, train 508-728-4353. jemason@ebt-now.

Advanced SQL. Jim Mason. www.ebt-now.com Web solutions for iseries engineer, build, deploy, support, train 508-728-4353. jemason@ebt-now. Advanced SQL Jim Mason jemason@ebt-now.com www.ebt-now.com Web solutions for iseries engineer, build, deploy, support, train 508-728-4353 What We ll Cover SQL and Database environments Managing Database

More information

There are five fields or columns, with names and types as shown above.

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.

More information