SQL Data Definition Language

Similar documents
SQL Data Query Language

Basic SQL. Basic SQL. Basic SQL

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

2.9 Table Creation. CREATE TABLE TableName ( AttrName AttrType, AttrName AttrType,... )

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

SQL Data Definition and Data Manipulation Languages (DDL and DML)

CS2300: File Structures and Introduction to Database Systems

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

Lecture 07. Spring 2018 Borough of Manhattan Community College

Lab # 2. Data Definition Language (DDL) Eng. Alaa O Shama

SQL: Data Definition Language

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Lab # 4. Data Definition Language (DDL)

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

Unit 1 - Chapter 4,5

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

EXAMINATIONS 2009 END-YEAR. COMP302 / SWEN304 Database Systems / Database System Engineering. Appendix

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

user specifies what is wanted, not how to find it

Database Management Systems,

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

Data Definition Language (DDL)

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

SQL Structured Query Language Introduction

CSCB20 Week 3. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

SQL DATA DEFINITION LANGUAGE

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

SQL Introduction. CS 377: Database Systems

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

1) Introduction to SQL

MTAT Introduction to Databases

COMP302. Database Systems

Database Foundations. 6-3 Data Definition Language (DDL) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Slides by: Ms. Shree Jaswal

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering Fall 2011 ECOM 4113: Database System Lab Eng.

Basic SQL. Dr Paolo Guagliardo. University of Edinburgh. Fall 2016

SQL DATA DEFINITION LANGUAGE

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Principles of Data Management

Constraints. Primary Key Foreign Key General table constraints Domain constraints Assertions Triggers. John Edgar 2

SQL DATA DEFINITION LANGUAGE

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

CSIE30600 Database Systems Basic SQL 2. Outline

Db2 Alter Table Alter Column Set Data Type Char

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Lab # 2 Hands-On. DDL Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

Intro to Database Commands

Tables From Existing Tables

Introduction to SQL Server 2005/2008 and Transact SQL

SQL: The Query Language Part 1. Relational Query Languages

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

SQL Data Definition: Table Creation

COMP 430 Intro. to Database Systems

SQL: DDL. John Ortiz Cs.utsa.edu

Data and Tables. Bok, Jong Soon

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

Draft. Students Table. FName LName StudentID College Year. Justin Ennen Science Senior. Dan Bass Management Junior

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

INFORMATION TECHNOLOGY NOTES

Exploring Popular SQL Implementations. Query the database to retrieve the data stored therein.

Chapter 3: Introduction to SQL

SQL - Subqueries and. Schema. Chapter 3.4 V4.0. Napier University

Information Systems for Engineers Fall Data Definition with SQL

After completing this unit, you should be able to: Define the terms

From theory to practice. Designing Tables for a postgresql Database System. psql. Reminder. A few useful commands

IT360: Applied Database Systems. SQL: Structured Query Language DDL and DML (w/o SELECT) (Chapter 7 in Kroenke) SQL: Data Definition Language

Full file at

CST272 SQL Server, SQL and the SqlDataSource Page 1

12 Rules Defined by Dr.Codd for a Relational Database Management System

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

QQ Group

Exact Numeric Data Types

From theory to practice. Designing Tables for an Oracle Database System. Sqlplus. Sqlplus. Technicalities. Add the following to your.

MySQL Workshop. Scott D. Anderson

Designing Tables for an Oracle Database System. From theory to practice

Relational Model. CSE462 Database Concepts. Demian Lessa. Department of Computer Science and Engineering State University of New York, Buffalo

Data types String data types Numeric data types Date, time, and timestamp data types XML data type Large object data types ROWID data type

Question 1. Relational Data Model and Relational Algebra [26 marks] Question 2. SQL and [26 marks]

The Relational Model of Data (ii)

SQL Server 2008 Tutorial 3: Database Creation

SQL STRUCTURED QUERY LANGUAGE

IBM DB2 UDB V7.1 Family Fundamentals.

CSCC43H: Introduction to Databases. Lecture 4

MySQL Creating a Database Lecture 3

Lecture7: SQL Overview, Oracle Data Type, DDL and Constraints Part #2

Summary Modifications. Database Construction (and Usage) Explicit attribute lists. Insertions with queries. Quiz. Default values

3ISY402 DATABASE SYSTEMS

SQL DDL II. CS121: Relational Databases Fall 2017 Lecture 8

Chapter 3: Introduction to SQL

CS634 Architecture of Database Systems Spring Elizabeth (Betty) O Neil University of Massachusetts at Boston

The Structured Query Language Get Started

Transcription:

SQL Data Definition Language André Restivo 1 / 56

Index Introduction Table Basics Data Types Defaults Constraints Check Not Null Primary Keys Unique Keys Foreign Keys Sequences 2 / 56

Introduction 3 / 56

SQL Structured Query Language. A special purpose language to manage data stored in a relational database. Based on relational algebra. Pronounced Sequel 4 / 56

History Early 70's SEQUEL Developed at IBM 1986 SQL-86 and SQL-87 Ratified by ANSI and ISO. 1989 SQL-89 1992 SQL-92 Also know as SQL2. 1999 SQL:1999 Also known as SQL3 Includes regurlar expressions, recursive queries, triggers, non-scalar data types and some object-oriented expressions. 2003 SQL:2003 XML support and auto-generated values. 2006 SQL:2006 XQuery support. 2008 SQL:2008. 2011 SQL:2011. 5 / 56

Standard Although SQL is an ANSI/ISO standard, every database system implements it in a slightly different way. These slides will try to adhere to the standard as much as possible. Sometimes we'll deviate and talk specifically about PostgreSQL. 6 / 56

Table Basics 7 / 56

Creating Tables The basic structure of a table creation statement in SQL: CREATE TABLE <table_name> ( <column_name> <data_type>, <column_name> <data_type>,... <column_name> <data_type> Values between <> are to be replaced. 8 / 56

Deleting Tables To delete a table we do: DROP TABLE <table_name>; If there are foreign keys referencing the table we must use the cascade keyword: DROP TABLE <table_name> CASCADE; 9 / 56

Data Types PostgreSQL 10 / 56

Numeric The types SMALLINT, INTEGER, and BIGINT store whole numbers. NUMERIC(precision, scale) stores numbers with a very large number of digits. The scale of a numeric data type is the count of decimal digits in the fractional part, to the right of the decimal point. The precision of a numeric is the total count of significant digits in the whole number. The data types REAL and DOUBLE precision are inexact, variable-precision numeric types. 11 / 56

Numeric Precision Precision for numeric types: SMALLINT 2 bytes -32768 to +32767 INTEGER 4 bytes -2147483648 to +2147483647 BIGINT 8 bytes -9223372036854775808 to +9223372036854775807 NUMERIC user-specified numeric(3,1) -99.9 to 99.9 REAL 4 bytes inexact 6 decimal digits precision DOUBLE 8 bytes inexact 15 decimal digits precision 12 / 56

Date / Time The type DATE stores date values in the ISO 8601 format. Example: 1980-12-25. The type TIME stores time values in the ISO 8601 format. Examples: 04:05 or 04:05:06.789 The type TIMESTAMP store time and date values in the ISO 8601 format. Example: 1980-12-25 04:05:06.789. Date/time types can also store timezone information. 13 / 56

Text The type CHARACTER CTER VARYING(n) YING(n), or VARCHAR(n) CHAR(n), stores variable-length text with a user defined limit. The type CHARACTER(n) CTER(n), or CHAR(n), stores fixed-length, blank-padded text. The type TEXT stores variable unlimited length text. VARCHAR, without a limit, is the same as TEXT CHAR, without a limit, is the same as CHAR(1) 14 / 56

Boolean The BOOLEAN type can only have two possible values: true or false. Possible values representing true: TRUE, 't', 'true', 'y', 'yes', 'on' and '1' Possible values representing false: FALSE, 'f', 'false', 'n', 'no', 'off' and '0' 15 / 56

Serial In PostgreSQL there is a pseudo-type that can be used to define auto-generatedated identifiers or auto-counters. To define a column as an auto-counter we use the type SERIAL. 16 / 56

Example CREATE TABLE employee ( id SERIAL, name VARCHAR(128), address VARCHAR(256), birthdate DATE, salary NUMERIC(6,2), taxes NUMERIC(6,2), card_number INTEGER, active BOOLEAN 17 / 56

Defaults 18 / 56

Default Values For each column we can define its default value: CREATE TABLE <table_name> ( <column_name> <data_type> DEFAULT <default_value>, <column_name> <data_type>,... <column_name> <data_type> The default default value is NULL 19 / 56

Example CREATE TABLE employee ( id SERIAL, name VARCHAR(128), address VARCHAR(256), birthdate DATE, salary NUMERIC(6,2), taxes NUMERIC(6,2), card_number INTEGER DEFAULT 0, active BOOLEAN DEFAULT TRUE Card number default value is 0. Employee is active by default. 20 / 56

Constraints 21 / 56

Constraint Types Several types of constraints can be defined using SQL: Check Not Null Unique Keys Primary Keys Foreign Key Constraints can be column based or table based. Column constraints appear in front of the column they are referring to. Table constraints appear as a separateate clause. 22 / 56

Check 23 / 56

Check Constraint Check contraints allows to define a constraint on the column values using an expression that the values must follow: CREATE TABLE <table_name> ( <column_name> <data_type> CHECK <check_expression>, <column_name> <data_type>,... <column_name> <data_type> 24 / 56

Example CREATE TABLE employee ( id SERIAL, name VARCHAR(128), address VARCHAR(256), birthdate DATE, salary NUMERIC(6,2) CHECK (salary > 500), taxes NUMERIC(6,2), card_number INTEGER DEFAULT 0 CHECK (card_number >= 0), active BOOLEAN DEFAULT TRUE Salary must be larger than 500. Card number must be larger or equal to 0. 25 / 56

Constraint Naming Giving names to constraints allows us to better identify them when errors occur or when we want to refer to them. CREATE TABLE <table_name> ( <column_name> <data_type> CONSTRAINT <constraint_name> CHECK <check_expression>, <column_name> <data_type>,... <column_name> <data_type> Naming constraints is optional but is a good practice. 26 / 56

Example CREATE TABLE employee ( id SERIAL, name VARCHAR(128), address VARCHAR(256), birthdate DATE, salary NUMERIC(6,2) CONSTRAINT minimum_wage CHECK (salary > 500), taxes NUMERIC(6,2), card_number INTEGER DEFAULT 0 CHECK (card_number >= 0), active BOOLEAN DEFAULT TRUE 27 / 56

Multiple Column Check If the check constraint refers to more than one column, we must use a table based constraint: CREATE TABLE employee ( id SERIAL, name VARCHAR(128), address VARCHAR(256), birthdate DATE, salary NUMERIC(6,2) CONSTRAINT minimum_wage CHECK (salary > 500), taxes NUMERIC(6,2), card_number INTEGER DEFAULT 0 CHECK (card_number >= 0), active BOOLEAN DEFAULT TRUE, CONSTRAINT taxes_lower_salary CHECK (taxes < salary) Taxes have to be lower than salary. 28 / 56

Not Null 29 / 56

Not Null Constraint We can define that a certain column does not allow NULL values: CREATE TABLE <table_name> ( <column_name> <data_type> NOT NULL, <column_name> <data_type>,... <column_name> <data_type> 30 / 56

Example CREATE TABLE employee ( id SERIAL, name VARCHAR(128) NOT NULL, address VARCHAR(256), birthdate DATE, salary NUMERIC(6,2) CONSTRAINT minimum_wage CHECK (salary > 500), taxes NUMERIC(6,2), card_number INTEGER DEFAULT 0 CHECK (card_number >= 0), active BOOLEAN DEFAULT TRUE, CONSTRAINT taxes_lower_salary CHECK (taxes < salary) Name cannot be null. 31 / 56

Primary Keys 32 / 56

Primary Key Constraints We can define one, and only one, primary key for our table: CREATE TABLE <table_name> ( <column_name> <data_type> PRIMARY KEY, <column_name> <data_type>,... <column_name> <data_type> 33 / 56

Example CREATE TABLE employee ( id SERIAL PRIMARY KEY, name VARCHAR(128) NOT NULL, address VARCHAR(256), birthdate DATE, salary NUMERIC(6,2) CONSTRAINT minimum_wage CHECK (salary > 500), taxes NUMERIC(6,2), card_number INTEGER DEFAULT 0 CHECK (card_number >= 0), active BOOLEAN DEFAULT TRUE, CONSTRAINT taxes_lower_salary CHECK (taxes < salary) Id cannot be null and cannot have repeated values. 34 / 56

Multiple Column Primary Key If a primary key is composed by more than one column, we must use a table based constraint: CREATE TABLE <table_name> ( <column_name> <data_type>, <column_name> <data_type>,... <column_name> <data_type>, PRIMARY KEY (<column_name>, <column_name>) 35 / 56

Example CREATE TABLE telephone ( employee INTEGER, phone VARCHAR, PRIMARY KEY (employee, phone) An employee cannot have the same phone number twice. 36 / 56

Unique Keys 37 / 56

Unique Key Contraints Unique keys are identical to primary keys but: they allow NULL values; and there can be multiple unique keys in one table. They are created using the same type of syntax used in primary keys. 38 / 56

Example CREATE TABLE employee ( id SERIAL PRIMARY KEY, name VARCHAR(128) NOT NULL, address VARCHAR(256), birthdate DATE, salary NUMERIC(6,2) CONSTRAINT minimum_wage CHECK (salary > 500), taxes NUMERIC(6,2), card_number INTEGER UNIQUE DEFAULT 0 CHECK (card_number >= 0), active BOOLEAN DEFAULT TRUE, CONSTRAINT taxes_lower_salary CHECK (taxes < salary) Card keys cannot have repeated values. 39 / 56

Multiple Column Unique Keys If a unique key is composed by more than one column, we must use a table based constraint: CREATE TABLE <table_name> ( <column_name> <data_type>, <column_name> <data_type>,... <column_name> <data_type>, UNIQUE (<column_name>, <column_name>) 40 / 56

Foreign Keys 41 / 56

Foreign Key Constraints We can also declare foreign keys. A foreign key must always reference a key (primary or unique) from another (or the same) table. Databases don't allow columns with a foreign key containing values that do not exist in the referenced column. CREATE TABLE <table_a> ( <column_a> <data_type> PRIMARY KEY, <column_b> <data_type>,... <column_c> <data_type> CREATE TABLE <table_b> ( <column_x> <data_type> PRIMARY KEY, <column_y> <data_type>,... <column_z> <data_type> REFERENCES <table_a>(<column_a>) 42 / 56

Example CREATE TABLE employee ( id SERIAL PRIMARY KEY, name VARCHAR(128) NOT NULL, address VARCHAR(256), birthdate DATE, salary NUMERIC(6,2) CONSTRAINT minimum_wage CHECK (salary > 500), taxes NUMERIC(6,2), card_number INTEGER UNIQUE DEFAULT 0 CHECK (card_number >= 0), active BOOLEAN DEFAULT TRUE, department_id INTEGER REFERENCES department(id), CONSTRAINT taxes_lower_salary CHECK (taxes < salary) The id of the department references the id column in the department table. Employees cannot have a department number that doesn't exist in the department table. 43 / 56

Foreign Key to Primary Key If the referenced column is the primary key of the other table, we can omit the name of the column: CREATE TABLE employee ( id SERIAL PRIMARY KEY, name VARCHAR(128) NOT NULL, address VARCHAR(256), birthdate DATE, salary NUMERIC(6,2) CONSTRAINT minimum_wage CHECK (salary > 500), taxes NUMERIC(6,2), card_number INTEGER UNIQUE DEFAULT 0 CHECK (card_number >= 0), active BOOLEAN DEFAULT TRUE, department_id INTEGER REFERENCES department, CONSTRAINT taxes_lower_salary CHECK (taxes < salary) The id of the department references the primary key in the department table. 44 / 56

Multiple Column Foreign Key If the referenced table has a key with multiple columns, we must use a table based constraint to define our foreign key: CREATE TABLE telephone ( employee INTEGER, phone VARCHAR, PRIMARY KEY (employee, phone) CREATE TABLE call ( id INTEGER PRIMARY KEY, employee INTEGER, phone INTEGER, when DATE, caller INTEGER, FOREIGN KEY (employee, phone) REFERENCES telephone (employee, phone) 45 / 56

Example We can also ommit the referenced columns if they are the primary keys: CREATE TABLE telephone ( employee INTEGER, phone VARCHAR, PRIMARY KEY (employee, phone) CREATE TABLE call ( id INTEGER PRIMARY KEY, employee INTEGER, phone INTEGER, when DATE, caller INTEGER, FOREIGN KEY (employee, phone) REFERENCES telephone 46 / 56

Deleting Referenced Values Declaring a foreign key means that values in one table must also appear in the referenced column. When a line having values referencing it is deleted, three different things can occur: An error is thrown. The referencing values becomes NULL. All referencing lines are deleted (this might cause a cascade effect). 47 / 56

Updating Referenced Values The same problem happens when we update a line that is referenced by another column. When a line having values referencing it is updated, three different things can occur: An error is thrown. The referencing values becomes NULL. All referencing values are updated to the new value (this might cause a cascade effect). 48 / 56

On Delete and On Update To define the desired behavior, we should use the ON DELETE and ON UPDATE clauses with one of three possible values: RESTRICT (throws an error) SET NULL (values become null) CASCADE (lines are deletes or values updated) RESTRICT is the default value. 49 / 56

On Delete and On Update CREATE TABLE <table_a> ( <column_a> <data_type> PRIMARY KEY, <column_b> <data_type>,... <column_c> <data_type> CREATE TABLE <table_b> ( <column_x> <data_type> PRIMARY KEY, <column_y> <data_type>,... <column_z> <data_type> REFERENCES <table_a>(<column_a>) ON DELETE SET NULL ON UPDATE CASCADE 50 / 56

Example CREATE TABLE employee ( id SERIAL PRIMARY KEY, name VARCHAR(128) NOT NULL, address VARCHAR(256), birthdate DATE, salary NUMERIC(6,2) CONSTRAINT minimum_wage CHECK (salary > 500), taxes NUMERIC(6,2), card_number INTEGER UNIQUE DEFAULT 0 CHECK (card_number >= 0), active BOOLEAN DEFAULT TRUE, department_id INTEGER REFERENCES department ON DELETE SET NULL ON UPDATE CASCADE, CONSTRAINT taxes_lower_salary CHECK (taxes < salary) If a department with id 1 is deleted, all employess with department id equal to 1 will start having a null department number. If a department with id 1 is updated to id 2, all employess with department id equal to 1 will start having a department number equal to 2. 51 / 56

Sequences 52 / 56

Sequences A sequence is a special kind of database object designed for generating unique numeric identifiers. They ensure that a different value is generated for every client. 53 / 56

Sequences CREATE SEQUENCE <name>; Get the next and the current value of the sequence. SELECT nextval('<name>' SELECT currval('<name>' Getting the current value only works if called after calling nextval and in the same transaction. 54 / 56

The SERIAL type The data type serial is not a true type, but merely a notational convenience for setting up unique identifier columns. CREATE TABLE <tablename> ( <colname> SERIAL Is equivalent to: CREATE SEQUENCE <tablename_colname_seq>; CREATE TABLE <tablename> ( <colname> INTEGER DEFAULT nextval('<tablename_colname_seq>') NOT NULL 55 / 56

Example CREATE TABLE category ( id SERIAL PRIMARY KEY, name VARCHAR ) CREATE TABLE product ( id SERIAL PRIMARY KEY, name VARCHAR, cat_id INTEGER REFERENCES category INSERT INTO category VALUES(DEFAULT, 'Fruits' INSERT INTO products VALUES(DEFAULT, 'Lemon', currval('category_id_seq') 56 / 56