CS2300: File Structures and Introduction to Database Systems

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

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

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

SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE

SQL: Data Definition Language

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

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

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

SQL DATA DEFINITION LANGUAGE

Basic SQL. Basic SQL. Basic SQL

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

SQL Introduction. CS 377: Database Systems

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

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

Data Definition Language (DDL)

COSC Assignment 2

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

Database design process

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Slides by: Ms. Shree Jaswal

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

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

CS W Introduction to Databases Spring Computer Science Department Columbia University

Lab # 4. Data Definition Language (DDL)

SQL Data Definition Language

CSIE30600 Database Systems Basic SQL 2. Outline

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

SQL- Updates, Asser0ons and Views

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

Unit 1 - Chapter 4,5

Overview Relational data model

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

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

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

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

CS54100: Database Systems

Database Management Systems,

Creating Tables, Defining Constraints. Rose-Hulman Institute of Technology Curt Clifton

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

Database Technology. Topic 3: SQL. Olaf Hartig.

ECE 650 Systems Programming & Engineering. Spring 2018

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Relational Databases Fall 2017 Lecture 7

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

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

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

Constraints, Views & Indexes. Running Example. Kinds of Constraints INTEGRITY CONSTRAINTS. Keys Foreign key or referential integrity constraints

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

3ISY402 DATABASE SYSTEMS

DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

SQL STRUCTURED QUERY LANGUAGE

Databases 1. Defining Tables, Constraints

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

Database Technology. Topic 2: Relational Databases and SQL. Olaf Hartig.

Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries

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

Database Management System (15ECSC208) UNIT I: Chapter 2: Relational Data Model and Relational Algebra

Constraints. Local and Global Constraints Triggers

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

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

ECE 650 Systems Programming & Engineering. Spring 2018

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

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

Referential Integrity and Other Table Constraints Ray Lockwood

Downloaded from

The Relational Model of Data (ii)

The Relational Model 2. Week 3

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

Outer Join, More on SQL Constraints

COSC344 Database Theory and Applications. σ a= c (P) S. Lecture 4 Relational algebra. π A, P X Q. COSC344 Lecture 4 1

MySQL Creating a Database Lecture 3

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

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

CS 348 Introduction to Database Management Assignment 2

1) Introduction to SQL

user specifies what is wanted, not how to find it

CS 250 SQL and SQLite. Benjamin Dicken

Database Laboratory Note (MS SQL Server 2000)

COSC344 Database Theory and Applications. Lecture 11 Triggers

Relational Algebra Part I. CS 377: Database Systems

SQL Constraints and Triggers

Insertions, Deletions, and Updates

Full file at

The DBMS accepts requests for data from the application program and instructs the operating system to transfer the appropriate data.

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

Relational Database Systems Part 01. Karine Reis Ferreira

CSC 261/461 Database Systems Lecture 6. Fall 2017

SQL. Copyright 2013 Ramez Elmasri and Shamkant B. Navathe

Chapter 6: RELATIONAL DATA MODEL AND RELATIONAL ALGEBRA

Principles of Data Management

SQL Data Definition: Table Creation

Constraints and Triggers

2.2.2.Relational Database concept

NCSS: Databases and SQL

SQL-99: Schema Definition, Basic Constraints, and Queries. Create, drop, alter Features Added in SQL2 and SQL-99

DATABASE DESIGN - 1DL400

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

A tuple is dangling if it doesn't join with any

Transcription:

CS2300: File Structures and Introduction to Database Systems Lecture 14: SQL Doug McGeehan

From Theory to Practice The Entity-Relationship Model: a convenient way of representing the world. The Relational Model: a model for organizing data using tables. Oracle Database: a database infrastructure which implements the relational model. SQL(Structured Query Language): A language used to get information from a database.

Database Systems Oracle MySQL / MariaDB SQLite PostgreSQL

Create and Delete Tables in SQL

CREATE TABLE Command The basic format of the CREATE TABLE command is: CREATE TABLE TableName( Column1 DataType1 ColConstraint, ColumnN DataTypeN ColConstraint, TableConstraint1, TableConstraintM

Example SQL> CREATE TABLE DEPARTMENT ( DNAME VARCHAR(15) NOT NULL, DNUMBER INT, MGRSSN CHAR(9) NOT NULL, MGRSTARTDATE DATE, PRIMARY KEY (DNUMBER) Don t forget the semicolon ;

Example Create tables EMPLOYEE and DEPARTMENT

Data Types 1. INT or INTEGER. 2. REAL or FLOAT. 3. CHAR(n) = fixed length character string, padded with padding characters 4. VARCHAR (n) = variable-length strings up to n characters.

Data Types 5. NUMERIC(precision, decimal) is a number with precision digits with the decimal point decimal digits from the right. NUMERIC(10,2) can store ±99,999,999.99 6. DATE and TIME 7. Others Boolean Interval

Constraints in Create Table Adding constraints to a table: enforce data integrity Consequence: inserting takes longer Different types of constraints: Not Null Default Values Unique Primary Key Foreign Key Check Condition

Not Null Constraint Not Null = every tuple must have a real value for this attribute. CREATE TABLE Employee( ID INTEGER NOT NULL, Fname VARCHAR(20), Lname VARCHAR(20), Gender CHAR(1), Salary INTEGER NOT NULL, Dept INTEGER

Default Values Default value = a value to use whenever no other value of this attribute is known. CREATE TABLE Employee( ID Fname Lname INTEGER NOT NULL, VARCHAR(20), VARCHAR(20), Gender CHAR(1) default( F ), Salary Dept INTEGER NOT NULL, INTEGER

Example Insert Into Employee(id, Salary) Values(123,80000 id Fname Lname Gender Salary Dept 123 NULL NULL F 80000 NULL Insert Into Employee(id, Dept) Values(100,01 This operation will be rejected because Salary cannot be NULL.

Declaring Keys Each table can have only one Primary key, but many UNIQUE s. SQL enforces Entity Integrity constraint Primary key cannot be NULL NULLs allowed for unique columns Multiple tuples may have NULL in unique column Reason: inequality of NULL values

Declaring Keys Two places to declare: 1. After an attribute s type, if the attribute is a key by itself. 2. As a separate element. Essential if the key contains more than one attribute.

Unique Constraint (Syntax 1) CREATE TABLE Employee( ID INTEGER UNIQUE NOT NULL, Fname VARCHAR(20), Lname VARCHAR(20), Gender CHAR(1) default( F ), Salary INTEGER NOT NULL, Dept INTEGER

Unique Constraint (Syntax 2) CREATE TABLE Employee( ID INTEGER NOT NULL, Fname VARCHAR(20), Lname VARCHAR(20), Gender CHAR(1) default( F ), Salary INTEGER NOT NULL, Dept INTEGER, UNIQUE(ID)

Question Are they the same? CREATE TABLE Sells ( bar CHAR(20), beer VARCHAR(20), price REAL, UNIQUE(bar,beer) CREATE TABLE Sells ( bar CHAR(20) UNIQUE, beer VARCHAR(20) UNIQUE, price REAL

Primary Key Constraint (Syntax 1) CREATE TABLE Employee( ID INTEGER PRIMARY KEY, Fname VARCHAR(20), Lname VARCHAR(20), Gender CHAR(1), Salary INTEGER NOT NULL, Dept INTEGER, UNIQUE (FNAME,LNAME) Primary Key implies: NOT NULL and UNIQUE.

Primary Key Constraint (Syntax 2) Suppose that the primary key is (Fname, Lname). CREATE TABLE Employee( ID INTEGER, Fname VARCHAR(20), Lname VARCHAR(20), Gender CHAR(1), Salary INTEGER NOT NULL, Dept INTEGER, PRIMARY KEY(Fname,Lname)

CREATE TABLE Employee( CREATE TABLE Department( ID Fname Lname DeptNumber INTEGER PRIMARY KEY, Name INTEGER primary key, VARCHAR(20), VARCHAR(20), Gender CHAR(1), Salary INTEGER NOT NULL, Dept INTEGER VARCHAR(20), ManagerId INTEGER Shouldn t all department numbers in Employee appear in Department?

Employee ID FName LName Gender Sallary Dept 02334 Larry Bird M 80000 12 04556 Magic Johnson M 70000 45 Foreign Key Department DeptNumber Name ManID 12 Sales 988 45 Repair 876

Foreign Key Constraint (Syntax 1) CREATE TABLE Employee ( ID INTEGER primary key, Fname VARCHAR(20), Lname VARCHAR(20), Gender CHAR(1), Salary INTEGER NOT NULL, Dept INTEGER REFERENCES Department(DeptNumber) Note 1: DeptNumber must be unique (or primary key) in Department Note 2: The referencing attribute (Dept) can be null

Foreign Key Constraint (Syntax 2) CREATE TABLE Employee ( ID INTEGER primary key, Fname VARCHAR(20), Lname VARCHAR(20), Gender CHAR(1), Salary INTEGER NOT NULL, DeptNumber INTEGER REFERENCES Department Note: If the foreign key attribute is named the same as the primary key it references, then you don t need to specify the primary key s attribute name here

Foreign Key Constraint (Syntax 3) CREATE TABLE Employee( ID INTEGER primary key, Fname VARCHAR(20), Lname VARCHAR(20), Gender CHAR(1), Salary INTEGER NOT NULL, DeptNumber INTEGER, FOREIGN KEY (DeptNumber) REFERENCES Department This syntax is useful when a foreign key contains more than one attribute.

What Happens When a Foreign Key Constraint is Violated? 1. Insert or update a Employee tuple that refers to a nonexistent Dept in the Department table. Automatic reject 2. Delete or update Dept in the Department table that some Employee tuples refer to. Reject by default CASCADE SET NULL or SET DEFAULT

Example CREATE TABLE Employee( ID INTEGER primary key, DeptNumber INTEGER, FOREIGN KEY (DeptNumber) REFERENCES Department ON DELETE CASCADE ON UPDATE SET DEFAULT(000) Case 1: If Dept 123 is deleted from Department table, all tuples in Employee table with DeptNumber 123 will be automatically deleted. Case 2: If Dept 123 is updated to 100 in the Department table, the value of Dept in Employee tuples which is 123 will be automatically set to 000.

Giving Names to Constraints CREATE TABLE Employee ( ID INTEGER, Fname VARCHAR(20), Lname VARCHAR(20), Gender CHAR(1), Salary INTEGER NOT NULL, CONSTRAINT Dept_Primarykey Primary key (ID) Note 1: names of the constraints in the same relation must be unique Note 2: it is useful when the constraint needs to be dropped later

We are going to see the CHECK constraint in the next class

Delete a Table Basic Format: DROP TABLE Table_name RESTRICT; DROP TABLE Table_name CASCADE; RESTRICT : drop the table only when it is not referenced. CASCADE : will drop the constraints and views that reference the table DROP TABLE command removes all records in the table and the table definition. To delete the table DEPARTMENT: SQL> DROP TABLE DEPARTMENT CASCADE;

More SQL What s next