14897_bia_unit-2_part-3

67
Creating Databases & Tables, Constraints

Upload: maneesh-sharma

Post on 20-Jul-2016

213 views

Category:

Documents


0 download

DESCRIPTION

1

TRANSCRIPT

Page 1: 14897_bia_unit-2_part-3

Creating Databases & Tables, Constraints

Page 2: 14897_bia_unit-2_part-3

Relational Query Languages

• A major strength of the relational model: supports simple, powerful querying of data.

• Two sublanguages:

• DDL – Data Definition Language – define and modify schema (at all 3 levels)

• DML – Data Manipulation Language – Queries can be written intuitively.

• The DBMS is responsible for efficient evaluation. – The key: precise semantics for relational queries.

– Allows the optimizer to extensively re-order operations, and still ensure that the answer does not change.

– Internal cost model drives use of indexes and choice of access paths and physical operators.

Page 3: 14897_bia_unit-2_part-3

The SQL Query Language • The most widely used relational query language.

• Originally IBM, then ANSI in 1986

• Current standard is SQL-2003

• Introduced XML features, window functions, sequences, auto-generated IDs.

• Not fully supported yet

• SQL-1999 Introduced “Object-Relational” concepts.

Also not fully suppored yet.

• SQL92 is a basic subset

• Most systems support a medium

• PostgreSQL has some “unique” aspects (as do most systems).

Page 4: 14897_bia_unit-2_part-3

Introduction to SQL

• A standard language used in most DBMS. – Well, not as standardized as one might hope

• it keeps involving and growing

• Vendors have the tendency to add “unique” features.

• Pronounced as “S-Q-L” or “Sequel.”

• Both as a DDL and DML language. – DDL (Data Definition Language): define the schema

of the database.

– DML (Data Manipulation Language): provides commands to manipulate the database (query, insert, update, delete).

Page 5: 14897_bia_unit-2_part-3

SQL Continued

• Based on relational algebra, but not entirely identical. – Relations Tables

– Tuples Rows

– Attributes Columns

• Unlike a relation, a table is not a set. Duplicates are not automatically removed. – This is for practical reasons. Duplicate eliminations are

inefficient in implementation.

• Like a relation, the order of rows in a table is irrelevant.

Page 6: 14897_bia_unit-2_part-3

SQL

• It has 9 commands common to all RDBMS – CREATE, DROP, ALTER for Tables. – INSERT, UPDATE, DELETE for records. – GRANT, REVOKE for permission. – SELECT for query.

• SQL was initiated by IBM and is now controlled by ANSI (American National Standard Institute). Also pronounced as SEQUEL.

• It is command language for communication with ORACLE/MS-SQL Server.

• When an SQL statement is entered it is stored in the part of memory called SQL Buffer and remains there until a new SQL statement is entered.

Page 7: 14897_bia_unit-2_part-3

An Overview of SQL

• SQL stands for Structured Query Language.

• It is the most commonly used relational database language today.

• SQL works with a variety of different fourth-generation (4GL) programming languages, such as Visual Basic.

Page 8: 14897_bia_unit-2_part-3

SQL is used for:

• Data Manipulation

• Data Definition

• Data Administration

• All are expressed as an SQL statement or command.

Page 9: 14897_bia_unit-2_part-3

SQL Requirements

• SQL Must be embedded in a programming language, or used with a 4GL like VB

• SQL is a free form language so there is no limit to the the number of words per line or fixed line break.

• Syntax statements, words or phrases are always in lower case; keywords are in uppercase.

Not all versions are case sensitive!

Page 10: 14897_bia_unit-2_part-3

SQL is a Relational Database

• Represent all info in database as tables

• Keep logical representation of data independent from its physical storage characteristics

• Use one high-level language for structuring, querying, and changing info in the database

• Support the main relational operations

• Support alternate ways of looking at data in tables

• Provide a method for differentiating between unknown values and nulls (zero or blank)

• Support Mechanisms for integrity, authorization, transactions, and recovery

A Fully Relational Database Management System must:

Page 11: 14897_bia_unit-2_part-3

Features of SQL

• It can be used by a range of users including those with little or no programming experience.

• It is a non-procedural language.

• It reduces the amount of time required for creating and maintaining system.

• It is English like language

Page 12: 14897_bia_unit-2_part-3

Rules of SQL

• SQL statements starts with a verb (SQL action word) eg. SELECT.

• Each verb is followed by number of clauses eg. FROM, WHERE, HAVING.

• A space separates clause eg. DROP TABLE. • Comma (,)separates parameters without clause. • A semicolon (;) is used to end SQL statement. • Characters and date literals must be enclosed in single

quotes. • Numeric values can be represented by simple values 0.39,

-34, 01991. Scientific notations 2E5 = 2 * 105

Page 13: 14897_bia_unit-2_part-3

Types of Tables

• User Tables: contain information that is the database management system

• System Tables: contain the database description, kept up to date by DBMS itself

There are two types of tables which make up a relational database in SQL

Relation Table

Tuple Row

Attribute Column

Page 14: 14897_bia_unit-2_part-3

Using SQL

SQL statements can be embedded into a program

(cgi or perl script, Visual Basic, MS Access)

OR

SQL statements can be entered directly at the

command prompt of the SQL software being

used (such as mySQL)

Page 15: 14897_bia_unit-2_part-3

SQL Delimiters Addition + Exponential ** Subtraction – Concatenation || Multiplication * Comment -- , /*…*/ Division / Relational =,>,< Expression or List () Terminator ; Item Separator , Character String delimiter ‘ Quote Identifier “

Page 16: 14897_bia_unit-2_part-3

Components of SQL • DDL (Data Definition Language)

– CREATE, ALTER, DROP, TRUNCATE, COMMENT.

• DML (Data Manipulation Language) – INSERT, UPDATE, DELETE, CALL, LOCK TABLE

• DCL (Data Control Language) – COMMIT- Save the work.

– SAVE POINT – Identify a point in a transaction to which you can later on rollback.

– ROLLBACK – restore database to the original since the last COMMIT.

– SET TRANSACTION – Change transaction option like what rollback segment to use.

– GRANT/ REVOKE – Grant or take back permission to or from the users.

Page 17: 14897_bia_unit-2_part-3

• DQL (Data Query Language)

– SELECT – retrieve data from the database

Page 18: 14897_bia_unit-2_part-3

Data types • CHAR (size) – store characters maximum upto 255.

• VARCHAR (size) / VARCHAR2(size) – store alphanumeric data maximum upto 2000 / 4000.

• DATE – To represent Date and Time DD-MM-YY.

• NUMBER(P,S) – Precision (P) maximum length of data , Scale (S) determines number of places to the right of decimal.

• LONG – Variable length character strings containing upto 2 GB.

• CLOB/ BLOB – Character Large Objects/ Binary Large objects to store images, pictures, sounds, video, txt files etc.

Page 19: 14897_bia_unit-2_part-3

Sub Languages of SQL – DDL (Data Definition Language)

Command

Description

CREATE

Creates a new table, a view of a table, or other object in

database

ALTER

Modifies an existing database object, such as a table.

DROP

Deletes an entire table, a view of a table or other object in the

database.

Page 20: 14897_bia_unit-2_part-3

Sub Languages of SQL – DML (Data Manipulation Language)

Command

Description

INSERT

Creates a record

UPDATE Modifies records

DELETE

Deletes records

Page 21: 14897_bia_unit-2_part-3

Sub Languages of SQL – DCL (Data Control Language)

Command

Description

GRANT

Gives a privilege to user

REVOKE

Takes back privileges

granted from user

Page 22: 14897_bia_unit-2_part-3

Sub Languages of SQL – DQL (Data Query Language)

Command

Description

SELECT

Retrieves certain

records from one or more tables

Page 23: 14897_bia_unit-2_part-3

Domain Types

• char(n): fixed length char string

• varchar(n): variable length char string

• int or integer

• smallint

• numeric(p,d): fixed-point number of given precision

• real, double precision

• float(n): floats with a given precision

• date: containing year,month, date

• time: in hours, minutes and seconds

• Null value is part of each domain

• Define new domains:

– create domain person-name char(20)2

Page 24: 14897_bia_unit-2_part-3

Schema Definition

Create table r (

A1 D1 [not null] [default V1]

A2 D2 [not null] [default V2]

An Dn [not null] [default Vn]

<integrity constraint 1>

<integrity constraint 2>

<integrity constraint k>

)

Integrity constraints 1 … k could be:

– primary key

– candidate key

– foreign key

– check(predicate) specifies predicate that must be satisfied by each tuple

Page 25: 14897_bia_unit-2_part-3

SQL as DDL

create table Employee

(

name char[15] not null

age smallint

sex (M,F) default M

ss# integer not null

spouse_ss# integer

dept# integer

)

Primary Key (ssno)

Unique(spouse_ss#)

check (age >=20 and age <=70)

Foreign key (dept#) references Dept(dept#)

on delete cascade

on update cascade

Don’t allow null values

Default value is Male

if dept# updated/deleted in Dept

table, cascade update/delete to

employee-

if null/default instead of cascade,

null/default value taken by dangling

tuples in Employee

Page 26: 14897_bia_unit-2_part-3

SQL as DDL

• Attributes in primary key required to be not null

• Attributes in candidate key can take null values unless specified otherwise

• Unique constraint violated if two tuples have exactly same values for attribues in unique clause provided values are not null

• Null also permitted for attributes in foreign key.

• Foreign key constraint automatically satisfied if even one attribute in foreign key is null.

• Predicate in check clause can be very complex and can include SQL queries inside them

• Other DDL Statements:

– drop table r

– alter table r add A D

– alter table r drop A

Page 27: 14897_bia_unit-2_part-3

Design

• SQL represents all information in the form of tables

• Supports three relational operations: selection, projection, and join. These are for specifying exactly what data you want to display or use

• SQL is used for data manipulation, definition and administration

SQ

L

Page 28: 14897_bia_unit-2_part-3

Rows

describe the

Occurrence of

an Entity

Table Design

Name Address

Jane Doe 123 Main Street

John Smith 456 Second Street

Mary Poe 789 Third Ave

Columns describe one

characteristic of the entity

Page 29: 14897_bia_unit-2_part-3

Basic DDL Commands in SQL

• CREATE: to define new tables (to define relation schemas)

• DROP: to delete table definitions (to delete relation schemas)

• ALTER: to change the definitions of existing tables (to change relation schema)

• Other features as DDL

– Specify referential integrity constraints (FKs)

– Specify user-defined attributes constraints

Page 30: 14897_bia_unit-2_part-3

Data Definition Language

• The schema for each relation, including attribute types.

• Integrity constraints

• Authorization information for each relation.

• Non-standard SQL extensions also allow specification of

– The set of indices to be maintained for each relations.

– The physical storage structure of each relation on disk.

Allows the specification of:

Page 31: 14897_bia_unit-2_part-3

SQL Commands Are Sequential

• Commands are executed in the order they are encountered.

• DDL commands are not like C/Java declarations.

• DDL and DML commands can be mixed – For example, you can define a table, fill it up with

contents, and delete a columns.

– That is, table definitions (relation schema) can be changed during the lifespan of a database. • The ability of doing so does imply it is a good practice.

• It is best the schema/design of a database is well thought through before its use.

Page 32: 14897_bia_unit-2_part-3

MySQL

• Open source and free

• Generally not as powerful as Oracle

– Our projects will not need advanced and/or proprietary features of Oracle.

• Still, it is an industrial strength package.

– Users include Amazon, NASA, Google, Yahoo …

• A commercial edition is also available (MySQL Enterprise) --- You are paying for the services.

Page 33: 14897_bia_unit-2_part-3

Comparison Operators

Page 34: 14897_bia_unit-2_part-3

Defining Search Expressions

• Character strings – Must be enclosed in single quotes

– Case sensitive

• Dates – Use to_date function with date string and format model

• Intervals – Use to_yminterval and to_dsinterval with interval string format model

Page 35: 14897_bia_unit-2_part-3

Slide 35

The COMPANY Database

Page 36: 14897_bia_unit-2_part-3

Using SQL

To begin, you must first CREATE a database using

the following SQL statement:

CREATE DATABASE database_name

Depending on the version of SQL being used

the following statement is needed to begin

using the database:

USE database_name

Page 37: 14897_bia_unit-2_part-3

Using SQL

• To create a table in the current database, use the CREATE TABLE keyword

CREATE TABLE authors

(auth_id int(9) not null,

auth_name char(40) not null)

auth_id auth_name

(9 digit int) (40 char string)

Page 38: 14897_bia_unit-2_part-3

CREATE

• Syntax – CREATE TABLE <Table name> (<columnName1> <DataType>(<size>), (<columnName2> <DataType>(<size>));

• Example:

create table bank_sys ( branch_no varchar2(10), name varchar2(20));

Output- Table Created

Page 39: 14897_bia_unit-2_part-3

Create the COMPANY Database

• To create

create datatbase COMPANY;

• To use (or switch to) the database

use COMPANY;

• Subsequent commands will operate on the COMPANY database by default.

Page 40: 14897_bia_unit-2_part-3

Create Table Construct • An SQL relation is defined using the create table

command:

create table r (A1 D1, A2 D2, ..., An Dn, (integrity-constraint1), ..., (integrity-constraintk)) – r is the name of the relation

– each Ai is an attribute name in the schema of relation r

– Di is the data type of attribute Ai

• Example:

create table branch (branch_name char(15), branch_city char(30),

assets integer)

Page 41: 14897_bia_unit-2_part-3

Domain Types in SQL • char(n). Fixed length character string, with user-specified

length n.

• varchar(n). Variable length character strings, with user-specified maximum length n.

• int. Integer (a finite subset of the integers that is machine-dependent).

• smallint. Small integer (a machine-dependent subset of the integer domain type).

• numeric(p,d). Fixed point number, with user-specified precision of p digits, with n digits to the right of decimal point.

• real, double precision. Floating point and double-precision floating point numbers, with machine-dependent precision.

• float(n). Floating point number, with user-specified precision of at least n digits.

Page 42: 14897_bia_unit-2_part-3

Integrity Constraints on Tables

• not null

• primary key (A1, ..., An )

Example: Declare branch_name as the primary key for branch

.

create table branch

(branch_name char(15),

branch_city char(30) not null,

assets integer,

primary key (branch_name))

primary key declaration on an attribute automatically ensures

not null in SQL-92 onwards, needs to be explicitly stated in

SQL-89

Page 43: 14897_bia_unit-2_part-3

CREATE TABLE

CREATE TABLE DEPARTMENT ( Dname VARCHAR(10) NOT NULL,

Dnumber INTEGER Default 0,

Mgr_ssn CHAR(9),

Mgr_Startdate CHAR(9),

PRIMARY KEY (Dnumber),

UNIQUE (Dname),

FOREIGN KEY (Mgr_ssn)

REFERENCES EMPLOYEE (Ssn));

• The “UNIQUE” clause specifies secondary keys. • EMPLOYE)has to be created first for the FK Mgr_ssn

to refer to it. • How could we have defined the Dno FK in EMPLOYEE?

Page 44: 14897_bia_unit-2_part-3

Additional Data Types

• DATE: – Made up of year-month-day in the format yyyy-mm-dd

• TIME: – Made up of hour:minute:second in the format hh:mm:ss

• TIMESTAMP: – Has both DATE and TIME components

• Decimal (i,j): – i: total number of digits

– j: the number of digits after the decimal point

• Others: Boolean, Float, Double Precision

Page 45: 14897_bia_unit-2_part-3

Adding the Dno FK(Foreign Key) to EMPLOYEE

• If “create table EMPLOYEE” is issued first, we cannot specify Dno as a FK in that create command.

• An ALTER command must be used to change the schema of EMPLOYEE, after the “create table DEPARTMENT,” to add a FK.

alter table EMPLOYEE

add constraint

foreign key (Dno)

references DEPARTMENT (Dnumber);

Page 46: 14897_bia_unit-2_part-3

The Check Clause

• Used to specify user-defined constraints

• Assume that dept. numbers are from 0 to 99.

create table DEPARTMENT (

Dnumber INTEGER Default 0

check (Dnumber>=0 AND Dumber<=99),

…);

• “Check” can also be a clause of the entire table.

create table DEPARTMENT (

Dept_create_date date,

Mgr_start_date date,

check (Dept_create_date <= Mgr_start_date)

);

Page 47: 14897_bia_unit-2_part-3

Using SQL

ALTER TABLE authors

ADD birth_date datetime null

auth_id auth_name auth_city auth_state

123456789 Jane Doe Dearborn MI

000000001 John Smith Taylor MI

To change a table in a database use ALTER TABLE. ADD adds a

characteristic.

ADD puts a new column in the table called birth_date

birth_date

.

.

Type Initializer

Page 48: 14897_bia_unit-2_part-3

Using SQL

ALTER TABLE authors

DROP birth_date

auth_id auth_name auth_city auth_state

123456789 Jane Doe Dearborn MI

000000001 John Smith Taylor MI

To delete a column or row, use the keyword DROP

DROP removed the birth_date characteristic from the table

auth_state

.

.

Page 49: 14897_bia_unit-2_part-3

ALTER Table • Syntax – ALTER Table <Tablename> ADD

(<newcolumnname> <Datatype> (<size>), <newcolumn2> <Datatype2>(size));

• Example:

ALTER TABLE Bank_sys add (address varchar(40));

• Syntax ALTER TABLE <Tablename> DROP COLUMN <Columnname>;

• Example:

ALTER TABLE Bank_sys DROP COLUMN name;

Page 50: 14897_bia_unit-2_part-3

• Syntax – ALTER TABLE <Tablename> MODIFY ( <Columnname> <Newdatatype> (<size>));

• Example:

ALTER TABLE Bank_sys Modify (Branch_no varchar(15));

Page 51: 14897_bia_unit-2_part-3

Add Columns to Existing Tables

• To add spouse SSN (S_ssn) to EMPLOYEE alter table EMPLOYEE add column S_ssn

char(9);

– The new attribute will have NULLs in all the tuples of the relation right after the command is executed

• Alternatively, we can set a default value. alter table EMPLOYEE add column S_ssn char(9)

default “000000000”;

Page 52: 14897_bia_unit-2_part-3

Referential Integrity Options

• Causes of referential integrity violation for a foreign key FK (consider the Mgr_ssn of DEPARTMENT). – On Delete: when deleting the foreign tuple

• What to do when deleting the manager tuple in EMPLOYEE ?

– On Update: when updating the foreign tuple • What to do when updating/changing the SSN of the manager tuple

in EMPLOYEE is changed ?

• Actions when the above two causes occur. – Set Null: the Mgr_ssn is set to null.

– Set Default: the Mgr_ssn is set to the default value.

– Cascade: the Mgr_ssn is updated accordingly • If the manager is deleted, the department is also deleted.

Page 53: 14897_bia_unit-2_part-3

The Mgr_ssn Example

CREATE TABLE DEPARTMENT (

Mgr_ssn CHAR(9),

FOREIGN KEY (Mgr_ssn)

REFERENCES EMPLOYEE (Ssn)

ON DELETE ???

ON UPDATE ???

);

Page 54: 14897_bia_unit-2_part-3

Another Example

Create table EMP(

ESSN CHAR(9),

DNO INTEGER DEFAULT 1,

SUPERSSN CHAR(9),

PRIMARY KEY (ESSN),

FOREIGN KEY (DNO) REFERENCES DEPT

ON DELETE SET DEFAULT

ON UPDATE CASCADE,

FOREIGN KEY (SUPERSSN) REFERENCES

EMP ON DELETE SET NULL

ON UPDATE CASCADE);

Page 55: 14897_bia_unit-2_part-3

Miscellaneous Commands

• show databases;

– Show all the databases on the server

• show tables;

– Show all the tables of the present database

• show columns from table EMPLOYEE;

• drop table t_name;

– Delete the entire table t_name

• drop database db_name;

– Delete the entire database db_name • load data infile f_name into table t_name;

– To be discussed with the next homework.

Page 56: 14897_bia_unit-2_part-3

More on SQL

Constraints

Triggers

Page 57: 14897_bia_unit-2_part-3

Integrity Constraints (Review)

• Constraint describes conditions that every legal instance of a relation must satisfy. – Inserts/deletes/updates that violate ICs are disallowed.

– Can be used to :

• ensure application semantics (e.g., sid is a key), or

• prevent inconsistencies (e.g., sname has to be a string, age must be < 200)

• Types of IC’s: – Fundamental: Domain constraints, primary key constraints, foreign key

constraints

– General constraints : Check Constraints, Table Constraints and Assertions.

Page 58: 14897_bia_unit-2_part-3

Check or Table Constraints

• Can use queries to express constraint.

CREATE TABLE Sailors

( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL, PRIMARY KEY (sid),

CHECK ( rating >= 1

AND rating <= 10 ))

Page 59: 14897_bia_unit-2_part-3

Explicit Domain Constraints

CREATE TABLE Sailors

( sid INTEGER, sname CHAR(10), rating values-of-ratings, age REAL, PRIMARY KEY (sid))

CREATE DOMAIN values-of-ratings INTEGER

DEFAULT 1

CHECK ( VALUE >= 1 AND VALUE <= 10)

Page 60: 14897_bia_unit-2_part-3

More Powerful Table Constraints

CREATE TABLE Reserves

( sname CHAR(10), bid INTEGER, day DATE, PRIMARY KEY (bid,day),

CONSTRAINT noInterlakeRes

CHECK (`Interlake’ <>

( SELECT B.bname

FROM Boats B

WHERE B.bid= bid)))

Constraint that Interlake boats cannot be reserved:

If condition evaluates to FALSE, update is rejected.

Page 61: 14897_bia_unit-2_part-3

Table Constraints

Associated with one table

Only needs to hold TRUE when table is non-empty.

Page 62: 14897_bia_unit-2_part-3

Table Constraints with Complex CHECK

CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL, PRIMARY KEY (sid), CHECK ( (SELECT COUNT (S.sid) FROM Sailors S) + (SELECT COUNT (B.bid) FROM Boats B) < 100 )

• Symmetric constraint, yet associated with Sailors.

• If Sailors is empty, the number of Boats tuples can be anything!

Number of boats plus number of sailors is < 100

Page 63: 14897_bia_unit-2_part-3

Assertions ( Constraints over Multiple Relations)

CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL, PRIMARY KEY (sid), CHECK ( (SELECT COUNT (S.sid) FROM Sailors S) + (SELECT COUNT (B.bid) FROM Boats B) < 100 )

• ASSERTION not associated with either table.

CREATE ASSERTION smallClub CHECK ( (SELECT COUNT (S.sid) FROM Sailors S) + (SELECT COUNT (B.bid) FROM Boats B) < 100 )

Number of boats plus number of sailors is < 100

Page 64: 14897_bia_unit-2_part-3

Using SQL

DROP DATABASE authors

auth_id auth_name auth_city auth_state

123456789 Jane Doe Dearborn MI

000000001 John Smith Taylor MI

The DROP statement is also used to delete an entire database.

DROP removed the database and returned the memory to system

Page 65: 14897_bia_unit-2_part-3

Summary

• SQL allows specification of rich integrity constraints and their efficient maintenance

Page 66: 14897_bia_unit-2_part-3

Drop and Alter Table Constructs

• The drop table command deletes all information about the dropped relation from the database.

• The alter table command is used to add attributes to an existing relation:

alter table r add A D

where A is the name of the attribute to be added to relation r and D is the domain of A.

– All tuples in the relation are assigned null as the value for the new attribute.

• The alter table command can also be used to drop attributes of a relation:

alter table r drop A

where A is the name of an attribute of relation r

– Dropping of attributes not supported by many databases

Page 67: 14897_bia_unit-2_part-3

Conclusion

• SQL is a versatile language that can integrate with numerous 4GL languages and applications

• SQL simplifies data manipulation by reducing the amount of code required.

• More reliable than creating a database using files with linked-list implementation