Chapter 6 SQL. Agenda Data Definition Language (DDL) Access Control.

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
Advertisements

SQL Rohit Khokher.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Database Design -- Basic SQL
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 SQL: Data Definition, Constraints, and Basic Queries and Updates.
Chapter 6 SQL. Agenda Data Definition Language (DDL) Access Control.
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.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database.
Database Management System LICT 3011 Eyad H. Elshami.
CSC 240 (Blum)1 Data Definition Language Based on Chapter 6 of Database Systems (Connolly and Begg)
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.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
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.
SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley.
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.
Chapter 6 SQL Data Definition Language Chapter 7 in Textbook.
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.
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.
-Software School of Hunan University-
SQL Data Definition Language (DDL) Using Microsoft SQL Server 1SDL Data Definition Language (DDL)
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 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
1 Creating and Modifying Database Objects. 2 An Oracle database consists of multiple user accounts Each user account owns database objects Tables Views.
SQL: DDL John Ortiz Cs.utsa.edu.
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.
CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
Chapter Name SQL: Data Definition
Chapter 6 SQL. Agenda Data Definition Language (DDL) Access Control.
1 Chapter 2: Creating and Modifying Database Objects.
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.
SQL Overview Structured Query Language
©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.
Oracle 9i. Agenda Start and exit SQL Plus (General) Start and exit SQL Plus (Tah 1006) Syntax Create a new user Create a new table Enter data into a new.
1 CS 430 Database Theory Winter 2005 Lecture 10: Introduction to SQL.
Branch (Bno, Area, City, Pcode) Staff (Sno, FName, LName, Position, Sex, DOB, Salary, Bno) Property_for Rent (Pno, Area, City, Type, Rooms, Rent, Ono,
Data Definition Language
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows.
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.
國立臺北科技大學 課程:資料庫系統 Chapter 7 SQL Data Definition.
SQL: Data Definition Transparencies
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
CS 3630 Database Design and Implementation
Chapter 7 SQL – Data Definition Pearson Education © 2014.
Minggu 5, Pertemuan 9 SQL: Data Definition
STRUCTURED QUERY LANGUAGE
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:

Chapter 6 SQL

Agenda Data Definition Language (DDL) Access Control

String Data Types Boolean data: true, false, null, unknown value (true > false) Character data –Fixed length: CHAR(4) –Variable length: VARCHAR(30) Bit data: binary string –BIT(4)

Numeric Data Types NUMERIC or NUMBER [precision, [scale]] DECIMAL or DEC [precision, [scale]] INTEGER or INT SMALLINT (32,767) FLOAT [precision] REAL DOUBLE PRECISION

Datetime data DATE: year, month, day (DD-MON-YY) TIME [timeprecision (for second, default=0)] [with time zone (control hour and minute)]: hour:minute:second a.m. or p.m. TIMESTAMP [timeprecision (for second, default=6] [with time zone (control hour and minute)]: date and times (DD-MON-YY hour:second:minute a.m. or p.m.)

Integrity Enhancement Feature Required data Domain constraint Entity integrity constraint Referential integrity constraint Enterprise constraint

Required Data Position VARCHAR(10) NOT NULL

Domain Constraint - I CHECK (search condition) –sex CHAR(1) NOT NULL CHECK (VALUE IN (‘m’, ‘f’)) –credit AS NUMBER(3) CHECK (VALUE BETWEEN 0 AND 999) – major AS CHAR(3) DEFAULT ‘mis’ CHECK (VALUE IN (‘mis’, ‘man’, ‘act’, ‘obe’))

Domain Constraint - II CREATE DOMAIN domain name [AS] data type [DEFACULT default option] [CHECK (VALUE IN (search condition))] –CREATE DOMAIN sextype AS CHAR(1) DEFACULT ‘m’ CHECK (VALUE IN (‘m’,’f’)); –sex sextype NOT NULL –CREATE DOMAIN cnumber AS CHAR(2) CHECK (VALUE IN (SELECT cno FROM customer)); –cid cnumber NOT NULL DROP DOMAIN domain name [RESTRICT l CASCADE] –DROP DOMAIN cnumber;

Entity Integrity PRIMARY KEY key name or key names NOT NULL –PRIMARY KEY sno NOT NULL –sno INTEGER NOT NULL UNIQUE –PRIMARY KEY (sno, classno, sdate) NOT NULL

Referential Integrity Referential action for ON UPDATE and ON DELETE –CASCADE –SET NULL –SET DEFAULT –NO ACTION Examples –FOREIGN KEY hotelno REFERENCES hotel (hotelno) –FOREIGN KEY hotelno REFERENCES hotel (hotelno) ON DELETE SET NULL –FOREIGN KEY hotelno REFERENCES hotel (hotelno) ON UPDATE CASCADE

Enterprise Constraint Methods –CHECK clause –UNIQUE clause –CREATE ASSERTION statement CREATE ASSERTION assertion name CHECK (assertion condition): for defining attribute contraint CREATE ASSERTION toomuch CHECK (NOT EXIST (SELECT sno FROM enroll GROUP BY sno HAVING COUNT (*) >10));

SQL DDL CREATE ALTER DROP

CREATE SCHEMA DOMAIN TABLE INDEX VIEW

ALTER TABLE DOMAIN

DROP SCHEMA DOMAIN TABLE INDEX VIEW

CREATE Schema CREATE SCHEMA [name | AUTHORIZATION creator-id] DROP SCHEMA name [RESTRICT | CASCADE] Examples –CREATE SCHEMA mis150 AUTHORIZATION tsai; –DROP SCHEMA mis150;

SQL DDL For Table CREATE TABLE table-name (colm data- type [NOT NULL][UNIQUE] [DEFAULT option][CHECK search- cond][,...],[PRIMARY KEY (colm [,colm])],[FOREIGN KEY (colm [,colm]) REFERENCES (parent-table)[colms])

Create A New Table Example CREATE TABLE student (stuid NUMBER(5) NOT NULL CHECK (VALUE BETWEEN AND 99999), stuname CHAR(10), major CHAR(10), credit NUMBER(3), CONSTRAINT pkstudent PRIMARY KEY (stuid));

Create A New Table Example CREATE TABLE faculty (facid NUMBER (5) NOT NULL, facname CHAR(10), dept CHAR(10), rank CHAR(3) CHECK (VALUE IN (‘F’,’Aso’, Ast’)), CONSTRAINT pkfaculty PRIMARY KEY (facid));

Create A New Table Example CREATE TABLE class (course# NUMBER(5) NOT NULL, facid NUMBER(5), sched CHAR(10), room CHAR(10), CONSTRAINT pkclass PRIMARY KEY (course#), CONSTRAINT fkclassfaculty FOREIGN KEY (facid) REFERENCES faculty (facid));

Create A New Table Example CREATE TABLE enrollment (course# NUMBER(5) NOT NULL, stuid NUMBER(5) NOT NULL, grade CHAR(10), CONSTRAINT pkenroll PRIMARY KEY (course#, stuid), CONSTRAINT fkenrollclass FOREIGN KEY (course#) REFERENCES class (course#), CONSTRAINT fkenrollstudent FOREIGN KEY (stuid) REFERENCES student (stuid));

Create A New Table Example CREATE DOMAIN PropertyNo AS SMALLINT; CREATE DOMAIN StaffNo AS CHAR(5) CHECK (VALUE IN (SELECT Sno FROM Staff); CREATE DOMAIN Prent AS DECIMAL(6,2) CHECK (VLAUE BETWEEN 0 AND ); CREATE TABLE PropertyForRent (Pno PropertyNo NOT NULL, Sno StaffNo CONSTRAINT StaffNotTooMuch CHECK (NOT EXIST (SELECT Sno FROM PropertyForRent GROUP BY Sno HAVING COUNT (*) >10)) NOT NULL, Rent Prent NOT NULL, CONSTRAINT pkPropertyForRent PRIMARY KEY (Pno), CONSTRAINT fkPropertyForRentStaff FOREIGN KEY (Sno) REFERENCES Staff (Sno));

SQL DDL For Table DROP TABLE table-name [RESTRICT | CASCADE]; ALTER TABLE table-name [ADD][MODIFY][COLUMN] colm data-type [NOT NULL][UNIQUE] [DEFAULT option][CHECK search- cond][,...][DROP [COLUMN]colm [RESTRICT|CASCADE]

Example ALTER TABLE enrollment MODIFY (grade NUMBER(3)); ALTER TABLE enrollment ADD (datetake DATE not null); DROP TABLE enrollment;

SQL DDL For Table ALTER TABLE table-name [ADD [CONSTRAINT[constrnt-name]]table- constrnt][DROP CONSTRAINT constrnt-name][RESTRICT | CASCADE]

CREATE TABLE customer (lastname CHAR(20) NOT NULL, firstname CHAR(20) NOT NULL, customerid NUMBER(5) NOT NULL, address CHAR(10)); ALTER TABLE customer ADD (CONTRAINT pkcustomer PRIMARY KEY (lastname, firstname)); ALTER TABLE customer DROP CONSTRAINT pkcustomer; ALTER TABLE customer ADD (CONTRAINT pkcustomer PRIMARY KEY (customerid));

Example CREATE TABLE purchaseorder (ponumber CHAR (5) NOT NULL, podate DATE) CONTRAINT pkpurchasorder PRIMARY KEY (ponumber)); CREATE TABLE puchaseorderlineitem (ponumber CHAR(5) NOT NULL, lineitem CHAR(5) NOT NULL, quantity NUMBER(5) NOT NULL, CONTRAINT pkpuchaseorderlineitem PRIMARY KEY (ponumber, lineitem)); ALTER TABLE puchaseorderlineitem ADD (CONSTRAINT fkpolineitem FOREIGN KEY (ponumber) REFERENCES purchaseorder (ponumber)); ALTER TABLE puchaseorderlineitem DROP (CONSTRAINT fkpolineitem);

SQL DDL For Index CREATE [UNIQUE] INDEX index- name ON base-table-name (column [ASC | DESC] [,... ) DROP INDEX index-name

Example CREATE INDEX studentname ON student (stuname DESC); CREATE INDEX majorcredit ON student (major, credit); DROP INDEX majorcredit;

SQL DDL For View CREATE VIEW view-name [(view- colm [,...])] AS SELECT... [WITH [CASCADE|LOCAL] CHECK OPTION] DROP VIEW view-name [RESTRICT | CASCADE]

Example CREATE VIEW substudent (studentid, studentname, major) AS SELECT stuid, stuname, major FROM student; CREATE VIEW productprofit (productid, productname, profit) AS SELECT productid, productname, sale-cost FROM inventory; DROP VIEW productprofit;

Advantage of View Data independence Currency Improved security Reduced complexity Convenience Customization Data integrity

Disadvantage of View Update restriction Structure restriction (new attribute in the base table) Performance (complex views)

Access Control GRANT system-privilege TO role-name GRANT object-privilege ON [owner.] object-name TO role-name GRANT role-name TO user-name REVOKE privilege FROM role-name REVOKE role-name FROM user-name

System Privileges CREATE TABLE CREATE VIEW CREATE USER ALTER INDEX

Object Privileges SELECT INSERT UPDATE DELETE

Examples GRANT CREATE TABLE, CREATE VIEW, CREATE USER TO manager; GRANT SELECT, INSERT, UPDATE, DELETE ON student TO manager; GRANT manger TO tsai;

REVOKE DELETE ON student FROM manager; REVOKE manager FROM tsai;

Student (Stuid, Stuname, Major, Credits) Class (Course#, Facid, Sched, Room) Faculty (Facid, Facname, Dept, Rank) Enrollment (Course#, Stuid, Grade)

Branch (Bno, Area, City) Staff (Sno, Name, Position, Sex, Salary, Bno) Property_for Rent (Pno, Area, City, Type, Rooms, Rent, Ono, Sno, Bno) Renter (Rno, Name, Max_Rent) Owner ( Ono, Name,) Viewing (Rno, Pno, Date)

Define City for Branch according to the following constraints: –Has to have a two character string value –Has to be equal to SF, NY, LA, or DC –Set default value to SF

Define Sno for Staff according to the following constraints: –Has to have a three digits integer –Has to be between 111 and 999 –Has to be unique

Define a domain citylocation for City according to the following constraints: –Has a two character string value –Has to be equal to SF, NY, LA, or DC –Set default value to SF Define the City in the Branch or Property_for_Rent

Define a domain noforsno according to the following constraints: –Has to have a three digits integer –Has to be equal to one of the sno in the Staff Define the sno in the Property_for_rent using the domain noforsno

Define an assertion that will limit 10 staff members in any branch

Create staff table using following constraints –Sno is three digits integer, between 111 and 999, a primary key –Name and position are 30 alphanumeric fields –Sex has a default value (m) limits to m or f –Salary is between 50,000 and & 100,000 –Bno is equal to one value of bno in branch with 10 or less staff member, set null for delete, set cascade for update

Points To Remember Structured Query Language (SQL) Data Definition Language (DDL) Access Control

Assignment Review chapters 1-4, 5, and appendix C Read chapters M-2, M-3 Homework assignment –6.10, 6.11(a, b, c, d), 6.12, 6.13, 6.14 –Due date: