structured query language (sql) ist2101. structured query language – acronym: sql – pronounced...
Post on 03-Jan-2016
228 Views
Preview:
TRANSCRIPT
IST210 1
Structured Query Language (SQL)
IST210 2
Structured Query Language
• Structured Query Language– Acronym: SQL– Pronounced as “S-Q-L” [“Ess-Que-El”]– Originally developed by IBM as the SEQUEL
language in the 1970s– SQL-92 is an ANSI national standard adopted in
1992– SQL:2011 is current standard
IST210 3
SQL Defined
• SQL is not a programming language, but rather a data sub-language
• SQL is comprised of– A data definition language (DDL)
• Used to define database structures– A data manipulation language (DML)
• Data definition and updating• Data retrieval (Queries)
– There are other SQL functions not covered in this chapter• Concurrency control [See Chapter 6]• Transaction control [See Chapter 6]
SQL for Data Definition
• The SQL data definition statements include– CREATE• To create database objects
– ALTER• To modify the structure and/or characteristics of database
objects– DROP• To delete database objects
IST210 5
Running Example• DEPARTMENT(DepartmentName, BudgetCode, OfficeNumber, Phone)• EMPLOYEE(EmployeeNumber, FirstName, LastName, Department, Phone, Email)• PROJECT(ProjectID, ProjectName, Department, MaxHours, StartDate, EndDate)• ASSIGNMENT(ProjectID, EmployeeNumber, HoursWorked)
IST210 6
Running Example
IST210 7
Running Example (cont.)
IST210 8
Create DEPARTMENT
• NOT NULL: null values are NOT allowed• If this attribute must have value, use NOT NULL
• Primary key: DepartmentName is a primary key• Char(35) a string with length up to 35
IST210 9
Data Types
Step 1: Get your SQL Account Online
• Go to https://www.up.ist.psu.edu/db/mssql.php
• Click either the “create a SQL account” button or the “reset SQL password” button, depending on whether you already have an account or not, and follow the instructions there
• You should receive an email in your PSU mailbox which contains your username/password
IST210 11
Step 2: Connect to your SQL Server• Log on an IST Windows machine
– If not in the lab, use remote desktop • https://www.up.ist.psu.edu/vlabs/
• Run the SQL Server application– Start Application Development and Management Microsoft SQL Server
2014 SQL Server 2014 Management Studio• Parameters
– Server Type: Database Engine– Server Name: upsql– Authentication: SQL Server Authentication– Username and password have been sent to you via email
• Hit “Connect”• Navigate to your own database under the Databases folder
(IMPORTANT!!!)– Your database name is your PSU ID
IST210 12
Step 3: Create a Table in SQL Server
• Click “New Query” on the upper-left corner• Copy & Paste script in the next slide • Click “Execute”
Your PSU ID
IST210 13
CREATE TABLE DEPARTMENT(DepartmentName Char(35) NOT NULL PRIMARY KEY, BudgetCode Char(30) NOT NULL,OfficeNumber Char(15) NOT NULL,Phone Char(12) NOT NULL
);
IST210 14
Step 4: View the Result
• Right click Tables• Click “Refresh”• Right click table DEPARTMENT• Click “Design”
IST210 15
Create EMPLOYEE Table
Define Foreign Key
IDENTITY(x,y): Surrogate key. Start from x, increment by y
Allow NULL values(Can be omitted)
UNIQUE: requires unique value for Email
Default value for Department
• Varchar(35) and Char(35) both defines a string with length up to 35
• Varchar(35) the storage is the actual length• Char(35) the storage is fixed 35
IST210 16
Referential Integrity
• Two operations in the main table:– UPDATE– DELETE
• Two corresponding operations in the foreign key table:– CASCADE
• Affect the primary key as well as the foreign keys
– NO ACTION (by default)• Cannot be changed/deleted unless a record is NOT
referred by any other table at all
IST210 17
UPDATE CASCADE/NO ACTION
Update “Human Resources” to “HR”
CASCADE NO ACTION
Error message! The operation is NOT allowed!(But it is fine to change the name of Accounting, because no Employee belongs to department Accounting.)
Original data
IST210 18
DELETE CASCADE/NO ACTION
Delete “Human Resources”
CASCADE NO ACTION
Error message! The operation is NOT allowed!(But it is fine to delete Accounting, because no Employee belongs to department Accounting.)
Original data
Julie is deleted!!!
IST210 19
CREATE TABLE EMPLOYEE(EmployeeNumber Int NOT NULL IDENTITY (1, 1)
PRIMARY KEY,FirstName Char(25) NOT NULL,LastName Char(25) NOT NULL,Department Char(35) NOT NULL DEFAULT
'Human Resources',Phone Char(12) NULL,Email VarChar(100) NOT NULL UNIQUE,CONSTRAINT EMP_DEPART_FK FOREIGN KEY(Department)
REFERENCES DEPARTMENT(DepartmentName)
ON UPDATE CASCADE);
Copy and paste the SQL script
Select only the new script you just pasted and click Execute
IST210 20
In-class exercise: Create PROJECT Table
Requirements:• ProjectID is a surrogate key, starting from 1000, increment 100• Use Numeric(8,2) for MaxHours, which means 8 decimal digits, and 2 decimal digits to
the right of the decimal point. E.g.: 12345678.21• Set MaxHours as 100 by default• Use DateTime for StartDate and EndDate• Make Update Cascade and Delete No Action
IST210 21
Create Assignment Table
A composite primary key
IST210 22
CREATE TABLE ASSIGNMENT ( ProjectID Int NOT NULL,
EmployeeNumber Int NOT NULL, HoursWorked Numeric(6,2) NULL, CONSTRAINT ASSIGNMENT_PK PRIMARY KEY (ProjectID, EmployeeNumber), CONSTRAINT ASSIGN_PROJ_FK FOREIGN KEY (ProjectID)
REFERENCES PROJECT (ProjectID)ON UPDATE NO ACTIONON DELETE CASCADE,
CONSTRAINT ASSIGN_EMP_FK FOREIGN KEY (EmployeeNumber)REFERENCES EMPLOYEE
(EmployeeNumber)ON UPDATE NO ACTIONON DELETE NO ACTION
);
IST210 23
Order to Create Tables
ASSIGNMENT is dependent on PROJECT and EMPLOYEEPROJECT is dependent on DEPARTMENTEMPLOYEE is dependent on DEPARTMENT
So we need to create DEPARTMENT first; then EMPLOYEE and PROJECT;Lastly, ASSIGNMENT
top related