Structured Query Language (SQL)
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
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
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
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
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
ProjectID Int NOT NULL IDENTITY (1000, 100) PRIMARY KEY, 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
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 ACTION ON DELETE CASCADE, CONSTRAINT ASSIGN_EMP_FK FOREIGN KEY (EmployeeNumber) REFERENCES EMPLOYEE (EmployeeNumber) ON DELETE NO ACTION ); A composite primary key IST210
Order to Create Tables ASSIGNMENT is dependent on PROJECT and EMPLOYEE PROJECT is dependent on DEPARTMENT EMPLOYEE is dependent on DEPARTMENT So we need to create DEPARTMENT first; then EMPLOYEE and PROJECT; Lastly, ASSIGNMENT IST210
Insert Data to Department Table One to one mapping INSERT INTO DEPARTMENT VALUES('Administration', 'BC-100-10', 'BLDG01-300', '360-285-8100'); IST210
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
View the Result (Software) IST210
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', 'Mary.Jacobs@WPC.com'); IST210
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 EMPLOYEE(FirstName, LastName, Department, Email) VALUES('James', 'Nestor', 'InfoSystems', 'James.Nestor@WPC.com'); OR INSERT INTO EMPLOYEE VALUES('James', 'Nestor', 'InfoSystems', NULL, 'James.Nestor@WPC.com'); IST210
Insert Data to Employee Table INSERT INTO EMPLOYEE VALUES( 'Rosalie', 'Jackson', 'Administration', '360-285-8120', 'Rosalie.Jackson@WPC.com'); 'Richard', 'Bandalone', 'Legal', '360-285-8210', 'Richard.Bandalone@WPC.com'); 'Tom', 'Caruthers', 'Accounting', '360-285-8310', 'Tom.Caruthers@WPC.com'); 'Heather', 'Jones', 'Accounting', '360-285-8320', 'Heather.Jones@WPC.com'); 'Mary', 'Abernathy', 'Finance', '360-285-8410', 'Mary.Abernathy@WPC.com'); 'George', 'Smith', 'Human Resources', '360-285-8510', 'George.Smith@WPC.com'); 'Tom', 'Jackson', 'Production', '360-287-8610', 'Tom.Jackson@WPC.com'); 'George', 'Jones', 'Production', '360-287-8620', 'George.Jones@WPC.com'); 'Ken', 'Numoto', 'Marketing', '360-287-8710', 'Ken.Mumoto@WPC.com'); 'Rick', 'Brown', 'InfoSystems', '360-287-8820', 'Rick.Brown@WPC.com'); IST210
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
Edit Data (Software) See the effects of ON UPDATE CASCADE Change “Human Resources” to “HR” See the effects of ON UPDATE CASCADE IST210
Delete Data (Software) Delete the “Finance” row See the effects of ON DELETE NO ACTION IST210
Delete Table (Software) Caution! All data in that table will be deleted! IST210
Drop Tables Wrong order!!! Employee is dependent on Department Must delete Employee before deleting Department Correct order! The reverse order of the order we create these tables IST210
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_NAME ADD ColumnName DataType; Note: the keyword COLUMN is not used in this command Example: Add a “CurrentTotalHours” attribute into PROJECT ALTER 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 PROJECT ALTER 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 PROJECT DROP COLUMN CurrentTotalHours;