Structured Query Language (SQL)
IST210 2
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)
Get your SQL Account Online
• Go to https://www.up.ist.psu.edu• Click Databases at the top, then
Microsoft SQL Database on the left• 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 4
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 5
Create Tables in SQL Server
• Click “New Query” on the upper-left corner• Copy & Paste script in the next three slides • Click “Execute”
Your PSU ID
IST210 6
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
);
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);
IST210 7
CREATE TABLE PROJECT (ProjectID Int NOT NULL IDENTITY
(1000, 100) PRIMARY KEY,ProjectName Char(50) NOT NULL,Department Char(35) NOT NULL,MaxHours Numeric(8,2) NOT NULL DEFAULT 100,
StartDate DateTime NULL, EndDate DateTime NULL,
CONSTRAINT PROJ_DEPART_FK FOREIGN KEY(Department)REFERENCES
DEPARTMENT(DepartmentName)ON UPDATE CASCADE
);
IST210 8
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
);
A composite primary key
IST210 9
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
IST210 10
Insert Data to Department Table
One to one mapping
INSERT INTO DEPARTMENT VALUES('Administration', 'BC-100-10', 'BLDG01-300', '360-285-8100');
IST210 11
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');
IST210 12
View the Result (Software)
IST210 13
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]');
IST210 14
INSERT INTO EMPLOYEE(FirstName, LastName, Department, Email)VALUES('James', 'Nestor', 'InfoSystems', '[email protected]');
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 columns
INSERT INTO EMPLOYEEVALUES('James', 'Nestor', 'InfoSystems', NULL, '[email protected]');
OR
IST210 15
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]');
IST210 16
In-Class Exercise: Insert Data to Project Table
Note: For INSERT statement, numbers such as Integer and Numeric values should not be enclosed in single quotes, but Char, VarChar, and DateTime values should.
IST210 17
Edit Data (Software)
See the effects of ON UPDATE CASCADE
Change “Human Resources” to “HR”
IST210 18
Delete Data (Software)
See the effects of ON DELETE NO ACTION
Delete the “Finance” row
IST210 19
Delete Table (Software)
Caution! All data in that table will be deleted!
IST210 20
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
ALTER Statement• We often need to change the design of
databases.– Deleting old ones is not an option when
databases have data already.
• ALTER statement is what we need.
ADD New Attributes with ALTER
ALTER TABLE TABLE_NAMEADD ColumnName DataType;
• Note: the keyword COLUMN is not used in this command• Example: Add a “CurrentTotalHours” attribute into
PROJECTALTER TABLE PROJECT
ADD CurrentTotalHours Numeric(8, 2) NULL;
View the Result
Change Attributes with ALTER
ALTER TABLE TABLE_NAME ALTER COLUMN ColumnName DataType;
• Example: Set all the values of CurrentTotalHours to 0 and make it NOT NULL
UPDATE PROJECT SET CurrentTotalHours = 0;
ALTER TABLE PROJECTALTER COLUMN CurrentTotalHours Numeric(8,2) NOT NULL;
Delete an Attribute with ALTER
ALTER TABLE TABLE_NAME DROP COLUMN ColumnName;
• Example: Delete the CurrentTotalHours attribute in PROJECT
ALTER TABLE PROJECTDROP COLUMN CurrentTotalHours;