CSC 240 (Blum)1 Data Definition Language Based on Chapter 6 of Database Systems (Connolly and Begg)

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
Advertisements

SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
SQL’s Data Definition Language (DDL) n DDL statements define, modify and remove objects from data dictionary tables maintained by the DBMS n Whenever you.
Chapter 6 SQL. Agenda Data Definition Language (DDL) Access Control.
Introduction to Structured Query Language (SQL)
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
Chapter 7 SQL: Data Definition Pearson Education © 2009.
1 Pertemuan 11 SQL Data Definition Matakuliah: >/ > Tahun: > Versi: >
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.
Introduction to Structured Query Language (SQL)
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Relational Database Management System A type of database in which records are stored in relational form is called relational database management system.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Chapter 6 SQL. Agenda Data Definition Language (DDL) Access Control.
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
Oracle Data Definition Language (DDL)
Copyright © Curt Hill SQL The Data Definition Language.
Chapter 7 SQL: Data Definition Pearson Education © 2009.
INTEGRITY. Integrity constraint Integrity constraints are specified on a database schema and are expected to hold on every valid database state of the.
SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
1 Chapter 2 Database Environment Transparencies © Pearson Education Limited 1995, 2005.
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
10/25/2012ISC239 Isabelle Bichindaritz1 SQL Commands.
Ms. Hatoon Al-Sagri CCIS – IS Department SQL-99 :Schema Definition, Constraints, Queries, and Views 1.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
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)
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
SQL : Data Definition Session 9 – 10 Course Name: Database System Year : 2012.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
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.
Chapter 6 SQL: Data Definition Transparencies. 2 Chapter 6 - Objectives u Data types supported by SQL standard. u Purpose of integrity enhancement feature.
Chapter 7 SQL: Data Definition Pearson Education © 2009.
Chapter Name SQL: Data Definition
Chapter 6 SQL. Agenda Data Definition Language (DDL) Access Control.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
Chapter 6 SQL: Data Definition Transparencies © Pearson Education Limited 1995, 2005.
The relational model A data model (in general) : Integrated collection of concepts for describing data (data requirements). Relational model was introduced.
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.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Chapter 6 SQL: Data Definition Transparencies © Pearson Education Limited 1995, 2005.
Chapter 7 SQL – Data Definition Pearson Education © 2014.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
Lecture # 24 Introduction to SQL Muhammad Emran Database Systems.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Teacher Workshop Database Design Pearson Education © 2014.
國立臺北科技大學 課程:資料庫系統 Chapter 7 SQL Data Definition.
SQL: Data Definition Transparencies
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Chapter 7 SQL – Data Definition Pearson Education © 2014.
Minggu 5, Pertemuan 9 SQL: Data Definition
Introduction to SQL Chapter 3.
STRUCTURED QUERY LANGUAGE
CS4222 Principles of Database System
SQL data definition using Oracle
Oracle Data Definition Language (DDL)
Chapter 7 SQL – Data Definition Pearson Education © 2014.
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
SQL (Structured Query Language)
Presentation transcript:

CSC 240 (Blum)1 Data Definition Language Based on Chapter 6 of Database Systems (Connolly and Begg)

CSC 240 (Blum)2 DDL Recall that the Data Definition Language (DDL) is the part of SQL that allows one to set up a database’s schema, that is, to define attributes, domains for those attributes, defines tables, define relationships among those tables and so on.

CSC 240 (Blum)3 Start with an attribute One of the smallest units one defines is an attribute. An attribute is given a name (identifier) and assigned a type.

CSC 240 (Blum)4 An identifier by any other name An SQL identifier is the name assigned to a table, column, view, etc. –The identifier is a string of characters from some set. –The string is at most 128 characters long. –The standard set of characters consists of capital letters (A,B,..), small letters (a,b,…), digits (0,1,…) and the underscore character (_). –An identifier starts with a letter. –An identifier contains no spaces (allowed by Access but not by standard SQL).

CSC 240 (Blum)5 SQL Data Types

CSC 240 (Blum)6 BOOLEAN A Boolean variable is one that is either true or false. –In the RealPerson table in the Simpsons database, regular was a Boolean. Either one is a regular employee of the Simpsons or one is not. –In some cases, there may be a third choice for a Boolean variable: NULL.

CSC 240 (Blum)7 CHAR and VARCHAR CHAR and VARCHAR are character strings When defining an attribute of the character type, one includes a length –characterID CHAR(6) –fName VARCHAR(30) the CHAR variable must have the specified length, whereas the VARCHAR variable can be up to the specified length.

CSC 240 (Blum)8 INTEGER and SMALLINT Used for whole numbers, such as: 56, 0 or – 77 There is not always a distinction between INTEGER and SMALLINT, but if there is SMALLINT is used if the numbers are fairly small and one wants to save space. –rooms SMALLINT –population INTEGER

CSC 240 (Blum)9 NUMERIC and DECIMAL Used for numbers that have an exact fractional part. They have a PRECISION (total number of digits) and a SCALE (number of digits after the decimal place). –The number has a PRECISION of 9 and a SCALE of 5. The implementation will have a default PRECISION and SCALE to use if they are not specified. –Salary DECIMAL(7,2)

CSC 240 (Blum)10 NUMERICAL vs DECIMAL NUMERICAL is stricter on the limits of possible numbers. For example, with PRESICION 9 and SCALE 5, –the largest NUMERICAL value is strictly –The largest DECIMAL value may be larger if the implementation can handle it

CSC 240 (Blum)11 FLOATS, REALS, etc. In floating point numbers (like scientific notation  ), the decimal place moves and digits on the right-hand side (least significant) might be dropped during various calculations. This allows for a larger range of numbers, but the numbers are handled somewhat more approximately.

CSC 240 (Blum)12 DATE and TIME Used for variables that track when something happened. DATE keeps track of year, month and day. TIME keeps track of hours, minutes and seconds. TIME can have a precision argument to specify what fractional part of seconds are tracked. –originalAirDate DATE

CSC 240 (Blum)13 Establishing Domains for Attributes The type tells the computer how to interpret data. The type also puts restrictions on the values the data is allowed to have. Further restrictions can be placed on the data by introducing a domain constraint. A domain is an allowed set of values.

CSC 240 (Blum)14 NULL or NOT NULL A simple but important domain constraint is whether or not a value is allowed to be NULL. NULL is used if the data is unknown or not applicable. NULL is distinct from 0 for numbers and “” (the empty string) for characters. The SQL key words are NULL and NOT NULL –NULL means the data can have the NULL value –NOT NULL means it cannot

CSC 240 (Blum)15 NULL is the default If nothing is said, NULL is implied. For example, in the Character table in the Simpsons database –lName VARCHAR(30) –lName VARCHAR(30) NULL would be the same and say that a character may not have a last name –characterID CHAR(6) NOT NULL says that a character must have an ID.

CSC 240 (Blum)16 CHECK: For More Restrictions on Domain CHECK(condition) verifies that the value obeys some condition jobPos VARCHAR(15) CHECK(jobPos IN (‘actor’, ‘producer’)) allows values of ‘actor’, ‘producer’ or NULL for the jobPos. wage DECIMAL(5,2) CHECK(wage > 5.25 AND wage < )

CSC 240 (Blum)17 DOMAIN: making a more restrictive type The SQL keyword DOMAIN allows one in a sense to define one’s own type, provided that type is a restriction on a standard type. CREATE DOMAIN JobType AS VARCHAR(15) CHECK(VALUE IN (‘actor’, ‘producer’)) jobPos JobType

CSC 240 (Blum)18 DEFAULT One can specify a value from the domain to use as a default CREATE DOMAIN LevelType AS VARCHAR(15) DEFAULT ‘Beginner’ CHECK(VALUE IN (‘Beginner’, ‘Intermediate’, ‘Advanced’)

CSC 240 (Blum)19 Collecting Attributes to make a Table After defining attributes and their domains, one collects the attributes together to create a table. Some of the attributes will play special roles in the table, for instance, they may act as the primary key, a candidate key or a foreign key.

CSC 240 (Blum)20 PRIMARY KEY Recall that no part of a primary key can be NULL and that the primary key must uniquely identify a record (known as Entity Integrity). The keyword PRIMARY KEY specifies that attribute(s) will serve this purpose. PRIMARY KEY(characterID) PRIMARY KEY(episodeID, characterID)

CSC 240 (Blum)21 Candidate Keys Recall that a candidate key is an attribute or set of attributes having the same features as the primary key (uniqueness). To specify that a set of attributes must have this uniqueness property, use the keyword UNIQUE PRIMARY KEY(date, time, room) UNIQUE(date, time, tutee) UNIQUE(date, time, tutor)

CSC 240 (Blum)22 Foreign Keys Recall that a foreign key is an attribute in a table that is either NULL or must match a value from another table (called Referential Integrity). It realizes a relationship between two tables. The SQL is FOREIGN KEY (personID) REFERENCES RealPerson

CSC 240 (Blum)23 What happens to old relationships? A foreign key takes it values from another table (its parent in the relationship). What happens if the parent value is updated or deleted? How will referential integrity be maintained?

CSC 240 (Blum)24 Four choices for maintaining referential integrity CASCADE: make same update to child as made to parent SET NULL: set the child value to NULL when the parent is updated SET DEFAULT: set the child value to its DEFAULT when the parent is updated NO ACTION: prevent parent from being updated if children are affected

CSC 240 (Blum)25 ON DELETE / ON UPDATE Different choices can be specified depending on what is happening to parent FOREIGN KEY (stateSymbol) REFERENCES State ON DELETE NO ACTION FOREIGN KEY (stateSymbol) REFERENCES State ON UPDATE CASCADE

CSC 240 (Blum)26 Enterprise Constraints An enterprise constraint (a.k.a. a business rule) is an additional condition placed on the database that does not fall into one of the previous categories of domain constraint, entity integrity and referential integrity. An example would be allowing a video store customer to have out at most ten items at any time.

CSC 240 (Blum)27 Imposing Enterprise Constraints The SQL keyword ASSERTION is used to impose enterprise constraints. CREATE ASSERTION PreventTooManyItems CHECK( NOT EXISTS(SELECT custID FROM ItemsCurrentlyOut GROUP BY custID HAVING COUNT(*) >10))

CSC 240 (Blum)28 Main DDL statements SQL allows one to create, alter and destroy domains, tables, views and schemas. The main SQL DDL statements are: CREATE SCHEMA, DROP SCHEMA CREATE DOMAIN, ALTER DOMAIN, DROP DOMAIN CREATE TABLE, ALTER TABLE, DROP TABLE CREATE VIEW, DROP VIEW

CSC 240 (Blum)29 Environment/Catalog Database objects exist in an environment. Each environment contains one or more catalogs, and each catalog consists of set of schemas. A schema is a named set of related database objects. Objects in a schema include domains, tables, assertions, views, and so on. All of the objects have the same owner.

CSC 240 (Blum)30 CREATE TABLE TableName {(colName dataType [NOT NULL] [UNIQUE] [DEFAULT defaultOption] [CHECK searchCondition] [,...]} [PRIMARY KEY (listOfColumns),] {[UNIQUE (listOfColumns),] […,]} {[FOREIGN KEY (listOfFKColumns) REFERENCES ParentTableName [(listOfCKColumns)], [ON UPDATE referentialAction] [ON DELETE referentialAction ]] [,…]} {[CHECK (searchCondition)] [,…] }) Define fields Identify primary key Identify candidate keys Identify foreign keys, relationships and actions to maintain referential integrity. Business rules

CSC 240 (Blum)31 CREATE DOMAIN OwnerNumber AS VARCHAR(5) CHECK (VALUE IN (SELECT ownerNo FROM PrivateOwner)); CREATE DOMAIN StaffNumber AS VARCHAR(5) CHECK (VALUE IN (SELECT staffNo FROM Staff)); CREATE DOMAIN PNumber AS VARCHAR(5); CREATE DOMAIN PRooms AS SMALLINT; CHECK(VALUE BETWEEN 1 AND 15); CREATE DOMAIN PRent AS DECIMAL(6,2) CHECK(VALUE BETWEEN 0 AND );

CSC 240 (Blum)32 CREATE TABLE PropertyForRent ( propertyNoPnumber NOT NULL, …. rooms Prooms NOT NULL DEFAULT 4, rentPrent NOT NULL, DEFAULT 600, ownerNo OwnerNumber NOT NULL, staffNo StaffNumber Constraint StaffNotHandlingTooMuch …. branchNo BranchNumberNOT NULL, PRIMARY KEY (propertyNo), FOREIGN KEY (staffNo) REFERENCES Staff ON DELETE SET NULL ON UPDATE CASCADE ….);

CSC 240 (Blum)33 ALTER TABLE The ALTER TABLE keyword is used to change the schema (not the instance) of a table. For example, one can –Add a new column to a table. –Drop a column from a table. –Add a new table constraint. –Drop a table constraint. –Set a default for a column. –Drop a default for a column.

CSC 240 (Blum)34 DML Putting data into the table, changing data in the table and asking questions about data in the table are all part of the Data Manipulation Language (DML) portion of SQL. We have seen the SQL for asking questions (SELECT statements), next we will look at a statement for adding data to a table (an INSERT statement).

CSC 240 (Blum)35 Table before: INSERT a new record

CSC 240 (Blum)36 SQL for: INSERT a new record

CSC 240 (Blum)37 Warning about: INSERT a new record

CSC 240 (Blum)38 Table after: INSERT a new record

CSC 240 (Blum)39 References Database Systems, Connolly and Begg SQL for Dummies, Taylor