Onsdag The concepts in a relation data model SQL DDL DML.

Slides:



Advertisements
Similar presentations
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Advertisements

SQL Group Members: Shijun Shen Xia Tang Sixin Qiang.
SQL Queries Principal form: SELECT desired attributes FROM tuple variables –– range over relations WHERE condition about tuple variables; Running example.
Winter 2002Arthur Keller – CS 1806–1 Schedule Today: Jan. 22 (T) u SQL Queries. u Read Sections Assignment 2 due. Jan. 24 (TH) u Subqueries, Grouping.
SQL CSET 3300.
OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
Database technology Lecture 2: Relational databases and SQL
Database Design -- Basic SQL
From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.
IS698: Database Management Min Song IS NJIT. Overview  Query processing  Query Optmization  SQL.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 SQL: Data Definition, Constraints, and Basic Queries and Updates.
Winter 2002Arthur Keller – CS 1809–1 Schedule Today: Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers,
CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #3.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #2.
Chapter 6 Notes. 6.1 Simple Queries in SQL SQL is not usually used as a stand-alone language In practice there are hosting programs in a high-level language.
CSE314 Database Systems Lecture 4 Basic SQL Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
SQL 2014, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes via his course web.
SCUHolliday6–1 Schedule Today: u SQL Queries. u Read Sections Next time u Subqueries, Grouping and Aggregation. u Read Sections And then.
Databases : SQL-Introduction 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
1 IT 244 Database Management System Lecture 11 SQL Select-From-Where Statements Meaning of queries Subqueries Ref : -A First Course in Database System.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
Computational Biology Dr. Jens Allmer Lecture Slides Week 6.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
Databases 1 Second lecture.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1 Introduction to SQL Database Systems. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation.
Introduction to SQL Introduction Select-From-Where Statements Queries over Several Relations Subqueries.
Himanshu GuptaCSE 532-SQL-1 SQL. Himanshu GuptaCSE 532-SQL-2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
1 Database Systems Basic SQL. 2Outline  SQL Data Definition and Data Types  Specifying Constraints in SQL  Basic Retrieval Queries in SQL  INSERT,
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Embedded SQL, JDBC. u Read Sections.
Databases : SQL Multi-Relations 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman.
1 COMP 1100 Basic SQL David J. Stucki. Outline SQL Overview Retrievals Schema creation Table creation Constraints Inserts Updates 2.
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
1 Database Design: DBS CB, 2 nd Edition SQL: Select-From-Where Statements & Multi-relation Queries & Subqueries Ch. 6.
Select-From-Where Statements Multirelation Queries Subqueries
The SQL Database Grammar
Schedule Today: Jan. 28 (Mon) Jan. 30 (Wed) Next Week Assignments !!
Slides are reused by the approval of Jeffrey Ullman’s
CPSC-310 Database Systems
Computational Biology
Outerjoins, Grouping/Aggregation Insert/Delete/Update
Foreign Keys Local and Global Constraints Triggers
Introduction to Database Systems, CS420
CPSC-608 Database Systems
06a: SQL-1 The Basics– Select-From-Where
CPSC-608 Database Systems
376a. Database Design Dept. of Computer Science Vassar College
376a. Database Design Dept. of Computer Science Vassar College
CPSC-310 Database Systems
CPSC-310 Database Systems
IT 244 Database Management System
CMSC-461 Database Management Systems
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
CPSC-608 Database Systems
CPSC-608 Database Systems
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

Onsdag The concepts in a relation data model SQL DDL DML

Concepts

create table Employee (fname varchar(15) not null, minit char, lname varchar(15) not null, ssn char(9), bdate datetime, address varchar(30), sex char, salary decimal(10,2), superssn char(9), dno int not null, primary key (ssn), foreign key(superssn) references Employee(ssn), foreign key (dno) references Department(dnumber));

Create table create table Employee (……. dno int not null default 1, primary key (ssn), foreign key(superssn) references Employee(ssn) on update cascade, foreign key (dno) references Department(dnumber) on delete set default on update cascade); )

constraints On attributes not null check (dno > 0 and dno < 20) unique primary key (defines the PK, implicit not null)

Referential integrity constraints Foreign key foreign key(dno) references deparment(dnumber) Cascade foreign key(superssn) references Employee(ssn) on update cascade

Alter table alter table employee add varchar(40); alter table employee add constraint fkmr foreign key (dept) references department(dnumber);

SQL - DML Modifying the data insert into …, update... set... where..., delete from … where... Query on the data select... from... where...

insert insert into employee values ('James','E','Borg',' ',' ','450 Stone, Houston,TX','M',55000,null,1);

Update update employee set salary = where ssn = ’ ’ update employee set salary = salary * 1.2 where salary >= 30000

Deleting data Delete, deletes tuples from the database if the fulfill the where condition Deleting a tuple with referentiel integritet constraints defined with cascade, can delete tuples in other tables If there are no where condition, all tuples from at table are deleted delete from department

delete delete from employee where ssn = ” ” //delete departments which has no employees delete from department where dnumber not in (select dno from employee )

Select-From-Where Statements SELECT FROM WHERE

Single-Relation Query Starts with the FROM clause. Then the WHERE clause. At last the attributes from the SELECT clause. Algerbra operations?

* in SELECT clauses SELECT * FROM Employee WHERE ssn = ‘ ’;

Renaming Attributes SELECT fname AS firstName FROM employee WHERE ssn = ‘ ’

NULL Values Tuples in SQL relations can have NULL as a value for one or more components. Meaning depends on context. Two common cases: Missing value : e.g., we know Joe’s Bar has some address, but we don’t know what it is. Inapplicable : e.g., the value of attribute spouse for an unmarried person.

Comparing NULL’s to Values The logic of conditions in SQL is really 3-valued logic: TRUE, FALSE, UNKNOWN. When any value is compared with NULL, the truth value is UNKNOWN. But a query only produces a tuple in the answer if its truth value for the WHERE clause is TRUE (not FALSE or UNKNOWN).

Use is or is not select Fname, lname from employee where superssn is null Instead of = or != Since sql considers each null value as being distinct from every other null value

Multirelation Queries Interesting queries often combine data from more than one relation. We can address several relations in one query by listing them all in the FROM clause. Distinguish attributes of the same name by “. ”

Example select fname from employee, dependent where fname = dependent_name and ssn = essn;

Formal Semantics Almost the same as for single-relation queries: 1.Start with the product of all the relations in the FROM clause. 2.Apply the selection condition from the WHERE clause. 3.Project onto the list of attributes and expressions in the SELECT clause. Algebra operations

Explicit Tuple-Variables Sometimes, a query needs to use two copies of the same relation. Distinguish copies by following the relation name by the name of a tuple-variable, in the FROM clause. It’s always an option to rename relations this way, even when not essential.

Example Foreach employee select the name from the employee and his supervisor select e.fname,e.lname, s.fname, s.lname from employee e, employee s where e.superssn = s.ssn;

Subqueries A parenthesized SELECT-FROM-WHERE statement (subquery) can be used as a value in a number of places, including FROM and WHERE clauses. Example: in place of a relation in the Where clause, we can place another query, and then query its result.

Subqueries That Return One Tuple If a subquery is guaranteed to produce one tuple, then the subquery can be used as a value. Usually, the tuple has one component. Also typically, a single tuple is guaranteed by keyness of attributes. A run-time error occurs if there is no tuple or more than one tuple.

Example From Sells(bar, beer, price), find the bars that serve Miller for the same price Joe charges for Bud. Two queries would surely work: 1.Find the price Joe charges for Bud. 2.Find the bars that serve Miller at that price.

Query + Subquery Solution SELECT bar FROM Sells WHERE beer = ‘Miller’ AND price = (SELECT price FROM Sells WHERE bar = ‘Joe’’s Bar’ AND beer = ‘Bud’); The price at which Joe sells Bud

The IN Operator IN is true if and only if the tuple is a member of the relation. NOT IN means the opposite. IN-expressions can appear in WHERE clauses. The is often a subquery.

Example From Beers(name, manf) and Likes(drinker, beer), find the name and manufacturer of each beer that Fred likes. SELECT * FROM Beers WHERE name IN (SELECT beer FROM Likes WHERE drinker = ‘Fred’); The set of beers Fred likes

The Exists Operator EXISTS( ) is true if and only if the is not empty. Being a boolean-valued operator, EXISTS can appear in WHERE clauses.

Example Query with EXISTS select e.fname, e.lname from employee e where exists (select * from dependent where e.ssn = essn and e.sex = sex and e.fname = dependent_name)

Important Points Two single quotes inside a string represent the single-quote (apostrophe). Conditions in the WHERE clause can use AND, OR, NOT SQL is case-insensitive. In general, upper and lower case characters are the same, except inside quoted strings.

Patterns WHERE clauses can have conditions in which a string is compared with a pattern, to see if it matches. General form: LIKE or NOT LIKE Pattern is a quoted string with % = ‘any string’; _ = “any character.”

Exercises 8.13 (Company databasen) 8.14 (Company databasen FlereSQLØvelser (word doc. ) 8.10 and 8.11 create an database and queries on the database