Structured Query Language

Slides:



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

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
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)
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 COS 346 Day 11.
Structured Query Language Part II Chapter Three CIS 218.
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.
Structured Query Language Part I Chapter Three CIS 218.
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 Part 2 KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall.
Structured Query Language
1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu
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 – Structured Query Language CIS 324 – Chapter 5.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
Database Basics CPSC 4670/ Purpose of a Database The purpose of a database is to keep track of things Unlike a list or spreadsheet, a database.
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.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
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.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
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.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
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.
+ Midterm Review. + Notes from the Midterm When updating, deleting records Make sure you have a WHERE statement that only will give you the row(s) you.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Chapter 3: Relational Databases
+ Structured Query Language Part 2 KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall.
Introduction to Database SEM I, AY Department of Information Technology Salalah College of Technology Chapter No.3 SQL.
Structured Query Language SQL-I IST 210 Organization of Data IST2101.
CHAPTER 7 DATABASE ACCESS THROUGH WEB
COP Introduction to Database Structures
SQL: Schema Definition and Constraints Chapter 6 week 6
Insert, Update and the rest…
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
SQL 101.
The Relational Model Relational Data Model
لغة قواعد البيانات STRUCTURED QUERY LANGUAGE SQL))
Chapter 2 Views.
مقدمة في قواعد البيانات
Structured Query Language
Oracle Data Definition Language (DDL)
Chapter 2 Views.
SQL-1 Week 8-9.
Database Processing: David M. Kroenke’s Chapter Seven:
Session - 6 Sequence - 1 SQL: The Structured Query Language:
IST 318 Database Administration
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Structured Query Language Path from Unorganized to Organized….
DATABASE Purpose of database
Database SQL.
Presentation transcript:

Structured Query Language Chapter Three Structured Query Language Part 1 – Create Tables

Chapter Objectives Learn basic SQL statements for creating database structures Learn basic SQL SELECT statements and options for processing a single table Learn basic SQL SELECT statements for processing multiple tables with subqueries Learn basic SQL SELECT statements for processing multiple tables with joins Learn SQL statements to add, modify, and delete data

Structured Query Language Acronym: SQL Pronounced as “S-Q-L” Also pronounced as “Sequel” Originally developed by IBM as the SEQUEL language in the 1970s SQL-92 is an ANSI national standard adopted in 1992

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]

Sample DB details PROJECT (ProjectID, Name, Department, MaxHours) EMPLOYEE (EmployeeNumber, Name, Phone, Department) ASSIGNMENT (ProjectID, EmployeeNum, HoursWorked)

Referential Integrity Constraints for Sample DB ProjectID in ASSIGNMENT must exist in ProjectID in PROJECT EmployeeNum in ASSIGNMENT must exist in EmployeeNumber in EMPLOYEE

Business Rules for Sample DB If a PROJECT row is deleted, then all of the ASSIGNMENT rows that are connected to the delete PROJECT row also will be deleted. If an EMPLOYEE row is deleted and that row is connected to any ASSIGNMENT, the EMPLOYEE row deletion will be disallowed. (It must first be re-assigned to another employee).

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

SQL for Data Definition: CREATE Creating database tables The SQL CREATE TABLE statement CREATE TABLE Employee( EmpID Integer Primary Key, EmpName Char(25) Not Null );

SQL for Data Definition: CREATE In general… CREATE TABLE newtablename ( three-part column description, optional table constraints … );

SQL for Data Definition: CREATE Column definition includes: Column Name Column data type Optional constraint: Primary Key, Not Null, Null, Default

SQL for Data Definition: CREATE with CONSTRAINT Creating database tables with PRIMARY KEY constraints The SQL CREATE TABLE statement The SQL CONSTRAINT keyword CREATE TABLE Employee( EmpID Integer Not Null, EmpName Char(25) Not Null CONSTRAINT EmpPK PRIMARY KEY (EmpID) );

SQL for Data Definition: CREATE with CONSTRAINT Creating database tables with composite primary keys using PRIMARY KEY constraints The SQL CREATE TABLE statement The SQL CONSTRAINT keyword CREATE TABLE Emp_Skill ( EmpID Integer Not Null, SkillID Integer Not Null, SkillLevel Integer, CONSTRAINT EmpSkillPK PRIMARY KEY (EmpID, SkillID) );

SQL for Data Definition: CREATE with CONSTRAINT Creating database tables using PRIMARY KEY and FOREIGN KEY constraints The SQL CREATE TABLE statement The SQL CONSTRAINT keyword CREATE TABLE Emp_Skill ( EmpID Integer Not Null, SkillID Integer Not Null, SkillLevel Integer, CONSTRAINT EmpSkillPK PRIMARY KEY (EmpID, SkillID), CONSTRAINT EmpFK FOREIGN KEY (EmpID) REFERENCES Employee (EmpID), CONSTRAINT SkillFK FOREIGN KEY (SkillID) REFERENCES Skill (SkillID) );

SQL for Data Definition: CREATE with CONSTRAINT Creating database tables using PRIMARY KEY and FOREIGN KEY constraints The SQL CREATE TABLE statement The SQL CONSTRAINT keyword ON UPDATE CASCADE and ON DELETE CASCADE CREATE TABLE Emp_Skill ( EmpID Integer Not Null, SkillID Integer Not Null, SkillLevel Integer, CONSTRAINT EmpSkillPK PRIMARY KEY (EmpID, SkillID), CONSTRAINT EmpFK FOREIGN KEY (EmpID) REFERENCES Employee (EmpID) ON DELETE CASCADE, CONSTRAINT SkillFK FOREIGN KEY (SkillID) REFERENCES Skill (SkillID) ON UPDATE CASCADE );

Primary Key Constraint: ALTER Adding primary key constraints to an existing table Note: ALTER not covered in Chapter 3 The SQL ALTER statement ALTER TABLE Employee ADD CONSTRAINT EmpPK PRIMARY KEY(EmpID);

Composite Primary Key Constraints: ALTER Adding a composite primary key constraint to an existing table The SQL ALTER statement ALTER TABLE Emp_Skill ADD CONSTRAINT EmpSkillPK PRIMARY KEY(EmpID, SkillID);

Foreign Key Constraint: ALTER Adding foreign key constraints to an existing table The SQL ALTER statement ALTER TABLE Employee ADD CONSTRAINT EmpFK FOREIGN KEY (DeptID) REFERENCES Department (DeptID);

Defining the Cascade Rules: ALTER Adding referential integrity rules ON UPDATE CASCADE ON DELETE CASCADE The SQL ALTER statement ALTER TABLE Employee ADD CONSTRAINT EmpFK FOREIGN KEY (DeptID) REFERENCES Department (DeptID) ON DELETE CASCADE;

Deleting Database Objects: DROP To remove unwanted database objects from the database, use the SQL DROP statement Warning… The DROP statement will permanently remove the object and all data DROP TABLE Employee;

Removing a Constraint: ALTER & DROP To change the constraints on existing tables, you may need to remove the existing constraints before adding new constraints ALTER TABLE Employee DROP CONSTRAINT EmpFK;