1 it420: database management and organization sql: structured query language 25 january 2006 adina...
TRANSCRIPT
![Page 1: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/1.jpg)
1
IT420: Database Management and Organization
SQL: Structured Query Language25 January 2006
Adina Crăiniceanuwww.cs.usna.edu/~adina
![Page 2: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/2.jpg)
2
Goals
SQL
![Page 3: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/3.jpg)
3
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 for efficient evaluation. The key: precise semantics for relational
queries Allows the optimizer to extensively re-order
operations, and still ensure that the answer does not change
![Page 4: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/4.jpg)
4
SQL: The Structured Query Language Developed by IBM (system R) in the
1970s Need for a standard since it is used by
many vendors Standards:
SQL-86 SQL-89 (minor revision) SQL-92 (major revision) SQL-99 (major extensions, current standard)
![Page 5: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/5.jpg)
5
SQL as a Data Sublanguage
SQL is not a full featured programming language as are C, C#, and Java
SQL is a data sublanguage Create and process database data
SQL is ubiquitous in enterprise-class DBMS products
SQL programming is a critical skill
![Page 6: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/6.jpg)
6
SQL DDL and DML
SQL statements can be divided into two categories: Data definition language (DDL) statements
Used for creating and modifying tables, views, and other structures
CREATE, DROP, ALTER Data manipulation language (DML)
statements. Used for queries and data modification INSERT, DELETE, UPDATE, SELECT
![Page 7: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/7.jpg)
7
SQL DDL Statements
CREATE ALTER DROP
![Page 8: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/8.jpg)
8
CREATE TABLE
CREATE TABLE statement is used for creating relations
Each column is described with three parts: column name data type optional constraints
![Page 9: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/9.jpg)
9
CREATE TABLE ExampleSTUDENTS
StudentNumber: INTEGER NOT NULL
StudentLastName: CHAR(18) NOT NULLStudentFirstName: CHAR(18) NOT NULLEmail: CHAR(50) NULLPhoneNumber: CHAR(18) NULL
CREATE TABLE Students(StudentNumber integer,StudentLastName char(18) NOT NULL,StudentFirstName char(18) NOT NULL,Email char(50),PhoneNumber char(18) )
Student
Number
Student
LastName
Student
FirstName
Email PhoneNumber
190 Smith John [email protected] 410-431-3456
673 Doe Jane [email protected]
312 Red Bob [email protected] 443-451-7865
![Page 10: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/10.jpg)
10
Constraints
Constraints can be defined within the CREATE TABLE statement, or they can be added to the table after it is created using the ALTER table statement
Five types of constraints: NULL/NOT NULL PRIMARY KEY may not have null values UNIQUE may have null values CHECK FOREIGN KEY
![Page 11: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/11.jpg)
11
Constraints Examples
STUDENTS
StudentNumber: INTEGER NOT NULL
StudentLastName: CHAR(18) NOT NULLStudentFirstName: CHAR(18) NOT NULLEmail: CHAR(50) NULL (AK1.1)PhoneNumber: CHAR(18) NULL
CREATE TABLE Students(StudentNumber integer,StudentLastName char(18) NOT NULL,StudentFirstName char(18) NOT NULL,Email char(50),PhoneNumber char(18),PRIMARY KEY (StudentNumber),UNIQUE (Email) )
Student
Number
Student
LastName
Student
FirstName
Email PhoneNumber
190 Smith John [email protected] 410-431-3456
673 Doe Jane [email protected]
312 Doe Bob [email protected] 443-451-7865
![Page 12: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/12.jpg)
12
Default Values and Data Constraints
Students table Default value for
PhoneNumber: 410-123-4567
Email like “*@usna.edu”
CREATE TABLE Students (StudentNumber integer, StudentLastName char(18) NOT NULL, StudentFirstName char(18) NOT NULL, Email char(50), PhoneNumber char(18) DEFAULT “410-123-4567”, PRIMARY KEY (StudentNumber), UNIQUE(Email), CHECK (Email LIKE “*@usna.edu”) )
Syntax depends on DBMS!!!
![Page 13: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/13.jpg)
13
FOREIGN KEY Constraints
Majors
I:SNU:SN
D:SNU:C
DEPARTMENTS
DepartmentName: char(18)
Phone: char(18)Building: char(18)Room: integer
STUDENTS
StudentNumber: integer
StudentLastName: char(18)StudentFirstName: char(18)Email: varchar(50)PhoneNumber: char(18)DepartmentName: char(18) (FK)
Student
Number
Student
LastName
Student
FirstName
Email PhoneNumber MajorDepartmentName
190 Smith John [email protected] 410-431-3456
673 Doe Jane [email protected] Computer Science
312 Doe Bob [email protected] 443-451-7865 Mathematics
DepartmentName Phone Building Room
Mathematics 410-293-4573 Michelson Hall 308
History 410-293-2255 Sampson Hall 120
Computer Science 410-293-6800 Michelson Hall 340
![Page 14: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/14.jpg)
14
FOREIGN KEY Constraints
Majors
I:SNU:SN
D:SNU:C
DEPARTMENTS
DepartmentName: char(18)
Phone: char(18)Building: char(18)Room: integer
STUDENTS
StudentNumber: integer
StudentLastName: char(18)StudentFirstName: char(18)Email: varchar(50)PhoneNumber: char(18)DepartmentName: char(18) (FK)
Student
Number
Student
LastName
Student
FirstName
Email PhoneNumber MajorDepartmentName
190 Smith John [email protected] 410-431-3456
673 Doe Jane [email protected] Computer Science
312 Doe Bob [email protected] 443-451-7865 Mathematics
DepartmentName Phone Building Room
Mathematics 410-293-4573 Michelson Hall 308
History 410-293-2255 Sampson Hall 120
Computer Science 410-293-6800 Michelson Hall 340
CREATE TABLE Departments (DepartmentName char(18), Phone char(18) NOT NULL, Building char(18), Room integer, PRIMARY KEY (DepartmentName) )
![Page 15: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/15.jpg)
15
FOREIGN KEY ConstraintsCREATE TABLE Students (StudentNumber integer, StudentLastName char(18) NOT NULL, StudentFirstName char(18) NOT NULL, Email char(50), PhoneNumber char(18), MajorDepartmentName char(18), PRIMARY KEY (StudentNumber), UNIQUE(Email), FOREIGN KEY (MajorDepartmentName)REFERENCES Departments (DepartmentName) ON DELETE SET NULLON UPDATE CASCADE )
SQL/92 and SQL:1999 support all 4 options on deletes and updates. Default is NO ACTION
(delete/update is rejected)
CASCADE (also delete all rows that refer to deleted row)
SET NULL / SET DEFAULT (sets foreign key value of referencing row)
![Page 16: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/16.jpg)
16
Implementing Cardinalities
![Page 17: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/17.jpg)
17
ALTER Statement
ALTER statement changes table structure, properties, or constraints
after the table has been created
![Page 18: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/18.jpg)
18
Adding and Dropping Columns
The following statement will add a column named BirthDate to the Students table:ALTER TABLE Students ADD COLUMN BirthDate Datetime
NULL;
You can drop an existing column with the statement:ALTER TABLE Students DROP COLUMN BirthDate;
![Page 19: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/19.jpg)
19
Adding and Dropping Constraints
ALTER can be used to add a constraint as follows:ALTER TABLE Student ADD CONSTRAINT DepartmentFK
FOREIGN KEY (MajorDepartmentName)
REFERENCES Departments (DepartmentName)
ON DELETE NO ACTION
ON UPDATE CASCADE
ALTER can be used to drop a constraint:ALTER TABLE Student DROP CONSTRAINT DepartmentFK;
![Page 20: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/20.jpg)
20
Removing Tables
SQL DROP TABLE:DROP TABLE Departments;
If there are constraints:
ALTER TABLE Students
DROP CONSTRAINT DepartmentFK;
DROP TABLE Departments;
![Page 21: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/21.jpg)
21
Class Exercise
![Page 22: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/22.jpg)
22
SQL DDL and DML
Data definition language (DDL) statements
Used for creating and modifying tables, views, and other structures
CREATE, ALTER, DROP
Data manipulation language (DML) statements.
Used for queries and data modification INSERT, DELETE, UPDATE, SELECT
![Page 23: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/23.jpg)
23
INSERT Statement
INSERT command:INSERT INTO Students (StudentNumber, StudentLastName,
StudentFirstName)VALUES (190, ‘Smith', ‘John’);
INSERT INTO Students VALUES(190, ‘Smith’, ‘John’, ‘[email protected]’, ‘410-431-3456’)
Bulk INSERT:INSERT INTO Students (StudentNumber, StudentLastName,
StudentFirstName, Email, PhoneNumber)SELECT *FROM Second_Class_Students;
![Page 24: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/24.jpg)
24
UPDATE Statement UPDATE command:
UPDATE StudentsSET PhoneNumber = ‘410-123-4567’WHERE StudentNumber = 673;
BULK UPDATE command:UPDATE Students
SET PhoneNumber = ‘410-123-4567’WHERE StudentLAstName = ‘Doe’;
Student
Number
Student
LastName
Student
FirstName
Email PhoneNumber
190 Smith John [email protected] 410-431-3456
673 Doe Jane [email protected]
312 Doe Bob [email protected] 443-451-7865
![Page 25: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/25.jpg)
25
DELETE Statement
DELETE command:DELETE FROM Students
WHERE StudentNumber = 190;
If you omit the WHERE clause, you will delete every row in the table!
![Page 26: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/26.jpg)
26
The SQL SELECT Statement
The fundamental framework for SQL query statement is the SQL SELECT statement: SELECT {ColumnName(s)} FROM {TableName(s)} WHERE {Conditions}
![Page 27: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/27.jpg)
27
Specific Columns on One Table
SELECTStudentNumber,StudentLastName
FROM Students;
StudentNumber StudentLastName
190 Smith
673 Doe
312 Doe
![Page 28: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/28.jpg)
28
Specify Column Order
SELECT StudentLastName,StudentNumber
FROM Students;
StudentLastName StudentNumber
Smith 190
Doe 673
Doe 312
![Page 29: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/29.jpg)
29
The DISTINCT Keyword
SELECT StudentLastName
FROM Students;
StudentLastName
Smith
Doe
Doe
SELECT DISTINCT StudentLastName
FROM Students;
StudentLastName
Smith
Doe
![Page 30: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/30.jpg)
30
Selecting All Columns: The Asterisk (*) KeywordSELECT *
FROM Students;
Student
Number
Student
LastName
Student
FirstName
Email PhoneNumber
190 Smith John [email protected] 410-431-3456
673 Doe Jane [email protected]
312 Doe Bob [email protected] 443-451-7865
![Page 31: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/31.jpg)
31
Specific Rows from One Table
SELECT *FROM StudentsWHERE StudentLastName = ‘Doe';
NOTE: SQL wants a plain ASCII single quote: ' NOT ‘ !
Student
Number
Student
LastName
Student
FirstName
Email PhoneNumber
673 Doe Jane [email protected]
312 Doe Bob [email protected] 443-451-7865
![Page 32: 1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu adina](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649e025503460f94aec2d5/html5/thumbnails/32.jpg)
32
Specific Columns and Rows from One TableSELECT StudentNumber,
StudentLastName, StudentFirstName
FROM StudentsWHERE PhoneNumber NOT NULL;
Student
Number
Student
LastName
Student
FirstName
190 Smith John
312 Doe Bob