Announcements Read 6.4 – 6.6 for Monday Homework 5, due Friday (today) Project Step 4, due Monday Research paper –List of sources - due 10/29.

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
Advertisements

A Guide to Oracle9i1 Creating and Modifying Database Tables Chapter 2.
Data Definition Language (DDL)
Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
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.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
SQL. Components of SQL Data definition language (DDL) –To create database structures –To enforce constraints etc. Data manipulation language (DML) –To.
SQL Sangeeta Devadiga CS157A, Fall Outline Background Data Definition Basic Structure Set Operation.
Chapter 6 SQL. Agenda Data Definition Language (DDL) Access Control.
1 Chapter 2: Creating and Modifying Database Tables.
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
Creating Database Tables © Abdou Illia MIS Spring /21/2015.
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.
Creating Database Tables CS 320. Review: Levels of data models 1. Conceptual: describes WHAT data the system contains 2. Logical: describes HOW the database.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
Database Management System LICT 3011 Eyad H. Elshami.
Access Lecture 1 Database Overview and Creating Tables Create an Employee Table.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
1 CSE 480: Database Systems Lecture 9: SQL-DDL Reference: Read Chapter of the textbook.
Oracle Data Definition Language (DDL)
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Database Lecture # 1 By Ubaid Ullah.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
Irwin/McGraw-Hill Copyright © 2000 The McGraw-Hill Companies. All Rights reserved Whitten Bentley DittmanSYSTEMS ANALYSIS AND DESIGN METHODS5th Edition.
The Relational Model These slides are based on the slides of your text book.
Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
1 The Relational Model Instructor: Mohamed Eltabakh
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
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.
Wednesday, January 14,  Proposed by E.F.Codd in his 1970 paper  Used in System R, IBM’s research relational database in early 1970s-D.D. Chamberlin.
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.
11 3 / 12 CHAPTER Databases MIS105 Lec15 Irfan Ahmed Ilyas.
CS 3630 Database Design and Implementation. Assignment 3 Style! Agreement between database designer and the client. UserName1_EasyDrive UserName2_EasyDrive.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Advanced Database CS-426 Week 1 - Introduction. Database Management System DBMS contains information about a particular enterprise Collection of interrelated.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
The Relational Model Content based on Chapter 3 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
Visual Programing SQL Overview Section 1.
Announcements Reading for Monday –4.6 Homework 3 – Due 9/29.
Chapter 6 SQL. Agenda Data Definition Language (DDL) Access Control.
1 Chapter 2: Creating and Modifying Database Objects.
Sql DDL queries CS 260 Database Systems.
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.
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Basic SQL*Plus edit and execute commands SQL*Plus buffer and built-in editor holds the last SQL statement Statements are created in free-flow style and.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
CS 3630 Database Design and Implementation
Managing Tables, Data Integrity, Constraints by Adrienne Watt
ORACLE SQL Developer & SQLPLUS Statements
DATABASE MANAGEMENT SYSTEM
Instructor: Mohamed Eltabakh
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
Introduction to Oracle
SQL (Structured Query Language)
Presentation transcript:

Announcements Read 6.4 – 6.6 for Monday Homework 5, due Friday (today) Project Step 4, due Monday Research paper –List of sources - due 10/29

SQL Database Design Language Lecture 15

History of SQL Proposed by E.F.Codd in his 1970 paper Used in System R, IBMs research relational database in early 1970s-D.D. Chamberlin et al at IBM Research Center, San Jose, California Used in Oracle, released in late 1970s Incorporated into IBMs SQL/DS in 1981, and DB2 in 1983 Also used in Microsoft SQL Server, MySQL, Informix, Sybase, dBase, Paradox, r:Base, FoxPro, and others

Standards ANSI and ISO published SQL standards in 1986, called SQL-1 Minor revision, SQL-89 Major revision, SQL-2,1992 SQL-3, multi-part revision, contains SQL:1999, which included object-oriented (OO) facilities, and user defined datatypes (UDTs) Most vendors support standard, but have slight variations of their own

Components of SQL Data definition language - DDL Data manipulation language - DML Authorization language – grant privileges to users

Relational Database Architecture Separate external, logical, internal models Base tables and indexes form logical level Indexes are B+ trees or B trees – maintained by system Relational views (external level) are derived from base tables Users see views or base tables, or combination Internal level - files SQL supports dynamic database definition- can modify structures easily

DDL Commands CREATE TABLE CREATE INDEX ALTER TABLE RENAME TABLE DROP TABLE DROP INDEX Also – CREATE VIEW

CREATE TABLE CREATE TABLE base-table-name (colname datatype [column constraints – NULL/NOT NULL, DEFAULT…, UNIQUE, CHECK…, PRIMARY KEY], [,colname datetype [column constraints …]]... [table constraints – PRIMARY KEY…, FOREIGN KEY…, UNIQUE…, CHECK…] [storage specifications]);

Identifiers No SQL keywords may be used Table name must be unique within the database For each column, the user must specify a name that is unique within the table

Datatypes Each column must have a datatype specified Standards include various numeric types, fixed-length and varying-length character strings, bit strings, and user- defined types Available datatypes vary from DBMS to DBMS

Common data types in SQL:Oracle:Access: CHARACTER (n) or CHAR(n)CHAR(n)Text (1-256 char; 50 def) VARCHAR (n)VARCHAR2(n)Memo ( char) BIT (n)LONG BIT VARYING (n)NUMBERNumber DECIMAL (precision,scale) exact NUMBER (p,s) precision/scale Currency INTEGER or INTDATE ( stored with century info-- Date/Time FLOAT approximate def I/O format DD-MON-YY) DATEROWID (10B or 6B)AutoNumber TIMEBLOBOLE Object CLOBYes/No BFILEHyperlink + user-defined typesLookup Wizard Datatypes

SQL plus Interpreter for SQL ssh db.cs.loyola.edu Use user names and passwords to login To start SQL plus –type: sqlplus –Enter SQL commands Type exit when done

Creating the Tables for the University Database CREATE TABLE Student( stuIdCHAR(6), lastNameCHAR(20) NOT NULL, firstName CHAR(20) NOT NULL, majorCHAR(10), credits SMALLINT DEFAULT 0, CONSTRAINT Student_stuId_pk PRIMARY KEY (stuId), CONSTRAINT Student_credits_cc CHECK (credits>=0 AND credits < 150)); CREATE TABLE Faculty( facIdCHAR(6), name CHAR(20) NOT NULL, departmentCHAR(20) NOT NULL, rank CHAR(10), CONSTRAINT Faculty_facId_pk PRIMARY KEY (facId));

Creating the Tables for the University Database CREATE TABLE Class( classNumberCHAR(8), facId CHAR(6) NOT NULL, schedule CHAR(8), roomCHAR(6), CONSTRAINT Class_classNumber_pk PRIMARY KEY (classNumber), CONSTRAINT Class_facId_fk FOREIGN KEY (facId) REFERENCES Faculty (facId)) ; CREATE TABLE Enroll( stuIdCHAR(6), classNumberCHAR(8), gradeCHAR(2), CONSTRAINT Enroll_classNumber_stuId_pk PRIMARY KEY (classNumber, stuId), CONSTRAINT Enroll_classNumber_fk FOREIGN KEY (classNumber) REFERENCES Class (classNumber), CONSTRAINT Enroll_stuId_fk FOREIGN KEY (stuId) REFERENCES Student(stuId));

New Datatypes SQL:1999 provides new UDTs Can define structured UDTs Also new DISTINCT types e.g. CREATE DOMAIN creditValues INTEGER DEFAULT 0 CHECK (VALUE >=0 AND VALUE <150);

New Datatypes New type can then be used in defining columns In Student, can write credits creditValues,... Cant compare values of two different DISTINCT types, even if underlying datatype is the same Also cant apply functions to them, but can write our own functions for them …will be discussed more fully in Chapter 7

Constraints Can be defined at column level or table level Column-level constraints –NULL/NOT NULL, UNIQUE, PRIMARY KEY, CHECK and DEFAULT –Written immediately after column name, datatype

Constraints Table-level constraints –Primary key, foreign keys, uniqueness, checks, and general constraints –Note: any primary key can be defined at table level; composite primary keys can only be expressed as table-level constraints –Foreign key constraint requires that the referenced table exist already

Constraints Table-level constraints –SQL standard supports ON UPDATE and ON DELETE clauses for foreign keys; Options are CASCADE/SET NULL/SET DEFAULT/NO ACTION; Example: ON UPDATE CASCADE –Table uniqueness constraint used to specify values in a combination of columns must be unique; good for candidate keys –Constraints can be given a name; useful for disabling them at times –NOTE: Not all options are supported in all DBMSs

Indexes Can create any number of indexes for tables Stored in same file as base table Facilitate fast retrieval of records with specific values in a column Keep track of what values exist for the indexed columns, and which records have those values B+ trees or B trees used – see Appendix A for review of concepts Overhead – system must maintain index

CREATE INDEX Command CREATE [UNIQUE] INDEX indexname ON basetablename (colname [order] [,colname [order]]...) [CLUSTER] ;

CREATE INDEX Command Ex. CREATE INDEX Student_lastName_firstName_ndx ON Student (lastName, firstName); UNIQUE specification enforces unique values for indexed column or combination of columns Except when specified, column need not be unique Order is ASC(default) or DESC Can have major and minor orders CLUSTER specification keeps records with same value for indexed field together (only one per table) Oracle automatically indexes primary key columns

ALTER TABLE Command To add a new column ALTER TABLE basetablename ADD columnname datatype; Ex. ALTER TABLE Student ADD COLUMN birthdate DATETYPE; –Cannot specify NOT NULL, since existing records have no value for this field To drop a column ALTER TABLE basetablename DROP COLUMN columnname; Ex. ALTER TABLE Student DROP COLUMN major;

ALTER TABLE Command To add a constraint ALTER TABLE basetablename ADD CONSTRAINT constraint_defn; To drop a constraint ALTER TABLE basetablename DROP CONSTRAINT constraint_name;

Other Changes to Tables Renaming a table: RENAME TABLE old-table-name TO new-table-name; Ex: RENAME TABLE FACULTY TO TEACHERS; Dropping a table: –CASCADE; //remove table and all references to it (ex. in a REFERENCES) –RESTRICT; //remove the table only if there are no references to it elsewhere DROP TABLE basetablename; Ex. DROP TABLE CLASS; Dropping an index: DROP INDEX indexname; Ex. DROP INDEX Student_lastName_fristName_ndx;