Structured Query Language (SQL)

Slides:



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

MySQL. To start go to Login details: login: labuser password:macimd15 – There.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Beginning SQL Tutorial Author Jay Mussan-Levy. What is SQL?  Structured Query Language  Communicate with databases  Used to created and edit databases.
Structured Query Language Chapter Three Part 3 – Inserts, Updates, Deletes.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
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.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
Introduction to 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.
PHP and SQL Server: Queries IST2101. Project Report 4 SQL Queries Due Sunday, 4/5 at 11:59pm Instructions on how to access team webspace and SQL database.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Download three SQL script files from wiki page.
Oracle Data Definition Language (DDL)
1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu
1 Working with MS SQL Server. 2 Objectives You will be able to Use Visual Studio for GUI based interactive access to a Microsoft SQL Server database.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Working with Queries in Microsoft Access The Access Workbench: Section Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 3 rd Edition.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Creating Databases with MySQL Workbench Build the Forums database in Ullman’s Chapter 6.
Databases in Visual Studio. Database in VisualStudio An MS SQL database are built in Visual studio The Name can be something like ”(localdb)\Projects”
1 Working with MS SQL Server Textbook Chapter 14.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
Introduction to MySQL Lab no. 10 Advance Database Management System.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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.
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
Chapter 5 MYSQL Database. Introduction to MYSQL MySQL is the world's most popular open-source database. Open source means that the source code, the programming.
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.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is.
Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.
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.
4a. Structured Query Language - SELECT Statement Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets.
STRUCTURED QUERY LANGUAGE SQL-III IST 210 Organization of Data IST210 1.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
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.
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.
SQL-5 In-Class Exercise Answer IST 210 Organization of Data IST2101.
Agenda for Class 2/20/2014 Introduce Microsoft’s SQL Server database management system. Use the lab to discuss how to CREATE, DROP and populate (INSERT)
1 SY306: Web and Databases for Cyber Operations Slide Set: 11 Databases - Relational Model.
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
PHP AND SQL SERVER: QUERIES IST 210: Organization of Data IST210 1.
Installation Oracle 11g Express 2 double click the "setup" button to install the Oracle.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
Software-Projekt 2008 Seminarvortrag“Short tutorial of MySql“ Wei Chen Verena Honsel.
Structured Query Language SQL-I IST 210 Organization of Data IST2101.
 MySQL is a database system used on the web  MySQL is a database system that runs on a server  MySQL is ideal for both small and large applications.
Structured Query Language
CS320 Web and Internet Programming SQL and MySQL
ISQS 3358, Business Intelligence Database vs. Data Warehouse
SQL: Schema Definition and Constraints Chapter 6 week 6
Insert, Update and the rest…
ISQS 6339, Business Intelligence Database vs. Data Warehouse
Data Definition and Data Types
Using SQL Server through Command Prompt
ORACLE SQL Developer & SQLPLUS Statements
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
SQL Views Appendix C DATABASE CONCEPTS, 3rd Edition
CS3220 Web and Internet Programming SQL and MySQL
CS3220 Web and Internet Programming SQL and MySQL
Presentation transcript:

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;