1 Database Management Systems Physical Database Design - SQL (Part 1)

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Virtual training week 4 structured query language (SQL)
Database Systems: Design, Implementation, and Management Tenth Edition
Introduction to Structured Query Language (SQL)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 SQL: Data Definition, Constraints, and Basic Queries and Updates.
Introduction to Structured Query Language (SQL)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Introduction to Structured Query Language (SQL)
CONSTRAINTS AND UPDATES CHAPTER 3 (6/E) CHAPTER 5 (5/E) 1.
Oracle Data Definition Language (DDL)
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
Database Lecture # 1 By Ubaid Ullah.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
The Relational Model These slides are based on the slides of your text book.
ASP.NET Programming with C# and SQL Server First Edition
CSE314 Database Systems Lecture 4 Basic SQL Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.
RDB/1 An introduction to RDBMS Objectives –To learn about the history and future direction of the SQL standard –To get an overall appreciation of a modern.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
Database Technical Session By: Prof. Adarsh Patel.
Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
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.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Constraints, Triggers and Index James Wang.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
SQL FUNDAMENTALS SQL ( Structured Query Language )
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
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.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
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.
Oracle 11g: SQL Chapter 4 Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
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.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
IS6146 Databases for Management Information Systems Lecture 3: SQL III – The DDL Rob Gleasure robgleasure.com.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
SQL Basics Review Reviewing what we’ve learned so far…….
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Getting started with Accurately Storing Data
Fundamentals of DBMS Notes-1.
Fundamental of Database Systems
TABLES AND INDEXES Ashima Wadhwa.
SQL: Schema Definition and Constraints Chapter 6 week 6
SQL data definition using Oracle
Oracle Data Definition Language (DDL)
SQL-1 Week 8-9.
INTRODUCTION A Database system is basically a computer based record keeping system. The collection of data, usually referred to as the database, contains.
Presentation transcript:

1 Database Management Systems Physical Database Design - SQL (Part 1)

2 What Is SQL? A relational database language –It is not a programming language but a comprehensive database sub-language language for controlling and interacting with a database management system. NOT a DBMS

3 What Is SQL? A powerful data manipulation language –It has capabilities for: insertion update deletion query Protection Also designed for end users Non-procedural –We have to show ‘what is needed’ and not ‘how’, like in ‘relational algebra’ –Is similar more to ‘relational calculus’ Used in two ways: –Interactive –Programmatic: Dynamic / Embedded

4 Role of SQL A database programming language A database administration language A client/server language A distributed database language

5 Standard versions of SQL SQL-86 or SQL1 SQL-92 or SQL2 SQL3 (will extend SQL with object oriented and other recent developments)

6 Role of SQL It is vendor independent. –If a user was dissatisfied with a particular DBMS he could switch products easily without much overhead, as both would follow the same language standard. Client applications relatively portable. Programmer skills are portable. Supports many different client processes -- end- users, applications, developers, etc. Database servers use SQL to request services from each other.

7 SQL Basics Data Definition Language (DDL) CREATE TABLEAdds new table DROP TABLERemoves existing tables ALTER TABLEModifies structure of tables CREATE VIEWAdds a new view DROP VIEWRemoves a view CREATE INDEXBuild an index for a column DROP INDEXRemoves an index CREATE SYNONYMDefines an alias for a database object DROP SYNONYMRemove an alias COMMENTSDescribes a table or column LABELDefines a title for a table or column DDL defines the database: Physical Design

8 Data Manipulation Language (DML) More Data Manipulation SQL Basics (cont’d) SELECTRetrieves data INSERTAdds new rows of data DELETERemoves row of data UPDATEModifies existing data DECLAREDefines a cursor for query EXPLAINDescribes data access for a query OPENOpens a cursor to retrieve query results FETCHRetrieves a row of query CLOSECloses a cursor DML load the database: Implementation

9 Data Control Language (DCL) SQL Basics (cont’d) GRANTGives user access privileges REVOKERemoves privileges COMMITEnds current transaction ROLLBACKAborts current transaction DCL control the database: Maintenance

10 SQL Basics ANSI/ISO SQL Keywords –ANSI/ISO specifies standard SQL keywords which cannot be used to name databases objects like tables, columns and users. Note: Keywords used may varies with the different implementations of SQL Reserved

11 Example of some keywords: ALLCOUNTFOUNDMAXPRIVILEGES ANDCREATEFROMMINREFERENCE AVGDEFAULT GONOTROLLBACK BEGINDELETEGRANTNULLSELECT BETWEENDISTINCT GROUPNUMERICSET BYENDHAVINGOFSQL CEXECINORTABLE CHARFETCHINSERTORDERVIEW CHARACTERFLOATINTPASCAL COBOLFOREIGNINTEGERPRECISION COMMITFORTRANKEYPRIMARY Note: Keywords used may varies with the different implementations of SQL

12 Examples of ANSI/ISO SQL Data Types Note: Data types may varies in different implementations of SQL

13 Examples of Extended Data Types –Variable-length character strings (VARCHAR) –Money Amount (MONEY / CURRENCY) –Dates and Times (DATE / TIMESTAMP) –Boolean Data (LOGICAL) –Long Text (LONG / TEXT) –Unstructured Data (RAW) –Asian Characters Data type differences across SQL implementations is one barrier to portability

14 SQL for Data Integrity Value of Stored Data can be lost in many ways: –Invalid data added to data base –Existing data modified to a incorrect value –Changes made lost due to system error or power failure –Changes partially applied Data Integrity

15 SQL for Data Integrity Types of integrity constraints: –Required Data (NOT NULL) –Validity Checking (CHECK) –Entity Integrity (PRIMARY KEY & NOT NULL) –Referential Integrity (FOREIGN KEY) –Business Rules (ASSERTION, TRIGGER) –Consistency (CASCADE, RESTRICT, SET NULL) Data Integrity

16 NULL Values –What are Null values? Null values provides a systematic way of handling missing or inapplicable data in SQL. It is inevitable that in real-world, some data are missing, not yet known or do not apply. Null value is not a real data value.

17 NULL Values (cont’d) –Special Handling Null values require special handling by SQL and the DBMS. Null values can be handled inconsistently by various SQL products Example: How do we handle null values in summaries like SUM, AVERAGE, etc.?

18 Referential Integrity Referential integrity constraints define the rules for associating rows with each other, i.e. columns which reference columns in other tables: Every non-null value in a foreign key must have a corresponding value in the primary key which it references. A row can be inserted or a column updated in the dependent table only if (1) there is a corresponding primary key value in the parent table, or (2) the foreign key value is set null. Department (Parent Table) Dept-No D1 D3 D2 D7 Employee(Dependent Table) Dept-No D7 ? D1 D3 ? D7 Emp-No D2 INSERT ROW UPDATE COLUMN

19 Referential Integrity Deleting parent rows Database designers must explicitly declare the effect if a delete from the parent table on the dependent table: CASCADE deletes associated dependent rows RESTRICT will not allow delete from the parent table if there are associated dependent rows. SET NULL sets the value of associated dependent columns to null values. SET DEFAULT Department (Parent Table) Dept-No D1 D3 D2 D7 Employee(Dependent Table) Dept-No D7 ? D1 D3 ? D7 Emp-No D2 DELETE ROW CASCADE RESTRICT SET NULL

20 SQL for Data Definition Commands: –CREATE –DROP –ALTER SQL also allows for: –Definition of indexes to make table access faster –Control of physical storage by DBMS SQL lets a user define the structure and organisation of the stored data and relationships among the stored data items.

21 Command: CREATE Function: Define new objects ( database, schema, location, table, index and views) CREATE DATABASE initial allocation of storage space to contain database objects (not in SQL-92) CREATE SCHEMA define a portion of a database that a particular user owns CREATE LOCATION defines the location of the database (distributed systems) CREATE TABLE defines a new table CREATE INDEX defines an index that enables rapid access CREATE VIEW defines logical table from one or more tables or views

22 Command:CREATE TABLE Function: Defines a new table and its columns CREATE TABLE table-name(column-definition) (primary-key-definition) (foreign-key-definition) (uniqueness-constraint) (check-constraint) column definition:column-name data-type {NOT NULL} {WITH DEFAULT} primary-key:PRIMARY KEY (column-name) foreign-key:FOREIGN KEY {rel-name} (column-name) REFERENCES table-name {ON DELETE [RESTRICT, CASCADE, SET NULL]} uniqueness:UNIQUE (column-name) checkCHECK(expression)

23 CREATE TABLE Employee (Emp_NoCHAR(5)NOT NULL, NIDCHAR(10), AddressVARCHAR(50), SalaryCHAR(5)CHECK (Salary >= 0), GenderCHAR(1)CHECK (Gender IN (‘M’, ’F’)), DOBDATE, First_NameCHAR(10), Mid_InitialsCHAR(10), Last_NameCHAR(15)NOT NULL, Dept_NoCHAR(2)NOT NULL, SupervisorCHAR(5), PRIMARY KEY (Emp_No), FOREIGN KEY (Dept_No) REFERENCES Department, FOREIGN KEY (Supervisor) REFERENCES Employee); Employee(Emp_No, NID, Address, Salary, Gender, DOB, First_Name, Mid_Initials, Last_Name, Dept_No, Supervisor)

24 CREATE TABLE Dependent (Emp_No CHAR(4)NOT NULL, Depd_NameCHAR(15)NOT NULL, GenderCHAR(1)CHECK (Gender IN (‘M’, ’F’)), DOBDATENOT NULL, RelationVARCHAR(15), PRIMARY KEY (Emp_No, Depd_Name), FOREIGN KEY (Emp_No) REFERENCES Employee ON DELETE CASCADE); Dependent(Emp_No, Depd_Name, Gender, DOB, Relation) CONSTRAINT Dependent_PK PRIMARY KEY (Emp_No, Depd_Name) CONSTRAINT Dependent_FK FOREIGN KEY (Emp_No) REFERENCES Employee (Emp_No)

25 Single-field constraint: CONSTRAINT name {PRIMARY KEY | UNIQUE | NOT NULL | REFERENCES foreign-table [(foreignfield1, foreignfield2)]} Multiple-field constraint: CONSTRAINT name {PRIMARY KEY (primary1[, primary2 [,...]]) | UNIQUE (unique1[, unique2 [,...]]) | NOT NULL (notnull1[, notnull2 [,...]]) | FOREIGN KEY (ref1[, ref2 [,...]]) REFERENCES foreign-table [(foreignfield1 [, foreignfield2 [,...]])]}

26 Command: CREATE Create Index Command CREATE [UNIQUE] INDEX index-name ON table-name (field [ASC|DESC][, field [ASC|DESC],...]) [WITH { PRIMARY | DISALLOW NULL | IGNORE NULL }] Defines an index on one column or a concatenation of columns that enables rapid access to the rows of a table in a sequence or randomly by key value. A table many have many indexes and each may be specified to be unique (primary key) or not and may be sequenced in ascending or descending order

27 SCHEMA Early versions did not include this concept of a relational database schema, all tables were considered part of the same schema. This concept is used in SQL2 to group tables and other constructs that belong to the same database application. A schema is composed of –A schema name –Authorization identifier (To indicate user-account who own the schema) –Schema elements (tables, constraints,views,domans etc…)

28 Schema cont…. CREATE TABLE COMPANY AUTHORIZATION SMITH CREATE TABLE COMPANY.EMPLOYEE… Will make the employee table part of the company schema.

29 CREATE DOMAIN We have seen that there can be various data types used. When specifying the data type of an attribute we can specify directly or a domain can be specified. Eg:- –CREATE DOMAIN SSN_TYPE AS CHAR(9) –CREATE TABLE E(FNAME SSN_TYPE NOT NULL);

30 Command: CREATE (cont’d) index-name: Uniquely identifies the index table-name: Name of table being indexed column-name: Name of column(s) on which index is created. A limit is often imposed on the number of columns that can be used in a compound index (E.g. 64) integer: A whole number from 0 to 99, specifying the percentage of index space to be left free number-records: Duplicate block size

31 Command: CREATE (cont’d) –CREATE UNIQUE INDEX Dept_Name_IDX ON Department (Dept_Name) –CREATE INDEX Name_IDX ON Employee (Last_Name) –CREATE INDEX Emp_Name_IDX ON Employee (First_Name, Mid_Initials, Last_Name) Example:

32 Command: DROP Function: –Remove (erase) an existing object that is no longer needed DROP Command DROP [ table-name | index-name | view-name ] Note:Most RDBMSs will ensure that users dropping different kinds of objects must possess the authority (privileges)

33 Command: DROP (cont’d) –DROP TABLE Dependent –DROP INDEX Name_IDX –DROP VIEW Emp_VIEW Example:

34 Command: ALTER Function: –Change the definition of an existing table. ALTER TABLE table-name { option(s) } {ADD column-name data-type {NOT NULL} {WITH DEFAULT}, |DELETE column-name [,....] |RENAME old-column-name new-column-name [,....] |MODIFY column-name column-type |UNIQUE KEY key-name (column-list) |PRIMARY KEY key-name (column-list) |FOREIGN KEY [constraint-name ] (column-list) REFERENCES table-name [ON DELETE {RESTRICT | CASCADE | SET NULL} ] |DROP PRIMARY KEY |DROP FOREIGN KEY constraint-name ] |DROP KEY key-name ] |DROP CHECK}

35 Command: ALTER (cont’d) Adding a Column ALTER TABLE Project ADD Proj_Manager CHAR(5) Changing Primary or Foreign Key ALTER TABLE Department DROP PRIMARY KEY PRIMARY KEY (Dept_Name) FOREIGN KEY (Manager) REFERENCES Employee Example:

36 Referential Integrity SQL data definition for defining referential integrity constraints Parent table: CREATE TABLE DEPARTMENT (DEPT-NO CHAR(3), other column definitions PRIMARY KEY (DEPT-NO) );

37 Dependent table: CREATE TABLE EMPLOYEE (EMP-NO CHAR(5), DEPT-NO CHAR(3) other column definitions PRIMARY KEY (EMP-NO), FOREIGN KEY DEPT-N-FK (DEPT-NO) REFERENCES DEPARTMENT ON DELETE SET NULL) ); Defining referential integrity rules in the SQL DDL is known as declarative referential integrity Declarative referential integrity simplifies application programming and enables enforcement at the database server level, eliminating the possibility of programming errors

38 User Defined Integrity User defined integrity constraints can be enforced by the database server using triggers and stored procedures. Triggers and stored procedures are user written routines which are stored and executed under the control of the database server. They are often coded in proprietary procedural extensions to SQL, e.g. Sybase's Transact SQL or Oracle's PL/SQL.