1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu www.cs.usna.edu/~adina.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
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.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
The Relational Model Class 2 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) (From ER to Relational)
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 COS 346 Day 11.
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 2-1 David M. Kroenke’s Chapter Two: Introduction to Structured Query.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
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 Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
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,
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
The Relational Model These slides are based on the slides of your text book.
1 IT420: Database Management and Organization SQL - Data Manipulation Language 27 January 2006 Adina Crăiniceanu
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
Database. Basic Definitions Database: A collection of related data. Database Management System (DBMS): A software package/ system to facilitate the creation.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
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: 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.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
The Relational Model Content based on Chapter 3 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
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,
CMPT 258 Database Systems The Relationship Model (Chapter 3)
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 SY306 Web and Databases for Cyber Operations SQL: Structured Query Language.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
Introduction to Database SEM I, AY Department of Information Technology Salalah College of Technology Chapter No.3 SQL.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Understand Data Definition Language (DDL) Database Administration Fundamentals LESSON 1.4.
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Structured Query Language
COP Introduction to Database Structures
SQL: Schema Definition and Constraints Chapter 6 week 6
The Basics of Data Manipulation
Insert, Update and the rest…
The Relational Model Relational Data Model
The Basics of Data Manipulation
CMPT 354: Database System I
SQL-1 Week 8-9.
Database Processing: David M. Kroenke’s Chapter Seven:
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Presentation transcript:

1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu

2 Goals  SQL

3 Relational Query Languages  A major strength of the relational model:  supports simple, powerful querying of data  Queries can be written intuitively, and the DBMS is responsible for efficient evaluation.  The key: precise semantics for relational queries  Allows the optimizer to extensively re-order operations, and still ensure that the answer does not change

4 SQL: The Structured Query Language  Developed by IBM (system R) in the 1970s  Need for a standard since it is used by many vendors  Standards:  SQL-86  SQL-89 (minor revision)  SQL-92 (major revision)  SQL-99 (major extensions, current standard)

5 SQL as a Data Sublanguage  SQL is not a full featured programming language as are C, C#, and Java  SQL is a data sublanguage  Create and process database data  SQL is ubiquitous in enterprise-class DBMS products  SQL programming is a critical skill

6 SQL DDL and DML  SQL statements can be divided into two categories:  Data definition language (DDL) statements  Used for creating and modifying tables, views, and other structures  CREATE, DROP, ALTER  Data manipulation language (DML) statements.  Used for queries and data modification  INSERT, DELETE, UPDATE, SELECT

7 SQL DDL Statements  CREATE  ALTER  DROP

8 CREATE TABLE  CREATE TABLE statement is used for creating relations  Each column is described with three parts:  column name  data type  optional constraints

9 CREATE TABLE Example CREATE TABLE Students (StudentNumber integer, StudentLastName char(18) NOT NULL, StudentFirstName char(18) NOT NULL, char(50), PhoneNumber char(18) ) Student Number Student LastName Student FirstName PhoneNumber

10 Constraints  Constraints can be defined within the CREATE TABLE statement, or they can be added to the table after it is created using the ALTER table statement  Five types of constraints:  NULL/NOT NULL  PRIMARY KEY may not have null values  UNIQUE may have null values  CHECK  FOREIGN KEY

11 Constraints Examples CREATE TABLE Students (StudentNumber integer, StudentLastName char(18) NOT NULL, StudentFirstName char(18) NOT NULL, char(50), PhoneNumber char(18), PRIMARY KEY (StudentNumber), UNIQUE ( ) ) Student Number Student LastName Student FirstName PhoneNumber

12 Default Values and Data Constraints  Students table  Default value for PhoneNumber:  like CREATE TABLE Students (StudentNumber integer, StudentLastName char(18) NOT NULL, StudentFirstName char(18) NOT NULL, char(50), PhoneNumber char(18) DEFAULT “ ”, PRIMARY KEY (StudentNumber), UNIQUE( ), CHECK ( LIKE ) Syntax depends on DBMS!!!

13 FOREIGN KEY Constraints Student Number Student LastName Student FirstName PhoneNumberMajorDepartmentName Science DepartmentNamePhoneBuildingRoom Mathematics Michelson Hall308 History Sampson Hall120 Computer Science Michelson Hall340

14 FOREIGN KEY Constraints Student Number Student LastName Student FirstName PhoneNumberMajorDepartmentName Science DepartmentNamePhoneBuildingRoom Mathematics Michelson Hall308 History Sampson Hall120 Computer Science Michelson Hall340 CREATE TABLE Departments (DepartmentName char(18), Phone char(18) NOT NULL, Building char(18), Room integer, PRIMARY KEY (DepartmentName) )

15 FOREIGN KEY Constraints CREATE TABLE Students (StudentNumber integer, StudentLastName char(18) NOT NULL, StudentFirstName char(18) NOT NULL, char(50), PhoneNumber char(18), MajorDepartmentName char(18), PRIMARY KEY (StudentNumber), UNIQUE( ), FOREIGN KEY (MajorDepartmentName) REFERENCES Departments (DepartmentName) ON DELETE SET NULL ON UPDATE CASCADE )  SQL/92 and SQL:1999 support all 4 options on deletes and updates.  Default is NO ACTION (delete/update is rejected)  CASCADE (also delete all rows that refer to deleted row)  SET NULL / SET DEFAULT (sets foreign key value of referencing row)

16 Implementing Cardinalities

17 ALTER Statement  ALTER statement changes  table structure,  properties, or  constraints after the table has been created

18 Adding and Dropping Columns  The following statement will add a column named BirthDate to the Students table: ALTER TABLE Students ADD COLUMN BirthDate Datetime NULL;  You can drop an existing column with the statement: ALTER TABLE Students DROP COLUMN BirthDate ;

19 Adding and Dropping Constraints  ALTER can be used to add a constraint as follows: ALTER TABLE Student ADD CONSTRAINT DepartmentFK FOREIGN KEY (MajorDepartmentName) REFERENCES Departments (DepartmentName) ON DELETE NO ACTION ON UPDATE CASCADE  ALTER can be used to drop a constraint: ALTER TABLE Student DROP CONSTRAINT DepartmentFK;

20 Removing Tables  SQL DROP TABLE: DROP TABLE Departments;  If there are constraints : ALTER TABLE Students DROP CONSTRAINT DepartmentFK; DROP TABLE Departments;

21 Class Exercise

22 SQL DDL and DML  Data definition language (DDL) statements  Used for creating and modifying tables, views, and other structures  CREATE, ALTER, DROP  Data manipulation language (DML) statements.  Used for queries and data modification  INSERT, DELETE, UPDATE, SELECT

23 INSERT Statement  INSERT command: INSERT INTO Students (StudentNumber, StudentLastName, StudentFirstName) VALUES (190, ‘Smith', ‘John’); INSERT INTO Students VALUES(190, ‘Smith’, ‘John’, ‘ ’)  Bulk INSERT: INSERT INTO Students (StudentNumber, StudentLastName, StudentFirstName, , PhoneNumber) SELECT * FROM Second_Class_Students;

24 UPDATE Statement  UPDATE command: UPDATE Students SET PhoneNumber = ‘ ’ WHERE StudentNumber = 673;  BULK UPDATE command: UPDATE Students SET PhoneNumber = ‘ ’ WHERE StudentLAstName = ‘Doe’; Student Number Student LastName Student FirstName PhoneNumber

25 DELETE Statement  DELETE command: DELETE FROM Students WHERE StudentNumber = 190;  If you omit the WHERE clause, you will delete every row in the table!

26 The SQL SELECT Statement  The fundamental framework for SQL query statement is the SQL SELECT statement:  SELECT{ColumnName(s)}  FROM{TableName(s)}  WHERE{Conditions}

27 Specific Columns on One Table SELECTStudentNumber,StudentLastName FROM Students; StudentNumberStudentLastName 190Smith 673Doe 312Doe

28 Specify Column Order SELECT StudentLastName,StudentNumber FROM Students; StudentLastNameStudentNumber Smith190 Doe673 Doe312

29 The DISTINCT Keyword SELECT StudentLastName FROM Students; StudentLastName Smith Doe SELECT DISTINCT StudentLastName FROM Students; StudentLastName Smith Doe

30 Selecting All Columns: The Asterisk (*) Keyword SELECT* FROMStudents; Student Number Student LastName Student FirstName PhoneNumber

31 Specific Rows from One Table SELECT* FROMStudents WHERE StudentLastName = ‘Doe'; NOTE:SQL wants a plain ASCII single quote: ' NOT ‘ ! Student Number Student LastName Student FirstName PhoneNumber

32 Specific Columns and Rows from One Table SELECTStudentNumber, StudentLastName, StudentFirstName FROMStudents WHEREPhoneNumber NOT NULL; Student Number Student LastName Student FirstName 190SmithJohn 312DoeBob