structured query language sql-i ist 210 organization of data ist210 1

35
STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

Upload: maximilian-mcdonald

Post on 03-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 1

STRUCTURED QUERY LANGUAGE SQL-IIST 210 Organization of Data

Page 2: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 2

Content in today’s class• Create table• Insert data• Delete table

Page 3: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 3

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:2008 is current standard

Page 4: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 4

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]

Page 5: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 5

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

Page 6: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 6

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)

Page 7: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 7

Running Example

Page 8: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 8

Running Example (cont.)

Page 9: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 9

Create DEPARTMENT

• NOT NULL: null values are NOT allowed• If this attribute must have value, use NOT NULL• By default, we allow NULL values unless the variable is a primary

key• Primary key: DepartmentName is a primary key• Char(35) a string with length up to 35

Page 10: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 10

Data Types

Page 11: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 11

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/• Or in your browser:

https://svg.up.ist.psu.edu/rdpdirect.html?gateway=svg.up.ist.psu.edu&server=vlabs-web.up.ist.psu.edu&mapClipboard=on

• 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: Windows Authentication• Alternate Authentication (connect to the same database):

• SQL Server Authentication• Get your SQL account: https://www.up.ist.psu.edu/db/mssql.php

• Hit “Connect”• Navigate to your own database under the Databases folder

(IMPORTANT!!!)• Your database name is your PSU ID

Page 12: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 12

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

Page 13: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 13

CREATE TABLE DEPARTMENT(DepartmentNameChar(35) NOT NULL

PRIMARY KEY, BudgetCode Char(30) NOT NULL,OfficeNumber Char(15) NOT NULL,Phone Char(12) NOT NULL

);

Page 14: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 14

View the Result

• Right click Tables• Click “Refresh”• Right click table DEPARTMENT• Click “Design”

Page 15: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 15

Create EMPLOYEE Table

Define Foreign Key

IDENTITY(x,y): Surrogate key. Start from x, increment by y

Allow NULL valuesCan be omitted

UNIQUE: requires unique value for Email

Default value for Department

Varchar(100) and Char(100) both defines a string with length up to 35Varchar(35) the storage is the actual lengthChar(35) the storage is fixed 35

Page 16: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

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

Page 17: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 17

ON 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

Page 18: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 18

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

Page 19: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 19CREATE TABLE EMPLOYEE(EmployeeNumberInt 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

Page 20: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

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

Page 21: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 21

Create Assignment Table

A composite primary key

Page 22: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

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

);

Page 23: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

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

Page 24: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 24

Insert Data to Department Table

One to one mapping

INSERT INTO DEPARTMENT VALUES('Administration', 'BC-100-10', 'BLDG01-300', '360-285-8100');

Page 25: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 25

Insert Data to Department Table

INSERT INTO DEPARTMENT VALUES('Legal', 'BC-200-10', 'BLDG01-200', '360-285-8200');INSERT INTO DEPARTMENT VALUES('Accounting', 'BC-300-10', 'BLDG01-100', '360-285-8300');INSERT INTO DEPARTMENT VALUES('Finance', 'BC-400-10', 'BLDG01-140', '360-285-8400');INSERT INTO DEPARTMENT VALUES('Human Resources', 'BC-500-10', 'BLDG01-180', '360-285-8500');INSERT INTO DEPARTMENT VALUES('Production', 'BC-600-10', 'BLDG02-100', '360-287-8600');INSERT INTO DEPARTMENT VALUES('Marketing', 'BC-700-10', 'BLDG02-200', '360-287-8700');INSERT INTO DEPARTMENT VALUES('InfoSystems', 'BC-800-10', 'BLDG02-270', '360-287-8800');

Page 26: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 26

View the Result (Software)

Page 27: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 27

Insert Data to Employee Table

EmployeeNumber is a surrogate key, no need to insert EmployeeNumber

Department is a foreign key, so we need to make sure it does exist in the DEPARTMENT table

INSERT INTO EMPLOYEE VALUES('Mary', 'Jacobs', 'Administration', '360-285-8110',

'[email protected]');

Page 28: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 28

Insert Data to Employee Table

What if no phone number information for this employee?(When we define EMPLOYEE table, we allow phone number to be NULL)

We need to specify the table and corresponding columnsINSERT INTO EMPLOYEE(FirstName, LastName, Department, Email)

VALUES('James', 'Nestor', 'InfoSystems', '[email protected]');

INSERT INTO EMPLOYEEVALUES('James', 'Nestor', 'InfoSystems', NULL,

'[email protected]');

OR

Page 29: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 29

Insert Data to Employee TableINSERT INTO EMPLOYEE VALUES('Rosalie', 'Jackson', 'Administration', '360-285-8120', '[email protected]');INSERT INTO EMPLOYEE VALUES('Richard', 'Bandalone', 'Legal', '360-285-8210', '[email protected]');INSERT INTO EMPLOYEE VALUES('Tom', 'Caruthers', 'Accounting', '360-285-8310', '[email protected]');INSERT INTO EMPLOYEE VALUES('Heather', 'Jones', 'Accounting', '360-285-8320', '[email protected]');INSERT INTO EMPLOYEE VALUES('Mary', 'Abernathy', 'Finance', '360-285-8410', '[email protected]');INSERT INTO EMPLOYEE VALUES('George', 'Smith', 'Human Resources', '360-285-8510', '[email protected]');INSERT INTO EMPLOYEE VALUES('Tom', 'Jackson', 'Production', '360-287-8610', '[email protected]');INSERT INTO EMPLOYEE VALUES('George', 'Jones', 'Production', '360-287-8620', '[email protected]');INSERT INTO EMPLOYEE VALUES('Ken', 'Numoto', 'Marketing', '360-287-8710', '[email protected]');INSERT INTO EMPLOYEE VALUES('Rick', 'Brown', 'InfoSystems', '360-287-8820', '[email protected]');

Page 30: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 30

In-Class Exercise: Insert Data to Project Table

Page 31: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 31

Edit Data (Software)

See the effects of DELETE/UPDATE CASCADE/NO ACTION

Page 32: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 32

Delete Data (Software)

See the effects of DELETE/UPDATE CASCADE/NO ACTION

Page 33: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 33

Delete Table (Software)

Caution! All data in that table will be deleted!

Page 34: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 34

Drop Tables

Wrong order!!!Employee is dependent on DepartmentMust delete Employee before deleting Department

Correct order!The reverse order of the order we create these tables

Page 35: STRUCTURED QUERY LANGUAGE SQL-I IST 210 Organization of Data IST210 1

IST210 35

Assignment 3-1• Assignment 3 is divided into two parts • Part 1 (40 points) is on course website now, and is due in

a week (no late submission!)• How to access SQL?

• Come and work in the classroom• Use remote desktop (if working outside of IST)

• https://www.up.ist.psu.edu/vlabs/

• Requires SQL programming, so start early!