SQL: DDL. John Ortiz Cs.utsa.edu

Similar documents
EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

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

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

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

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

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

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

The Relational Model of Data (ii)

SQL DATA DEFINITION LANGUAGE

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

Full file at

SQL DATA DEFINITION LANGUAGE

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

Data Definition Language (DDL)

Lab # 4. Data Definition Language (DDL)

Data Modeling. Yanlei Diao UMass Amherst. Slides Courtesy of R. Ramakrishnan and J. Gehrke

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

SQL: Data Definition Language

MTAT Introduction to Databases

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

The Structured Query Language Get Started

SQL Data Definition: Table Creation

SQL DATA DEFINITION LANGUAGE

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

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

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

CSC 453 Database Technologies. Tanu Malik DePaul University

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

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

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

Integrity constraints, relationships. CS634 Lecture 2

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

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

The Relational Model 2. Week 3

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

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

The Relational Model. Week 2

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Relational data model

The Relational Data Model. Data Model

Database Applications (15-415)

The Relational Model. Outline. Why Study the Relational Model? Faloutsos SCS object-relational model

The Relational Model. Roadmap. Relational Database: Definitions. Why Study the Relational Model? Relational database: a set of relations

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

doc. RNDr. Tomáš Skopal, Ph.D. RNDr. Michal Kopecký, Ph.D.

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

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

CS143: Relational Model

The Relational Model Constraints and SQL DDL

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

Introduction to Data Management. Lecture #5 Relational Model (Cont.) & E-Rà Relational Mapping

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

CS275 Intro to Databases

MySQL Introduction. By Prof. B.A.Khivsara

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

A <column constraint> is a constraint that applies to a single column.

Principles of Data Management

q Ø v v v v v v v v IBM - 2

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

CS 377 Database Systems

CS2300: File Structures and Introduction to Database Systems

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

3.2.4 Enforcing constraints

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

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

Review. The Relational Model. Glossary. Review. Data Models. Why Study the Relational Model? Why use a DBMS? OS provides RAM and disk

Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

SQL: The Query Language Part 1. Relational Query Languages

Introduction to Data Management. Lecture #4 (E-R à Relational Design)

General Overview - rel. model. Carnegie Mellon Univ. Dept. of Computer Science Database Applications. Reminder: our Mini-U db

SQL Introduction. CS 377: Database Systems

Simple SQL. Peter Y. Wu. Dept of Computer and Information Systems Robert Morris University

Lecture 07. Spring 2018 Borough of Manhattan Community College

Oracle Tables TECHGOEASY.COM

UNIT 4 DATABASE SYSTEM CATALOGUE

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

CSEN 501 CSEN501 - Databases I

3ISY402 DATABASE SYSTEMS

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

Relational Databases BORROWED WITH MINOR ADAPTATION FROM PROF. CHRISTOS FALOUTSOS, CMU /615

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #4: SQL---Part 2

Basic SQL. Basic SQL. Basic SQL

Chapter 1 SQL and Data

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

Lecture 5: SQL s Data Definition Language

Lecture 2: Introduction to SQL

The Relational Model. Chapter 3

SQL Data Definition Language

Outer Join, More on SQL Constraints

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

The Relational Model. Chapter 3. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

SQL: Part III. Announcements. Constraints. CPS 216 Advanced Database Systems

CompSci 516 Database Systems. Lecture 2 SQL. Instructor: Sudeepa Roy

CSCC43H: Introduction to Databases. Lecture 4

Content: How to transform the relational model into a database schema DDL: create table students(studentnr int, name varchar);

How to define a relational schema for a data base?

Unit 1 - Chapter 4,5

Database Design. Goal: specification of database schema Methodology:

Chapter 7 Constraints and Triggers. Spring 2011 Instructor: Hassan Khosravi

Transcription:

SQL: DDL John Ortiz Cs.utsa.edu

SQL Data Definition Language Used by DBA or Designer to specify schema A set of statements used to define and to change the definition of tables, columns, data types, constraints, views, indexes, SQL DDL & DML are integrated. A DDL statement often needs to contain some DML statements. Lecture 10 SQL: Overview 2

A Sample University Schema Students(SID:string, Name:string, Age:integer, Sex:char, Major:string, GPA:real) Courses(Cno:string, Title:string, Hours:integer, Dept:string) Enrollment(SID:string, Cno:string, Year:string, Grade:string) Offers(Cno:string, Year:integer, FID:string) Faculty(FID:string, Name:string, Rank:string, Dept:string, Salary:real) Departments(Name:string, Location:string, ChairID:string) Lecture 10 SQL: Overview 3

Create Students Table In SQL*Plus: SQL> create table Students 2 (SID char(9) not null, 3 Name varchar2(25), 4 Age integer, 5 Sex char(1), 6 Major char(4), 7 GPA number(3,2), 8 primary key (SID)); Lecture 10 SQL: Overview 4

Create Tables Syntax create table Table-Name ( Col-Name Type Deft-Val Col-Constraint, Col-Name Type Deft-Val Col-Constraint, Table-Constraint, Table-Constraint); Lecture 10 SQL: Overview 5

Oracle SQL Built-in Data Types char(n). String of n < 2000 char varchar2(n). String up to n <= 4000 char long. Char string of length up to 2GB number(n,m). n digits, m after decimal point. number. Integer or real up to 40 digits integer. Integer up to 40 digits blob. Binary data up to 4 GB date. DD-MMM-YY time. HH:MM:SS These may differ from SQL2 & SQL-1999. Lecture 10 SQL: Overview 6

SQL Integrity Constraints Rules or regulations imposed to ensure data integrity. Column Constraints. Table Constraints. Assertions (Multiple-table Constraints). Triggers. Primary Key, Foreign Key, Check, Not Null, Unique, Lecture 10 SQL: Overview 7

Column Definition Syntax for column definition: col_name data_type [default value] [column constraints] Syntax for column constraints: [constraint constraint_name] [not] null check condition unique primary key references table_name [(column)] [on delete cascade] Lecture 10 SQL: Overview 8

Column Constraints not null. Can not take null value. unique. Can not have identical non-null values primary key. Both not null and unique references T(A). All non-null values must be currently in T.A. check (condition). Values must satisfy the check condition. Can be expressed as table constraints, too. Lecture 10 SQL: Overview 9

Column Constraints Example SQL> create table Courses (CNo char(6) primary key, Title varchar2(50) not null, Hours integer default 3 check (Hours > 0 and hours < 6), Dept varchar2(20) references Departments(Name)); Lecture 10 SQL: Overview 10

Table Constraints Syntax for table constraints: [constraint constraint_name] check condition unique (column {, column}) primary key (column {, column}) foreign key (column {, column}) references table_name[(column {, column})] [on delete cascade] Lecture 10 SQL: Overview 11

Table Constraints Example SQL> create table Enrollment (SID char(9) not null references Students, CNo varchar2(7) not null, Year number(2) not null, Grade char(2), primary key (SID, CNo, Year), foreign key (CNo) references Courses); Lecture 10 SQL: Overview 12

Table Constraints Example (cont.) SQL> create table Students (SID char(9) primary key, Name varchar2(25), Age integer check(age > 18 and Age < 100), Sex char check(sex in { F, M }), Major varchar2(4) GPA number (3,2) not null, constraint ic12 check (GPA >= 2.0 and (Major = IS or GPA >= 3.0))); Lecture 10 SQL: Overview 13

Referential Integrity & Data Update Assume that Courses.Dept references Departments.Name. What should the system do to students if we change a department s name or delete a department? SQL provides four options: No action. Disallow such an update. Cascade. Accept update and update all affected foreign key values. Set default. Accept update & set default FK. Set null. Accept update & set FK to null. Lecture 10 SQL: Overview 14

Referential Integrity Example SQL>create table Courses ( CNo char(6) not null primary key, Title varchar(35) not null, Hours int check (Hours between 1 and 5), Dept varchar(20), foreign key (Dept) references Departments(Name) on delete no action on update cascade); Lecture 10 SQL: Overview 15

Drop Table Delete schema definition of a table. drop table Table-Name; Problem: drop table Departments will fail if it is referenced by foreign keys. Solution: drop table Departments cascade constraints; All referential constraints will be dropped before the table is dropped. Lecture 10 SQL: Overview 16

Alter Table Change table schema (even after entering data) Add a new column. alter table Students add (Address varchar2(40)); Add a new constraint. alter table Students add (unique(address)); Modify a column definition. alter table Students modify (Name varchar2(30)); Lecture 10 SQL: Overview 17

Remove a column. Alter Table (cont.) alter table Students drop (Address); Enable and disable a constraint on a table alter table Students enable constraint ic12; alter table Students disable constraint ic12; Newly added column can not be specified as not null. Can not modify a column to a type of a smaller size. Lecture 10 SQL: Overview 18

Simple Update Statements Insert Statement: insert into table_name [(column {, column})] [values (expression {, expression})] Update Statement: update table_name [corr_name] set column = {expression null} {, column = {expression null}} [where search_condition] Delete Statement: delete from table_name Lecture 10 SQL: Overview 19

insert into Students Example of Update values (`123456789', `Kathy', 26, F, CS, null) or insert into Students (Name, SID, Age, Major, Sex) values (`Kathy', `123456789', 26, CS, F ) Lecture 10 SQL: Overview 20

Example of Update (cont.) Increase the GPA of the student with SID = 123456789 by 0.5. update Students set GPA = GPA + 0.5 where SID = '123456789 Delete all tuples from Students. delete from Students The schema of Students remains. Lecture 10 SQL: Overview 21

Data Dictionary Data dictionary (system catalog) contains information about all database objects (tables, views, indexes, sequences, etc). Common Oracle Data Dictionary Tables user_objects(object_name, object_id, object_type, created, last_ddl_time, timestamp, status) Example database objects include tables, views, sequences, indexes, and packages. Lecture 10 SQL: Overview 22

Data Dictionary (cont.) user_tables(table_name, tablespace_name, num_rows, blocks, empty_blocks, avg_row_len) user_tab_columns(name, table_name, column_name, data_type, data_length, nullable, column_id, default_length, data_default, num_distinct, low_value, high_value) Use select * from dictionary to see all system tables and views. Use describe table-name to view a schema. Lecture 10 SQL: Overview 23

DDL Summary Specify appropriate data type for each column. You may also define your own domains. Specify as many constraints as needed for applications. Specify desirable actions for foreign key constraints. Not all constraints can be specified at the same time. It is necessary to update schemas. Major schema change after data is entered is very costly. Lecture 10 SQL: Overview 24