Structured Query Language SQL-I IST 210 Organization of Data IST2101.

Slides:



Advertisements
Similar presentations
SQL-week5-1 In-Class Exercise Answer IST 210 Organization of Data IST2101.
Advertisements

1 SQL Server Management Studio SQL DDL CREATE TABLE Constraints ALTER TABLE DROP TABLE The GUI way Steen Jensen, autumn 2013.
Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating.
Structured Query Language Chapter Three Part 3 – Inserts, Updates, Deletes.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
DT211 Stage 2 Databases Lab 1. Get to know SQL Server SQL server has 2 parts: –A client, running on your machine, in the lab. You access the database.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Structured Query Language (SQL)
SQL for Data Retrieval. Save your SQL Scripts When working with SQL Management Studio, you should keep saving your scripts as a.sql file to somewhere.
Database Systems Lecture 5 Natasha Alechina
Structured Query Language
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Download three SQL script files from wiki page.
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
Introduction to SQL  SQL or sequel  It is a standardised language with thousands of pages in the standard  It can be in database system through GUI,
1 CSE 480: Database Systems Lecture 9: SQL-DDL Reference: Read Chapter of the textbook.
1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
Introduction to SQL Steve Perry
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
CSC 2720 Building Web Applications Database and SQL.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Structured Query Language SQL-I
Structured Query Language (SQL) IST2101. Structured Query Language – Acronym: SQL – Pronounced as “S-Q-L” [“Ess-Que-El”] – Originally developed by IBM.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
SQL for Data Retrieval. Review Questions of Previous Class Q1. Show the sum of hours worked for project with ID 1200 (use ASSIGNMENT table) – Use “SUM”
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
STRUCTURED QUERY LANGUAGE SQL-II IST 210 Organization of Data IST210 1.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
STRUCTURED QUERY LANGUAGE SQL-III IST 210 Organization of Data IST210 1.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
SQL for Data Retrieval. Save your SQL Scripts When working with SQL Management Studio, you should keep saving your scripts as a.sql file to somewhere.
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
1 CS 430 Database Theory Winter 2005 Lecture 10: Introduction to SQL.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
PHP AND SQL SERVER: QUERIES IST 210: Organization of Data IST210 1.
MIDTERM REVIEW IST 210 Organization of Data IST210 1.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
Understand Data Definition Language (DDL) Database Administration Fundamentals LESSON 1.4.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
Structured Query Language
CS 3630 Database Design and Implementation
ISQS 3358, Business Intelligence Database vs. Data Warehouse
SQL: Schema Definition and Constraints Chapter 6 week 6
The Basics of Data Manipulation
ISQS 6339, Business Intelligence Database vs. Data Warehouse
Data Definition and Data Types
The Basics of Data Manipulation
Manipulating Data.
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Session - 6 Sequence - 1 SQL: The Structured Query Language:
SQL (Structured Query Language)
Presentation transcript:

Structured Query Language SQL-I IST 210 Organization of Data IST2101

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

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 IST2103

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] IST2104

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 IST2105

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

Running Example IST2107

Running Example (cont.) IST2108

Create DEPARTMENT IST2109 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

Data Types IST21010

Create EMPLOYEE Table IST21011 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 Default value for Department Varchar(100) and Char(100) both defines a string with length up to 35 Varchar(35) the storage is the actual length Char(35) the storage is fixed 35

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 IST21012

UPDATE CASCADE/NO ACTION IST21013 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

DELETE CASCADE/NO ACTION IST21014 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!!!

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: Windows Authentication –Alternate Authentication (connect to the same database): SQL Server Authentication Get your SQL account: Hit “Connect” Navigate to your own database under the Databases folder (IMPORTANT!!!) –Your database name is your PSU ID IST21015

Create a Table in SQL Server Click “New Query” on the upper-left corner Copy & Paste script in the next slide Click “Execute” IST21016 Your PSU ID

IST21017 CREATE TABLE DEPARTMENT( DepartmentNameChar(35)NOT NULLPRIMARY KEY, BudgetCodeChar(30)NOT NULL, OfficeNumberChar(15)NOT NULL, PhoneChar(12)NOT NULL ); View the Result Right click Tables Click “Refresh” You should see “Department” table

IST21018 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, 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

In-class exercise: Create PROJECT Table IST21019 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.: Set MaxHours as 100 by default Use DateTime for StartDate and EndDate Make Update Cascade and Delete No Action

Create Assignment Table IST21020 A composite primary key

IST21021 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 );

Order to Create Tables IST21022 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

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

Insert Data to Department Table IST21024 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', ' ');

View the Result (Software) IST21025

Insert Data to Employee Table IST21026 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', ' ',

Insert Data to Employee Table IST21027 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', INSERT INTO EMPLOYEE VALUES('James', 'Nestor', 'InfoSystems', NULL, OR

Insert Data to Employee Table IST21028 INSERT INTO EMPLOYEE VALUES( 'Rosalie', 'Jackson', 'Administration', ' ', INSERT INTO EMPLOYEE VALUES( 'Richard', 'Bandalone', 'Legal', ' ', INSERT INTO EMPLOYEE VALUES( 'Tom', 'Caruthers', 'Accounting', ' ', INSERT INTO EMPLOYEE VALUES( 'Heather', 'Jones', 'Accounting', ' ', INSERT INTO EMPLOYEE VALUES( 'Mary', 'Abernathy', 'Finance', ' ', INSERT INTO EMPLOYEE VALUES( 'George', 'Smith', 'Human Resources', ' ', INSERT INTO EMPLOYEE VALUES( 'Tom', 'Jackson', 'Production', ' ', INSERT INTO EMPLOYEE VALUES( 'George', 'Jones', 'Production', ' ', INSERT INTO EMPLOYEE VALUES( 'Ken', 'Numoto', 'Marketing', ' ', INSERT INTO EMPLOYEE VALUES( 'Rick', 'Brown', 'InfoSystems', ' ',

In-Class Exercise: Insert Data to Project Table IST21029

Drop Tables IST21030 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

Edit Data (Software) IST21031 See the effects of DELETE/UPDATE CASCADE/NO ACTION

Delete Data (Software) IST21032 See the effects of DELETE/UPDATE CASCADE/NO ACTION

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

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) Requires SQL programming, so start early! IST21034