Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)

Slides:



Advertisements
Similar presentations
9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
Advertisements

Data Definition Language (DDL)
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
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.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
1 A GUIDE TO ORACLE8 CHAPTER 2: Creating and ModifyingDatabaseTables 2.
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 Chapter 2: Creating and Modifying Database Tables.
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.
Database Management System LICT 3011 Eyad H. Elshami.
Oracle Data Definition Language (DDL)
Structured Query Language S Q L. What is SQL It is a database programming language developed by IBM in the early 1970’s. It is used for managing and retrieving.
Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
Chapter Four Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
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.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes
1 Creating and Modifying Database Objects. 2 An Oracle database consists of multiple user accounts Each user account owns database objects Tables Views.
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
10-1 Copyright  Oracle Corporation, All rights reserved. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns.
SQL: DDL John Ortiz Cs.utsa.edu.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter Eight Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Copyright  Oracle Corporation, All rights reserved. 11 Including Constraints.
Copyright  Oracle Corporation, All rights reserved. Introduction.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Schema Objects.
11 Including Constraints Objectives At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints At the.
8 Copyright © 2007, Oracle. All rights reserved. Managing Schema Objects.
1 SQL-2 Tarek El-Shishtawy Professor Ass. Of Computer Engineering.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
1 Chapter 2: Creating and Modifying Database Objects.
Sql DDL queries CS 260 Database Systems.
Copyright © 2004, Oracle. All rights reserved. Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes ORACLE.
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.
Creating and Managing Tables 14. ObjectivesObjectives After completing this lesson, you should be able to do the following: After completing this lesson,
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Chapter Five Objectives Insert Data into tables Create Query files to insert data into tables Make changes to the data in the tables Extract data from.
Database Programming Sections 9 & 10 – DDL Data Definition Language,
Chapter 3 Table Creation and Management Oracle 10g: SQL.
Installation Oracle 11g Express 2 double click the "setup" button to install the Oracle.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
Chapter Four Objectives Introduction to SQL Introduction to iSQL*PLUS Types of SQL statements Concepts of DDL & DML Data Manipulation Language (DML)
Creating and Managing Tables. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically represents subsets.
7 Copyright © 2004, Oracle. All rights reserved. Managing Schema Objects.
2 Copyright © 2009, Oracle. All rights reserved. Managing Schema Objects.
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
Managing Objects with Data Dictionary Views
SQL data definition using Oracle
SQL Statements SELECT INSERT UPDATE DELETE CREATE ALTER DROP RENAME
Oracle Data Definition Language (DDL)
SQL-1 Week 8-9.
Chapter 2: Creating And Modifying Database Tables
SQL (Structured Query Language)
Presentation transcript:

Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL) Data Manipulation Language (DML)

2 SQL Structured Query Language Developed by IBM Used in most Commercial DBMS Statements are not case sensitive. Statements can be on one or more lines. Reserved words cannot be abbreviated or split over lines. Terminated with a semi colon. Statements are entered at SQL prompt. The subsequent lines are numbered (SQL buffer) Only one statement can be current at any time in the buffer.

3 Data Definition Language (DDL) Data Manipulation Language (DML) Transaction Control Session Control System Control Embedded SQL Types of SQL Statements

4 Example : Student(Name, ID, GPA, Major, B_Date) Course(C_Num, Dept, Title, Cr) Student_Course(ID, C_Num, Dept, Grade) Faculty(ID, Name, Dept, Salary, Area) Faculty_Course(ID, C_Num, Dept, Semester) Department(Name, Num_Faculty)

5 Data Definition Language (DDL) Format: CREATE TABLE[schima.]Table_Name (AttributeAttribute_Type [DEFAULT expr] [Col_constraint], …. AttributeAttribute_Type [Table_constraint]);

6 Data Definition Language (DDL) Example: SQL> CREATE TABLE student 2(NameVARCHAR2(80), 3IDNUMBER(9), 4GPANUMBER(3,2), 5B_DateDATE, 6MajorCHAR(4) 7);

7 Data Definition Language Name: User Identifiers: 1-30 characters Start with an alphabet Followed by alphabet, digit, _ Unique Not reserved Not case sensitive

8 Oracle Data Types: –Built-in Datatypes –ANSI Supported Datatypes –Oracle Supplied Datatypes –User-Defined Types –External Datatypes

9 Example of Oracle Data Types: –CHAR(size) –VARCHAR2(size) –NUMBER(n,d) –DATE –LONG –ROW(size) –CLOB BLOB BFILE –XML –XML index

10 Built-in Data Types: 1- Character : Format: CHAR [ (size [ BYTE/CHAR ] )] VARCHAR2 (maxsize [ BYTE/CHAR ] ) ‘4321’ ‘19 Main St. Frostburg’ ‘ * ’ ‘Student’’s ID’

11 Built-in Data Types: 2-Number: Format:NUMBER [ (Precision] [, Scale] )] NUMBER NUMBER(P,S) 3-Date (&Time): Format:DATE MyBirthdate = ‘11-JAN-37’ TIMESTAMP

12 Built-in Data Types: 4-Large Objects: Format:BLOB CLOB BFILE CREATE TABLE add_cosc_F08 (titleVARCHAR2(80), compositeBLOB, textCLOB, graphBFILE);

13 Built-in Data Types: 5-ROWID: Format:ROWID

14 ANSI Supported Data Types: Examples: CHAR CHARACTER VARCHAR NUMERIC DECIMAL INTEGER INT FLOAT REAL ……

15 Oracle Supplied Data Types: Any Type: SYS.ANYDATA SYS.ANYTYPE SYS.ANYDATASET XML Type: XMLType URLType Media Type: ORDAudio ORDImage ORDDoc

16 DESCRIBEStudent; NameNull?Type NAMEVARCHAR2(80) IDNUMBER(9) GPA…… Display a Structure of a Table:

17 Tables User Tables: 1. USER_ 2. ALL_ 3. DBA_ 4. V$_  Data Dictionary 1.USER_TABLES 2.USER_OBJECTS 3.USER_CATalog

18 Integrity Constraints Not Null Unique Primary Key Foreign Key Check -Why Integrity Constraints?

19 Integrity Constraints Inline (Col_Constraint) Out_of_line(Table_Constraint) NOT NULL

20 Integrity Constraints 1.NOT NULL: CREATE TABLE student (NameVARCHAR2(80)NOT NULL, IDNUMBER(9)NOT NULL, GPANUMBER(3,2), B_DateDATE, MajorCHAR(4));

21 Integrity Constraints Constraints on Tables:Why? NOT NULL: CREATE TABLE student (NameVARCHAR2(80)NOT NULL, IDNUMBER(9)NOT NULL, GPANUMBER(3,2), B_DateDATE, MajorCHAR(4));

22 Integrity Constraints UNIQUE: CREATE TABLE student (NameVARCHAR2(80) UNIQUE NOT NULL, IDNUMBER(9) UNIQUE, GPANUMBER(3,2), B_DateDATE, MajorCHAR(4));

23 Integrity Constraints UNIQUE: CREATE TABLE student (NameVARCHAR2(80), IDNUMBER(9), GPANUMBER(3,2), B_DateDATE, MajorCHAR(4), UNIQUE (Name,ID) );

24 PRIMARY KEY: CREATE TABLE student (NameVARCHAR2(80), IDNUMBER(9) PRIMARY KEY, GPANUMBER(3,2), B_DateDATE, MajorCHAR(4) ); Integrity Constraints

25 Integrity Constraints PRIMARY KEY: CREATE TABLE student (NameVARCHAR2(80), IDNUMBER(9), GPANUMBER(3,2), B_DateDATE, MajorCHAR(4), PRIMARY KEY(ID) );

26 Integrity Constraints FOREIGN KEY: CREATE TABLE Course (C_NumNUMBER(4,0) NOT NULL, DeptVARCHAR2(20) REFERENCES Department(name), TitleVARCHAR2(40), CreditNUMBER(1), CONSTRAINT dep_PK PRIMARY KEY (C_Num, Dept) );

27 Integrity Constraints CREATE TABLE Course (C_NumNUMBER(4,0) NOT NULL, DeptVARCHAR2(20) TitleVARCHAR2(40), CreditNUMBER(1), CONSTRAINT dep_PK PRIMARY KEY (C_Num, Dept) CONSTRAINT dep_FK FOREIGN KEY (dept) REFERENCES Department(name) );

28 Integrity Constraints CREATE TABLE Course (C_NumNUMBER(4,0) NOT NULL, DeptVARCHAR2(20) REFERENCES Department(name) ON DELETE CASCADE, --ON DELETE SET NULL TitleVARCHAR2(40), CreditNUMBER(1), PRIMARY KEY (C_Num, Dept) );

29 Range Constraint: CREATE TABLE student (NameVARCHER2(80), IDNUMBER(9) CHECK (ID BETWEEN AND ), GPANUMBER(3,2) CHECK (GPA BETWEEN 0.00 AND 4.00), B_DateDATE, MajorCHAR(4) ); --CHECK (grade IN(‘A’, ‘B’, ‘C’, ‘D’, ‘F’)) --Constraint grade_CK CHECK (grade IN (‘A’,’B’,’C’,’D’,’F’)) Integrity Constraints

30 Integrity Constraints CREATE TABLE student (NameVARCHAR2(80) CONSTRAINT student_nnNOT NULL, IDNUMBER(9) NOT NULL, GPANUMBER(3,2), B_DateDATE, MajorCHAR(4), CONSTRAINT student_UQ UNIQUE (Name,ID) );

31 CREATE TABLE student (NameVARCHAR2(80), IDNUMBER(9), GPANUMBER(3,2) CHECK (GPA BETWEEN0.00 AND 4.00), B_DateDATE, MajorCHAR(4), CONSTRAINT student_ID_CK CHECK(ID BETWEEN AND ) ); Integrity Constraints

32 Constraints SELECT Constraint_Name, Constraint_Type, Table_Name FROM user_constraints; Constraint_NameConstraint_TypeTable_Name SYS_COU2111P student student_UQU student student_ID_CKC student dep_FKR student

33 Constraints States: DISABLE ENABLE Integrity Constraints

34 Integrity Constraints CREATE TABLE student (NameVARCHAR2(80) CONSTRAINT student_nnNOT NULL DISABLE, IDNUMBER(9) NOT NULL DISABLE, GPANUMBER(3,2), B_DateDATE, MajorCHAR(4), CONSTRAINT student_UQ UNIQUE (Name,ID) DISABLE );

35 Default CREATE TABLE Course (C_NumNUMBER(4,0) Default ‘XXXX’ NOT NULL, DeptVARCHAR2(20) REFERENCES Department(name) ON DELETE CASCADE, TitleVARCHAR2(40), CreditNUMBER(1).DEFAULT 3, PRIMARY KEY (C_Num, Dept));

36 Default CREATE TABLE Temp ( inserts VARCHAR2(80) DEFAULT USER, arrived DATE DEFAULT SYSDATE, loggedInAs NUMBER(4) DEFAULT UID );

37 Create table from a table CREATE TABLE tempstudent AS SELECT Name,ID,GPA, B_Date FROM student;

38 Delete a Table: DROP TABLE Student; DROP TABLE Student CASCADE CONSTRAINTS; DROP TABLE Student PURGE; TRUNCATE TABLE Student; PURGE TABLE Student;

39 –Privilege is required –DESC student; –DROP TABLE student CASCADE CONSTRAINTS; –SELECT * FROM RECYCLEBIN; -- USER_RECYCLEBIN Flash_back

40 Object_Name Original_Name Operation Type TS_Name RB$$48448$TABLE$0 STUDENTDROP TABLE USERS Creation Droptime :14:11: :12:30:31 Flash_back

41 Restoring Tables from Recycle Bin: FLASHBACK TABLE Student TO BEFORE DROP RENAME TO Student2;

42 Changing an existing Table Structure 1.Adding Columns: ALTER TABLE student ADD (AddressVARCHAR2(50), PhoneCHAR(10) ); 2.Modify Table Condition: ALTER TABLE student MODIFY (IDNUMBER(10,0), NameVARCHAR2(200) ); (continued)

43 Changing an Existing Table Structure 3. Deleting Columns: ALTER TABLE student DROP COLUMN Address; ALTER TABLE student DROP COLUMN (Major, Minor); 4. Modify Table Condition: ALTER TABLE student SET UNUSED COLUMN Address;

44 Renaming Table Columns 5. ALTER TABLE student RENAME COLUMN ID TO NewID;

45 Rules for Adding and Modifying: You can add columns with no NOT NULL You can increase the CHAR width You can increase the number of Digits You can increase/decrease the number of decimals You can convert CHAR to VARCHAR2 You can change data type if the column contains no values

46 Changing the name of a table: RENAME student TO GradStudent;

47 Adding Comments to a Table: COMMENT ON TABLE student IS ‘staff information’; COMMENT ON COLUMN student.column IS ‘text’;

48 Viewing information about a Table: USER_TABLES USER_TAB_COLUMNS USER_ALL_TABLES USER_TAB_COMMENTS USER_COL_COMMENTS USER_TAB_STATISTICS USER_TAB_MODIFICATIONS ALL_TABLES DBA_TABLES

49 Querying Data SELECT * FROM USER_TABLES; SELECTDISTINCT object_type FROMuser_object; SELECT* FROMcat;

50 Add a constraint to an existing table: ALTER TABLE student ADD CONSTRAINT student_pk PRIMARY KEY (ID);

51 Drop a Constraint: ALTER TABLE student DROP CONSTRAINT student_pk ; (if you did not name your constraint) ALTER TABLE student DROP PRIMARY KEY; ALTER TABLE student DROP CONSTRAINT student_pk CASCADE;

52 Constraints: Disable a constraint: ALTER TABLE student DISABLE CONSTRAINT student_pk ; Enable a constraint: ALTER TABLE student ENABLE CONSTRAINT student_pk;

53 Constraints ALTER TABLE student ENABLE PRIMARY KEY ENABLE UNIQUE (Name) ENABLE UNIQUE (Phone) ENABLE Student_ID_CK;

54 Constraints: You may enable or disable your Constraint: CREATE TABLE student (NameVARCHER2(80), DISABLE CONSTRAINT student_nn NOT NULL, IDNUMBER(9) NOT NULL, GPANUMBER(3,2), B_DateDATE, MajorCHAR(4), CONSTRAINT student_uk UNIQUE (Name,ID) );

55 Constraints: NOTE: Automatic system constraint naming: System ID SYS_C###### Viewing constraints: SELECT constraint_name,constraint_type FROM user_constraints; Viewing constraints: SELECT constraint_name,constraint_type FROM user_constraints WHEREtable_name= ‘ STUDENT ’ ;

56 Constraints: Viewing column constraints: SELECT constraint_name, column_name, constraint_type FROM user_cons_columns; Viewing column constraints: SELECT constraint_name, column_name, FROM user_cons_columns WHEREtable_name= ‘ STUDENT ’ ;

57 Constraints: Example: SELECT * FROM USER_COL_COMMENTS; DROP COMMENTS: COMMENT ON COLUMN student.id IS ‘ ’ ;

58 Encrypted Columns CREATE TABLE student (NameVARCHAR2(80) PRIMARY KEY, IDNUMBER(9) ENCRYPT, GPANUMBER(3,2), Reg_Date DATE DEFAULT (SYSDATE), MajorCHAR(4)) TABLE SPACE mytables STORAGE (INITIAL 50k);

59 Temporary Tables –You can create tables that solely exist for your session or whose data persists for the duration of your transactions. –Use CREATE GLOBAL TEMPORARY TABLE; you can specify whether it should last for the duration of your session (via ON COMMIT PRESERVE ROWS) (Session specific) or should be deleted when transaction is complete (via ON COMMIT DELETE ROWS) (Transaction specific)

60 CREATE GLOBAL TEMPORARY TABLE myTable ( col 1 Type 1 col 2 Type 2 col 3 Type 3 ) ON COMMIT PRESERVE ROWS; Temporary Tables

61 Case Study One –A web-based student registration application allows a student to create several options course schedule for a given semester. Each option will be a row in a Temporary Table. When a student decides which option s/he wants to select, the application moves the row from the Temporary Table to a permanent table. –During the session, the course data are private. At the end, data are dropped.

62 CREATE GLOBAL TEMPORARY TABLE student_work_area ( Course1CHAR(11) --COSC Course2CHAR(11) Course3CHAR(11) Course4CHAR(11) Course5CHAR(11) Day1CHAR(1) --T Day2CHAR(1) Day3CHAR(1) Day4CHAR(1) Day5CHAR(1) Start_Time1NUMBER(5,2) End_Time1NUMBER(5,2) Start_Time2NUMBER(5,2) End_Time2NUMBER(5,2) Start_Time3NUMBER(5,2) End_Time3NUMBER(5,2) Start_Time4NUMBER(5,2) End_Time4NUMBER(5,2) Start_Time5NUMBER(5,2) End_Time5NUMBER(5,2)) ON COMMIT DELETE ROWS;

63 PRACTICE This code compile: DECLARE Sysdate NUMBER; BEGIN Sysdate:=2; END; This code dose not compile: DECLARE then NUMBER; BEGIN then:=2; END; WHY?