Download presentation
Presentation is loading. Please wait.
Published byErick Williamson Modified over 9 years ago
1
Structured Query Language (SQL) IST2101
2
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 IST2102
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] IST2103
4
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
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) IST2105
6
Running Example IST2106
7
Running Example (cont.) IST2107
8
Create DEPARTMENT IST2108 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
9
Data Types IST2109
10
Step 1: Get your SQL Account Online Go to https://www.up.ist.psu.edu/db/ mssql.php 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
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 IST21011
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” IST21012 Your PSU ID
13
IST21013 CREATE TABLE DEPARTMENT( DepartmentNameChar(35)NOT NULLPRIMARY KEY, BudgetCodeChar(30)NOT NULL, OfficeNumberChar(15)NOT NULL, PhoneChar(12)NOT NULL );
14
Step 4: View the Result Right click Tables Click “Refresh” Right click table DEPARTMENT Click “Design” IST21014
15
Create EMPLOYEE Table IST21015 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
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 IST21016
17
UPDATE CASCADE/NO ACTION IST21017 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
18
DELETE CASCADE/NO ACTION IST21018 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!!!
19
IST21019 CREATE TABLE EMPLOYEE( EmployeeNumberInt NOT NULL IDENTITY (1, 1) PRIMARY KEY, FirstNameChar(25) NOT NULL, LastName Char(25) NOT NULL, DepartmentChar(35)NOT NULL DEFAULT 'Human Resources', PhoneChar(12)NULL, Email VarChar(100)NOT NULL UNIQUE, CONSTRAINT EMP_DEPART_FKFOREIGN 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
20
In-class exercise: Create PROJECT Table IST21020 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
21
Create Assignment Table IST21021 A composite primary key
22
IST21022 CREATE TABLE ASSIGNMENT ( ProjectIDInt NOT NULL, EmployeeNumberInt NOT NULL, HoursWorkedNumeric(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 UPDATE NO ACTION ON DELETE NO ACTION );
23
Order to Create Tables IST21023 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.