Structured Query Language (SQL) DDL

Slides:



Advertisements
Similar presentations
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.
Advertisements

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. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Spring 2008 Project 1, Lecture 3 Slides adapted from those used by Jeffrey Ullman, via Jennifer.
Database Management System LICT 3011 Eyad H. Elshami.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
CSE314 Database Systems Lecture 4 Basic SQL Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
Data Modeling and Database Design
Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Ms. Hatoon Al-Sagri CCIS – IS Department SQL-99 :Schema Definition, Constraints, Queries, and Views 1.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Chapter 5: Part 1: DDL STRUCTURED QUERY LANGUAGE (SQL)
1 Creating and Modifying Database Objects. 2 An Oracle database consists of multiple user accounts Each user account owns database objects Tables Views.
SQL: DDL John Ortiz Cs.utsa.edu.
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.
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 Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Visual Programing SQL Overview Section 1.
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.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
1 CS 430 Database Theory Winter 2005 Lecture 10: Introduction to SQL.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Fundamentals of DBMS Notes-1.
Chapter 10 SQL DDL.
Fundamental of Database Systems
Structured Query Language (SQL) DML
More SQL: Complex Queries,
Plan for Intro to DB Systems I
Chapter 5 Introduction to SQL.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
Oracle & SQL Introduction
CS 480: Database Systems Lecture 13 February 13,2013.
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
ORACLE SQL Developer & SQLPLUS Statements
DATABASE MANAGEMENT SYSTEM
STRUCTURED QUERY LANGUAGE
Instructor: Mohamed Eltabakh
لغة قواعد البيانات STRUCTURED QUERY LANGUAGE SQL))
CS4222 Principles of Database System
SQL OVERVIEW DEFINING A SCHEMA
SQL data definition using Oracle
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CMPT 354: Database System I
Oracle Data Definition Language (DDL)
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Chapter 2: Creating And Modifying Database Tables
Instructor: Mohamed Eltabakh
IST 318 Database Administration
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Structured Query Language Path from Unorganized to Organized….
SQL (Structured Query Language)
Presentation transcript:

Structured Query Language (SQL) DDL SWEN 304 Trimester 2, 2017 Slides by: Pavle Mogin & Hui Ma 1

Outline Data Definition Language Reading: Chapter 8 of the textbook Database schema definition Domain definition Table definition Constraints Schema Evolution Reading: Chapter 8 of the textbook SWEN304 Lect6: SQL(1) 2 2 2

Structured Query Language (SQL) We are concerned with three major problems, namely: How to define a database schema and its relation schemas? How to store and manipulate data in relations? How to retrieve data from a database? SQL is a standardised language used in almost all relational database systems developed at IBM and later defined as a standard by ANSI and ISO SQL stands for Structured Query Language (some also say SEQUEL for Structured English QUEry Language) versions in use: SQL-86, SQL-92, SQL:1999 (also known as SQL-3, the ANSI and ISO standard), SQL:2003, SQL:2006, SQL:2008, SQL:2011 SWEN304 Lect6: SQL(1) 3 3 3

Structured Query Language (SQL) We will discuss three different aspects of SQL, namely the usage of SQL as a Data Definition Language (DDL) a Data Manipulation Language (DML) a Query Language (QL) SQL provides some further features like the definition of external views (View Definition Language (VDL) on the database, authorisation and access rights, and coupling with programming Declarative and set oriented SWEN304 Lect6: SQL(1) 4 4 4

SQL as Data Definition Language Catalog Schemas (Relational database schemas) Domains Tables (Relation schemas) Constraints Modify schemas (schema evolution) SWEN304 Lect6: SQL(1) 5 5 5

Databases and Catalogs in DBMS A DBMS creates a catalog for every database that it manages The catalog records essential information about this database This information is also called meta data of the database The catalog is used by DBMS modules like the DDL compiler module, the query optimisation module, or the authorisation and security module, etc. The catalog includes in particular the following: Schema table: information on all relation schemas Attribute table: information on all attributes Data types table: information on all data types (domains) Constraints table: information on all constraints SWEN304 Lect6: SQL(1) 6 6 6

Databases and Catalogs in DBMS Other information recorded in the catalog: Descriptions of views (external schemas) and their queries Descriptions of storage structures and indexes to enable fast access to the data in the database Security and authorization information that records the owner of each relation and describes each user’s privileges to access specific relations or views of the database Catalogs and SQL: In a relational DBMS, the catalog is stored in form of tables This allows the DBMS software (and authorised users such as DBAs) to access and manipulate the catalog using SQL SQL’92 defines a standard format for the catalog, called the information schema The precise layout of the catalog differs from DBMS to DBMS SWEN304 Lect6: SQL(1) 7 7 7

Defining a Schema A RDB Schema describes that part of a database used by one “application” and its users CREATE SCHEMA UNIVERSITY AUTHORIZATION huima; Followed by definitions of the parts of the database schema: domains, tables, constraints, views, authorization grants Use CREATE SCHEMA only in the case you need more than one schema in the same database SWEN304 Lect6: SQL(1) 8 8 8

Notational Conventions We shall use BNF notation to describe SQL syntax: Non terminal symbols are shown in angled brackets 〈data_type〉, Optional parts shown in square brackets [DEFAULT 〈value〉], Alternatives shown in parentheses separated by bars (FULL | PARTIAL) Lists or repeated elements shown in braces {〈constraint_declaration〉,… } Note: In SQL, names are not case-sensitive SWEN304 Lect6: SQL(1) 9 9 9

Domain Definition Syntax CREATE DOMAIN 〈domain_name〉 [AS] 〈data_type〉 [DEFAULT 〈value〉] [{CONSTRAINT 〈name〉 〈constraint〉 , …}] 〈data_type〉 :: Numeric: INT , REAL, DECIMAL(d, p) Character-string: CHAR(n), VARCHAR(n) Bit-string: BIT(n), BIT VARYING(n) Date: DATE(format) Time: TIME(format) SWEN304 Lect6: SQL(1) 10 10 10

Domain Example CREATE DOMAIN idno AS INT DEFAULT 300001 NOT NULL CONSTRAINT idnoconstr CHECK (VALUE > 300000 AND VALUE <= 399999); SWEN304 Lect6: SQL(1) 11 11 11

Base Table (Relation Schema) Definition CREATE TABLE 〈table_name〉( {〈attribute_declaration〉, …} [, { [ CONSTRAINT〈name〉]〈table_constraint〉,… } ] ); 〈attribute_declaration〉 :: 〈attribute_name〉 (〈data_type〉[(max_length)] | 〈domain_name〉) [DEFAULT (〈value〉 | 〈function〉 | NULL) ] [ { [CONSTRAINT〈name〉] 〈att_constraint〉, …} ] SWEN304 Lect6: SQL(1) 12 12 12

Attribute Constraint Declarations 〈att_constraint〉 :: NOT NULL | (PRIMARY KEY | UNIQUE) | REFERENCES 〈referenced_table_name〉 [〈referenced_table_attribute〉] [ON DELETE (NO ACTION | CASCADE | SET NULL | SET DEFAULT )] [ON UPDATE (NO ACTION | CASCADE | SET NULL | SET DEFAULT )] [MATCH FULL | MATCH PARTIAL] | CHECK (〈conditional_expression〉) Examples: http://www.postgresql.org/docs/8.0/interactive/ddl-constraints.html SWEN304 Lect6: SQL(1) 13 13 13

Table Constraints Declarations PRIMARY KEY (〈attribute_list〉) | UNIQUE (〈attribute_list〉) | FOREIGN KEY 〈attribute_list〉 REFERENCES 〈referenced_table_name〉 [〈referenced_table_attribute_list〉] [MATCH (FULL | PARTIAL)] [ON DELETE (NO ACTION | CASCADE | SET NULL | SET DEFAULT )] [ON UPDATE (NO ACTION | CASCADE | SET NULL | SET DEFAULT )] CHECK 〈conditional_expression〉 SWEN304 Lect6: SQL(1) 14 14 14

UNIVERSITY Database UNIVERSITY ={STUDENT(StudId, Lname, Fname, Major), COURSE(CourId, Cname, Points, Dept), GRADES(StudId, CourId, Grade)} SWEN304 Lect6: SQL(1) 15 15 15

University Database Schema: COURSE CREATE TABLE COURSE ( CourId CHAR(7) CONSTRAINT cspk PRIMARY KEY, CName CHAR(15) NOT NULL, Points INT NOT NULL CONSTRAINT pointschk CHECK (Points >= 0 AND Points <= 50), Dept CHAR(25) ); SWEN304 Lect6: SQL(1) 16 16 16

University Database Schema: STUDENT Without using idno DOMAIN: CREATE TABLE STUDENT ( StudId INT NOT NULL DEFAULT 30000 CONSTRAINT stpk PRIMARY KEY CONSTRAINT StIdRange CHECK (StudId BETWEEN 300000 AND 399999), LName CHAR(15) NOT NULL, FName CHAR(15) NOT NULL, Major CHAR(25) DEFAULT ‘Comp’ ); SWEN304 Lect6: SQL(1) 17 17 17

University Database Schema: STUDENT Using idno DOMAIN: CREATE TABLE STUDENT ( StudId idno CONSTRAINT stpk PRIMARY KEY, LName CHAR(15) NOT NULL, FName CHAR(15) NOT NULL, Major CHAR(25) ); SWEN304 Lect6: SQL(1) 18 18 18

A Question for You SQL allows defining only one relation schema key – PRIMARY KEY Suppose A is defined as a primary key Can we define B as another key (not primary one)? Answers: ? Yes. Define B as UNIQUE and NOT NULL CREATE TABLE STAFF ( staff_id INT PRIMARY KEY, ird_number CHAR(7) NOT NULL UNIQUE, address VARCHAR(255) ); SWEN304 Lect6: SQL(1) 19 19 19

Example UNIQUE Constraints CREATE TABLE PERSONBS( Id INT PRIMARY KEY, LastName VARCHAR(255) NOT NULL, FirstName VARCHAR(255), Address VARCHAR(255), DoB DATE NOT NULL, CONSTRAINT uc_Person UNIQUE (LastName, DoB) ); SWEN304 Lect6: SQL(1) 20 20 20

Conditional Expressions: CHECK (Ref) Conditional expression of the CHECK clause can be any plausible combination of the following: [A  a] [A  B ] [A [NOT] BETWEEN a1 AND a2 ] [A [NOT] LIKE 〈pattern〉] [A [NOT] SIMILAR TO 〈regular expression〉 ] [A [NOT] IN 〈value_list〉 ] [A  ANY 〈value_list〉] [A  SOME 〈value_list〉] [A  ALL 〈value_list〉] Combined with AND or OR or NOT where   { =, <, <=, >, >=, < > }, A and B attributes or functions of attributes, ai  dom (A ), SWEN304 Lect6: SQL(1) 21 21 21

University Database Schema: GRADES CREATE TABLE GRADES ( StudId INT NOT NULL CONSTRAINT Gstidrange CHECK (StudId BETWEEN 300000 and 399999), CONSTRAINT gsri REFERENCES STUDENT ON DELETE CASCADE, CourId CHAR(8) NOT NULL CONSTRAINT gpri REFERENCES COURSE ON DELETE NO ACTION, Grade CHAR(2) CONSTRAINT grd CHECK (Grade IN ('A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', NULL)), CONSTRAINT gpk PRIMARY KEY (StudId, CourId ) ); SWEN304 Lect6: SQL(1) 22 22 22

CHECK: refer to several columns A CHECK constraint can also refer to several columns CREATE TABLE BOOK ( book_no INT PRIMARY KEY, title VARCHAR(30) NOT NULL, price NUMERIC CHECK (price > 0), discounted_price NUMERIC CHECK (discounted_price > 0), CHECK (price > discounted_price) ); SWEN304 Lect6: SQL(1) 23 23 23

Modifying a Schema: DROP DROP〈construct〉〈construct_name〉〈drop_behavior〉 〈construct〉::= (SCHEMA | TABLE | DOMAIN) 〈drop behavior〉::= (CASCADE | RESTRICT ) Generally: CASCADE behavior means deleting the construct itself and all the other constructs related to it, RESTRICT behavior means that the construct will be deleted only if it is empty (schema), or not referenced by any other construct (like: table, attribute, view) e.g. DROP TABLE COURSE RESTRICT DROP TABLE STUDENTS CASCADE SWEN304 Lect6: SQL(1) 24 24 24

Modifying a Schema: ALTER ALTER TABLE 〈table_name〉 … allows you to modify a table after it has been defined e.g. ALTER TABLE STUDENT ADD NoOfPoints INT DEFAULT 320; ALTER TABLE GRADES ALTER Grade SET DEFAULT ‘C'; ALTER TABLE GRADES DROP CONSTRAINT gsri ; ALTER TABLE GRADES ADD CONSTRAINT gsfk FOREIGN KEY StudId REFERENCES STUDENT ON DELETE NO ACTION; ALTER TABLE STUDENT DROP CONSTRAINT stpk CASCADE; SWEN304 Lect6: SQL(1) 25 25 25

Summary Structured Query Language (DDL, DML, QL and VDL) Data Definition Language to create schema constructs (CREATE SCHEMA, CREATE DOMAIN, CREATE TABLE) CREATE TABLE command is used to define relation schema attributes and constraints Attribute constraints Table constraints Schema evolution commands allow altering attributes and constraints SWEN304 Lect6: SQL(1) 26 26 26

Next Lecture SQL DML Single table queries Multiple table queries Nested queries Aggregate functions SWEN304 Lect6: SQL(1) 27 27