<<

Goals

IT420: Management and  SQL: Definition Language Organization  CREATE  ALTER  DROP  SQL: Data Manipulation Language SQL: Structured Query  INSERT Language  DELETE (Chapter 7)  UPDATE  SELECT

1 2

Relational Query Languages SQL DDL and DML

 A major strength of the :  SQL statements can be divided into two  supports simple, powerful querying of data categories:  (DDL) statements  Queries can be written intuitively, and the  Used for creating and modifying tables, views, and DBMS is responsible for efficient other structures evaluation.  CREATE, DROP, ALTER  Data manipulation language (DML) statements.  Used for queries and data modification  INSERT, DELETE, UPDATE, SELECT

3 4

Creating Tables CREATE Example

CREATE TABLE table_name(  CREATE TABLE Students column_name1 column_type1 [constraints1], (StudentNumber integer NOT NULL, …, StudentLastName char(18) NOT NULL, [[ CONSTRAINT constraint_name] table_constraint] StudentFirstName char(18) NOT NULL, ) Email char(50), Table constraints: PhoneNumber char(18),  NULL/NOT NULL MajorDepartmentName char(18),  (columns)  UNIQUE (columns) CONSTRAINT PK_Students PRIMARY KEY (StudentNumber),  CHECK (conditions) CONSTRAINT U_Email UNIQUE (Email),  (local_columns ) REFERENCES foreign_table CONSTRAINT FK_Dept FOREIGN KEY(MajorDepartmentName) (foreign_columns ) [ON DELETE action_d ON UPDATE action_u ] REFERENCES DEPARTMENTS(DepartmentName) ON DELETE NO ACTION ON UPDATE CASCADE Specify in SQL Server: ) column_name int_type IDENTITY (seed, increment)

5 6

1 FOREIGN KEY Constraints FOREIGN KEY Constraints

DEPARTMENTS DepartmentName Phone Building Room CREATE TABLE Students DepartmentName: char(18) Mathematics 410-293-4573 Michelson Hall 308  4 options on (StudentNumber integer, Phone: char(18) History 410-293-2255 Sampson Hall 120 StudentLastName char(18) NOT NULL, Building: char(18) Computer Science 410-293-6800 Michelson Hall 340 deletes and StudentFirstName char(18) NOT NULL, Room: integer Email char(50) NULL, D:SN updates: U:C Student Student Student Email PhoneNumber MajorDepartmentName PhoneNumber char(18) NULL, Number LastName FirstName MajorDepartmentName char(18) NULL, 190 Smith John [email protected] 410-431-3456  NO ACTION Majors PRIMARY KEY (StudentNumber), 673 Doe Jane [email protected] Computer Science (delete/update is UNIQUE(Email), 312 Doe Bob [email protected] 443-451-7865 Mathematics I:SN rejected) FOREIGN KEY (MajorDepartmentName) U:SN REFERENCES Departments (DepartmentName) STUDENTS CREATE TABLE Departments ON DELETE NULL StudentNumber: integer (DepartmentName char(18),  CASCADE ON UPDATE CASCADE StudentLastName: char(18) Phone char(18) NOT NULL, StudentFirstName: char(18)  ) Email: varchar(50) Building char(18), SET NULL PhoneNumber: char(18) Room integer, DepartmentName: char(18) (FK) PRIMARY KEY (DepartmentName)  SET DEFAULT ) 7 8

Modifying Tables ALTER TABLE Examples

 ALTER TABLE table_name clause  ALTER TABLE Students ADD BirthDate datetime NULL Clauses:  ALTER TABLE Students DROP COLUMN BirthDate ADD COLUMN column_name column_type [constraints] DROP COLUMN column_name  ALTER COLUMN / MODIFY – DBMS specific! ALTER TABLE Student ADD CONSTRAINT FK_Department ADD CONSTRAINT constraint FOREIGN KEY (MajorDepartmentName) DROP CONSTRAINT constraint_name REFERENCES Departments (DepartmentName) ON DELETE NO ACTION ON UPDATE CASCADE

9 10

Removing Tables SQL DDL and DML

 DROP TABLE table_name  Data definition language (DDL)

DROP TABLE Departments; statements  Used for creating and modifying tables, views, and other structures  If there are constraints dependent on table:  Remove constraints  CREATE, ALTER, DROP  Drop table  Data manipulation language (DML) statements. ALTER TABLE Students DROP CONSTRAINT FK_Department;  Used for queries and data modification  INSERT, DELETE, UPDATE, SELECT DROP TABLE Departments; 11 12

2 SQL DML INSERT Statement

INSERT INTO table_name [ (column_list) ] VALUES (data_values)  Data manipulation language (DML) INSERT INTO table_name [ (column_list) ] select_statement statements. INSERT command:  Used for queries and data modification INSERT INTO Students (StudentNumber, StudentLastName, StudentFirstName) VALUES (190, ‘Smith', ‘John’);  INSERT INSERT INTO Students VALUES(190, ‘Smith’, ‘John’, ‘[email protected]’,  DELETE ‘410-431-3456’)  UPDATE  SELECT  Bulk INSERT: INSERT INTO Students (StudentNumber, StudentLastName, StudentFirstName, Email, PhoneNumber) SELECT * FROM Second_Class_Students;

13 14

UPDATE Statement DELETE Statement

UPDATE table_name DELETE FROM table_name SET column_name1 = expression1 [ ,column_name2 = expression2,… ] [ WHERE search_condition ] [ WHERE search_condition ]  DELETE command:  UPDATE command: DELETE FROM Students UPDATE Students SET PhoneNumber = ‘410-123-4567’ WHERE StudentNumber = 190; WHERE StudentNumber = 673;  If you omit the WHERE clause, you will every in the table!

 BULK UPDATE command:  Another example: UPDATE Students SET PhoneNumber = ‘410-123-4567’ DELETE FROM Departments WHERE StudentLAstName = ‘Doe’; WHERE DepartmentName = ‘ComSci’

Student Student Student Email PhoneNumber Integrity constraints?! Number LastName FirstName 190 Smith John [email protected] 410-431-3456 673 Doe Jane [email protected] 15 16 312 Doe Bob [email protected] 443-451-7865

Selecting All Columns: The SQL SELECT Statement The Asterisk (*) Keyword  Basic SQL Query: SELECT * SELECT [DISTINCT] column_name(s) | * FROM Students; FROM table_name(s) Student Student Student Email PhoneNumber MajDeptName [WHERE conditions] Number LastName FirstName 190 Smith John [email protected] 410-431-3456 ComSci

673 Doe Jane [email protected] ComSci

312 Doe Jane [email protected] 443-451-7865 Math

17 18

3 Specific Columns and Rows from The DISTINCT Keyword One Table SELECT StudentNumber, SELECT SName SELECT DISTINCT StudentLastName, StudentFirstName FROM Students; SName FROM Students FROM Students; WHERE MajDeptName = ‘ComSci’;

StudentLastName

Student Student Student StudentLastName Number LastName FirstName Smith 190 Smith John Doe Smith 673 Doe Jane Doe Doe

19 20

Class Exercise Students, Courses, Enrolled

Find the names of students enrolled in IT420  Division(Name , Building, OfficeNb)  Department(DeptName , ChairName, WebAddress, SELECT SName DivName) FROM Students S, Enrolled E WHERE S.Snb = E.SNb AND E.Cid = ‘IT420’  Create tables SNb SName Email Cid CName CDept  Modify Department to add a FK constraint for DivName 190 Smith [email protected] IT420 Database ComSci

 Create table Colleges with same structure as Division 673 Doe [email protected] IT340 Networks ComSci  Insert everything from Division into Colleges 312 Doe [email protected] SM121 Calculus1 Math

 Remove Division table SNb Cid Semester

 Find the name of the Chair of the ‘Math’ Department 190 IT340 Spring2006

312 IT420 Fall2005

21 22

SELECT - Conceptual Evaluation Example Conceptual Evaluation Strategy

 Semantics of an SQL query defined in terms of SELECT SName the following conceptual evaluation strategy: FROM Students S, Enrolled E WHERE S.Snb = E.SNb AND E.Cid = ‘IT420’  Compute the cross-product of table_names  Discard resulting rows if they fail condition  Delete columns that are not in column_names S.SNb SName Email E.SNb Cid Semester 190 Smith [email protected] 190 IT340 Spring2006

 If DISTINCT is specified, eliminate duplicate rows 190 Smith [email protected] 312 IT420 Fall2005  This strategy is probably the least efficient way 673 Doe [email protected] 190 IT340 Spring2006 673 Doe [email protected] 312 IT420 Fall2005 to compute a query! 312 Doe [email protected] 190 IT340 Spring2006  An optimizer will find more efficient strategies to 312 Doe [email protected] 312 IT420 Fall2005 compute the same answers.

23 24

4 Example Conceptual Evaluation Example Conceptual Evaluation

SELECT SName SELECT SName SName

FROM Students S, Enrolled E FROM Students S, Enrolled E Doe WHERE S.Snb = E.SNb AND E.Cid = ‘IT420’ WHERE S.Snb = E.SNb AND E.Cid = ‘IT420’

S.SNb SName Email E.SNb Cid Semester 190 Smith [email protected] 190 IT340 Spring2006 S.SNb SName Email E.SNb Cid Semester 190 Smith [email protected] 190 IT340 Spring2006 190 Smith [email protected] 312 IT420 Fall2005 673 Doe [email protected] 190 IT340 Spring2006 190 Smith [email protected] 312 IT420 Fall2005 673 Doe [email protected] 312 IT420 Fall2005 673 Doe [email protected] 190 IT340 Spring2006 312 Doe [email protected] 190 IT340 Spring2006 673 Doe [email protected] 312 IT420 Fall2005 312 Doe [email protected] 312 IT420 Fall2005 312 Doe [email protected] 190 IT340 Spring2006 312 Doe [email protected] 312 IT420 Fall2005

25 26

Modified Query Sorting the Results

SELECT SNb SELECT … FROM Students S, Enrolled E FROM … [WHERE …] WHERE S.Snb = E.SNb AND E.Cid =‘IT420’ column_name(s) [ASC/DESC]

 Would the result be different with Example: DISTINCT? SELECT SNb, SName FROM Students ORDER BY SName ASC, SNb DESC

27 28

LIKE and Wildcards WHERE Clause Options

SELECT *  AND, OR FROM Students  IN, NOT IN, BETWEEN WHERE SNb LIKE ‘_9_%’ SELECT SNb  SQL 92 Standard (SQL Server, Oracle, etc.):  _ = Exactly one character FROM Students S, Enrolled E  % = Any set of one or more characters WHERE S.SNb = E.Nb AND  MS Access  ? = Exactly one character E.Cid NOT IN (‘ComSci’, ‘Math’)  * = Any set of one or more characters

29 30

5 Class Exercise

 Students(SNb , SName, Email)  Courses(Cid ,CName, Dept)  Enrolled(SNb,Cid, Semester)

 Find the student number and name for each student enrolled in ‘Spring2007’ semester  Find the names of all students enrolled in ‘ComSci’ courses

31

6