Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured Query Language (SQL)

Similar presentations


Presentation on theme: "Structured Query Language (SQL)"— Presentation transcript:

1 Structured Query Language (SQL)

2 Running Example DEPARTMENT(DepartmentName, BudgetCode, OfficeNumber, Phone) EMPLOYEE(EmployeeNumber, FirstName, LastName, Department, Phone, ) PROJECT(ProjectID, ProjectName, Department, MaxHours, StartDate, EndDate) ASSIGNMENT(ProjectID, EmployeeNumber, HoursWorked) IST210

3 Get your SQL Account Online
Go to 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 in your PSU mailbox which contains your username/password

4 Connect to your SQL Server
Log on an IST Windows machine If not in the lab, use remote desktop 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 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, VarChar(100) NOT NULL UNIQUE, CONSTRAINT EMP_DEPART_FK FOREIGN KEY(Department) REFERENCES DEPARTMENT(DepartmentName) ON UPDATE CASCADE ); IST210

7 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

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 ACTION ON DELETE CASCADE, CONSTRAINT ASSIGN_EMP_FK FOREIGN KEY (EmployeeNumber) REFERENCES EMPLOYEE (EmployeeNumber) ON DELETE NO ACTION ); A composite primary key IST210

9 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

10 Insert Data to Department Table
One to one mapping INSERT INTO DEPARTMENT VALUES('Administration', 'BC ', 'BLDG01-300', ' '); IST210

11 Insert Data to Department Table
INSERT INTO DEPARTMENT VALUES('Legal', 'BC ', 'BLDG01-200', ' '); INSERT INTO DEPARTMENT VALUES('Accounting', 'BC ', 'BLDG01-100', ' '); INSERT INTO DEPARTMENT VALUES('Finance', 'BC ', 'BLDG01-140', ' '); INSERT INTO DEPARTMENT VALUES('Human Resources', 'BC ', 'BLDG01-180', ' '); INSERT INTO DEPARTMENT VALUES('Production', 'BC ', 'BLDG02-100', ' '); INSERT INTO DEPARTMENT VALUES('Marketing', 'BC ', 'BLDG02-200', ' '); INSERT INTO DEPARTMENT VALUES('InfoSystems', 'BC ', 'BLDG02-270', ' '); 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', ' ', IST210

14 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, ) VALUES('James', 'Nestor', 'InfoSystems', OR INSERT INTO EMPLOYEE VALUES('James', 'Nestor', 'InfoSystems', NULL, IST210

15 Insert Data to Employee Table
INSERT INTO EMPLOYEE VALUES( 'Rosalie', 'Jackson', 'Administration', ' ', 'Richard', 'Bandalone', 'Legal', ' ', 'Tom', 'Caruthers', 'Accounting', ' ', 'Heather', 'Jones', 'Accounting', ' ', 'Mary', 'Abernathy', 'Finance', ' ', 'George', 'Smith', 'Human Resources', ' ', 'Tom', 'Jackson', 'Production', ' ', 'George', 'Jones', 'Production', ' ', 'Ken', 'Numoto', 'Marketing', ' ', 'Rick', 'Brown', 'InfoSystems', ' ', 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” See the effects of ON UPDATE CASCADE IST210

18 Delete Data (Software)
Delete the “Finance” row See the effects of ON DELETE NO ACTION IST210

19 Delete Table (Software)
Caution! All data in that table will be deleted! IST210

20 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

21 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.

22 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;

23 View the Result

24 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;

25 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;


Download ppt "Structured Query Language (SQL)"

Similar presentations


Ads by Google