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

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

Basic SQL. Basic SQL. Basic SQL

SQL: Data Definition Language

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

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

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

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

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

Lab # 4. Data Definition Language (DDL)

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

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

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

MTAT Introduction to Databases

CS2300: File Structures and Introduction to Database Systems

SQL DATA DEFINITION LANGUAGE

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

Data Definition Language (DDL)

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

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

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

SQL Introduction. CS 377: Database Systems

SQL DATA DEFINITION LANGUAGE

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

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

The Relational Model of Data (ii)

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

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

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

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

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

Database Management Systems,

SQL Data Definition Language

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

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Today's Party. Example Database. Faloutsos/Pavlo CMU /615

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

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

SQL DATA DEFINITION LANGUAGE

1) Introduction to SQL

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

user specifies what is wanted, not how to find it

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

CSCC43H: Introduction to Databases. Lecture 4

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

MTAT Introduction to Databases

Slides by: Ms. Shree Jaswal

CS143: Relational Model

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 2

Programming and Database Fundamentals for Data Scientists

SQL Data Definition Language: Create and Change the Database Ray Lockwood

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. General Overview - rel. model. Overview - detailed - SQL

Full file at

CSIE30600 Database Systems Basic SQL 2. Outline

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

Structured Query Language (SQL)

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

Chapter 1 SQL and Data

GlobAl EDITION. Database Concepts SEVENTH EDITION. David M. Kroenke David J. Auer

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

Lecture 5: SQL s Data Definition Language

Data Definition and Data Manipulation. Lecture 5: SQL s Data Definition Language CS1106/CS5021/CS6503 Introduction to Relational Databases

SQL DDL. Intro SQL CREATE TABLE ALTER TABLE Data types Service-based database in Visual Studio Database in PHPMyAdmin

MySQL Creating a Database Lecture 3

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

MySQL Introduction. By Prof. B.A.Khivsara

Lecture 07. Spring 2018 Borough of Manhattan Community College

In This Lecture. SQL Data Definition SQL SQL. Non-Procedural Programming. Notes. Database Systems Lecture 5 Natasha Alechina

BEGINNING T-SQL. Jen McCown MidnightSQL Consulting, LLC MinionWare, LLC

Data and Tables. Bok, Jong Soon

INFORMATION TECHNOLOGY NOTES

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

Kaotii.

Chapter 9: Working with MySQL

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

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

SQL Server 2008 Tutorial 3: Database Creation

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

SQL: DDL. John Ortiz Cs.utsa.edu

Simple Quesries in SQL & Table Creation and Data Manipulation

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

Information Systems for Engineers Fall Data Definition with SQL

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

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Chapter 3: Introduction to SQL

ASSIGNMENT NO 2. Objectives: To understand and demonstrate DDL statements on various SQL objects

Downloaded from

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

Unit 1 - Chapter 4,5

TINYINT[(M)] [UNSIGNED] [ZEROFILL] A very small integer. The signed range is -128 to 127. The unsigned range is 0 to 255.

Data Definition Language with mysql. By Kautsar

CSC 453 Database Technologies. Tanu Malik DePaul University

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

COMP 430 Intro. to Database Systems

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Overview Relational data model

SQL Structured Query Language Introduction

SQL Structured Query Language (1/2)

The SQL data-definition language (DDL) allows defining :

COSC344 Database Theory and Applications. Lecture 11 Triggers

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Full file at

Transcription:

.. Cal Poly CPE/CSC 365: Introduction to Database Systems Alexander Dekhtyar.. SQL Data Definition and Data Manipulation Languages (DDL and DML) Note: This handout instroduces both the ANSI SQL synatax for the SQL DDL and DML commands, as well as discusses the MySQL extensions to the syntax that are of importance to the course. As a general note, MySQL has a very rich syntax for the statements discussed in this handout, with many advanced features specifiable in addition to the standard ANSI SQL syntax. We only cover the features that are of immediate interest to us. Data Definition Language. Creating a Relation CREATE TABLE Name ( attribute-declarations constraint-declarations ) Attribute declarations: AttName AttType [ default expression ] [ ColConstraints ] Constraints Column constraints: [NOT] NULL : Not null constraint. PRIMARY KEY: Primary key constraint (when the primary key consists of exactly one attribute, otherwise, use constraint declaration). UNIQUE: Key constraint (when the key consists of exactly one attribute, otherwise, use constraint declaration). 1

REFERENCES <Table>[(<AttName>)] [ON DELETE CASCADE]: Foreign key constraint (when the foreign key consists of exactly one attribute, otherwise, use constraint declaration). ON DELETE CASCADE specifies that all rows containing a no longer existing value for must be deleted. AUTO INCREMENT: the values in the column (integer type) are incremented automatically as new tuples are added to the table. Constraint declarations: [constraint <ConstName>] PRIMARY KEY (<AttNames>): Primary key constraint. Use when the primary key includes multiple attributes. [constraint <ConstName>] UNIQUE (<AttNames>): Key constraint. Use when the key includes multiple attributes. [constraint <ConstName>] FOREIGN KEY (<AttNames>) REFERENCES <Table> [(<AttNames>)]: Foreign key constraint. Use when the foreign key involves multiple attributes. All column constraints except for not null constraint can only be used if the appropriate constraint (e.g., primary key) is associated with exactly one attribute. (i.e., if your primary key is two attributes, use the constraint declaration, rather than column constraint). Types Integer Real Fixed Point Strings Bit Strings Dates INTEGER or INT SMALLINT TINYINT MEDIUMINT BIGINT FLOAT or REAL DOUBLE DECIMAL(n,d) n - number of digits d - number of decimals NUMERIC(n, d) (Oracle) CHAR(n) n - length of string, max=255 VARCHAR(n), n - length of string BIT(n) DATE TIME TIMESTAMP DATETIME YEAR

Examples CREATE TABLE Books ( LibCode INT, ISBN CHAR(20), Title CHAR(80), Authors CHAR(60), Year INT, Publisher CHAR(20), PurchPrice REAL, TakeHome BOOLEAN, PRIMARY KEY (LibCode), UNIQUE (ISBN) ); CREATE TABLE Employees ( SSN INT PRIMARY KEY, Name CHAR(30) NOT NULL, Department INT REFERENCES Departments, Salary FLOAT NOT NULL Position CHAR(30) DEFAULT Not Specified, StartYear INT CHECK(StartYear > 1992) ); CREATE TABLE Departments ( DeptID INT PRIMARY KEY AUTO_INCREMENT, Name CHAR(30) UNIQUE, Head INT, FOREIGN KEY(Head) REFERENCES Employees ); Deleting a Table DROP TABLE Name [RESTRICT CASCADE] Example: DROP TABLE Books; DROP TABLE Departments CASCADE; In the latter case, all referential integrity constraints (foreign keys) are dropped from their respective tables, after Departments table is deleted. DROP TABLE Employees RESTRICT; The use of RESTRICT in the DROP TABLE command directs the DBMS server to drop the table only if doing so does not affect the constraints in other tables. Otherwise, the table is not deleted. Modifying a Table Adding an attribute

ALTER TABLE Name ADD [COLUMN] ( [AttName Type [FIRST AFTER AttName]]+ ) ALTER TABLE Books ADD (Genre CHAR(10), NumPages INT); ALTER TABLE Employees ADD COLUMN (TransferredFrom INT AFTER Department); The FIRST and AFTER AttName modifiers specify the position of the new column in the table. The default position (when both modifiers are omitted) is at the end of the table. Deleting an attribute ALTER TABLE Name DROP [COLUMN] (AttName+) Example: ALTER TABLE Books DROP (Year); Modifying an attribute ALTER TABLE Name MODIFY ( [AttName Type [FIRST AFTER AttName] ]+ ) Example: ALTER TABLE Books MODIFY (Genre VARCHAR2(30)); Renaming a table (MySQL only) ALTER TABLE Name RENAME [TO AS] NewName; Note: ALTER TABLE command can be used for a wide range of other changes to the database (manipulation of constraints, for example). These are covered later. Data Manipulation Language Inserting a Tuple INSERT INTO TableName(AttNames) VALUES(values )[,(values )]* values comma-separated list of values. The number of values must match the number attribute names in AttNames, and the types must be compatible. INSERT INTO TableName VALUES(values )[,(values )]*

Values for all attributes must be given and in the order in which attributes were defined in CREATE TABLE command. In both cases, in MySQL (but not in ANSI SQL), a single INSERT INTO command can insert multiple tuples. INSERT INTO Books(LibCode,Title,Year) VALUES (12349, Database Management Systems, 2000); INSERT INTO Books VALUES (15923, 1-56592-000-7, Lex \& Yacc, J. Levine, T. Mason, D. Brown, 1990, O Reily, 29.95, True); INSERT INTO Departments( VALUES ( Sales, 3), ( HR,11), ( IT, 76); (Note that in the latter example, DeptID is an auto incrementing attribute and does not need to be inserted into the table.) Deleting Tuples DELETE FROM TableName [WHERE Expression]; Expression identifies the properties of tuples to be removed from the table. DELETE FROM Books; DELETE FROM Books WHERE LibCode = 12349; DELETE FROM Books WHERE PurchPrice > 100.00 AND Year < 1950; Updating Tuples UPDATE TableName SET Assignments WHERE Expression; Expression identifies tuples to be updated. Assignments specifies modifications. UPDATE Books SET Year = 2003 WHERE Year > 2003;

UPDATE Books SET Year = Year - 1, PurchPrice = PurchPrice *1.05; WHERE Year > 2000; UPDATE Books SET TakeHome = True;