chapter six objectives introduction to sql types of sql statements concepts of ddl & dml...
TRANSCRIPT
![Page 1: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/1.jpg)
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)
![Page 2: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/2.jpg)
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.
![Page 3: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/3.jpg)
3
• Data Definition Language (DDL)
• Data Manipulation Language (DML)
• Transaction Control
• Session Control
• System Control
• Embedded SQL
Types of SQL Statements
![Page 4: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/4.jpg)
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)
![Page 5: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/5.jpg)
5
Data Definition Language (DDL)
• Format:CREATE TABLE [schima.]Table_Name(Attribute Attribute_Type [DEFAULT expr]
[Col_constraint],….Attribute Attribute_Type[Table_constraint]);
![Page 6: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/6.jpg)
6
Data Definition Language (DDL)
• Example:
SQL> CREATE TABLE student2 (Name
VARCHAR2(80),3 ID NUMBER(9),4 GPA
NUMBER(3,2),5 B_Date DATE,6 Major CHAR(4)7 );
![Page 7: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/7.jpg)
7
Data Definition Language
Name:User Identifiers:
1-30 charactersStart with an alphabet
Followed by alphabet, digit, _
UniqueNot reservedNot case sensitive
![Page 8: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/8.jpg)
8
Oracle Data Types:
– Built-in Datatypes– ANSI Supported Datatypes– Oracle Supplied Datatypes– User-Defined Types– External Datatypes
![Page 9: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/9.jpg)
9
Example of Oracle Data Types:
– CHAR(size)– VARCHAR2(size)– NUMBER(n,d)– DATE– LONG– ROW(size)– CLOB BLOB BFILE– XML– XML index
![Page 10: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/10.jpg)
10
Built-in Data Types:1-Character:
Format: CHAR [ (size [ BYTE/CHAR ] )]VARCHAR2 (maxsize [ BYTE/CHAR ] )
‘4321’‘19 Main St. Frostburg’‘ * ’‘Student’’s ID’
![Page 11: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/11.jpg)
11
Built-in Data Types:2-Number:
Format: NUMBER [ (Precision] [, Scale] )]
NUMBERNUMBER(P,S)
3-Date (&Time):Format: DATE
MyBirthdate = ‘11-JAN-37’TIMESTAMP
![Page 12: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/12.jpg)
12
Built-in Data Types:4-Large Objects:
Format: BLOBCLOBBFILE
CREATE TABLE add_cosc_F08
(title VARCHAR2(80),
composite BLOB,
text CLOB,
graph BFILE);
![Page 13: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/13.jpg)
13
Built-in Data Types:
5-ROWID:Format: ROWID
![Page 14: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/14.jpg)
14
ANSI Supported Data Types:
Examples:CHAR
CHARACTER
VARCHAR
NUMERIC
DECIMAL
INTEGER
INT
FLOAT
REAL
……
![Page 15: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/15.jpg)
15
Oracle Supplied Data Types:Any Type:
SYS.ANYDATA
SYS.ANYTYPE
SYS.ANYDATASET
XML Type:XMLType
URLType
Media Type:ORDAudio
ORDImage
ORDDoc
![Page 16: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/16.jpg)
16
DESCRIBE Student;
Name Null? Type----------------------------------------------------------NAME VARCHAR2(80)ID NUMBER(9)GPA ……
Display a Structure of a Table:
![Page 17: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/17.jpg)
17
Tables
• User Tables:1. USER_
2. ALL_
3. DBA_
4. V$_
Data Dictionary
1. USER_TABLES
2. USER_OBJECTS
3. USER_CATalog
![Page 18: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/18.jpg)
18
Integrity Constraints
Not Null
Unique
Primary Key
Foreign Key
Check
-Why Integrity Constraints?
![Page 19: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/19.jpg)
19
Integrity Constraints
• Inline (Col_Constraint)
• Out_of_line(Table_Constraint)
NOT NULL
![Page 20: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/20.jpg)
20
Integrity Constraints
1. NOT NULL:
CREATE TABLE student(Name VARCHAR2(80) NOT
NULL,ID NUMBER(9) NOT
NULL,GPA NUMBER(3,2),B_Date DATE,Major CHAR(4));
![Page 21: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/21.jpg)
21
Integrity Constraints• Constraints on Tables: Why?
NOT NULL:
CREATE TABLE student(Name VARCHAR2(80) NOT
NULL,ID NUMBER(9) NOT
NULL,GPA NUMBER(3,2),B_Date DATE,Major CHAR(4));
![Page 22: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/22.jpg)
22
Integrity Constraints
UNIQUE:
CREATE TABLE student(Name VARCHAR2(80) UNIQUE NOT NULL,ID NUMBER(9) UNIQUE,GPA NUMBER(3,2),B_Date DATE,Major CHAR(4));
![Page 23: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/23.jpg)
23
Integrity Constraints
UNIQUE:
CREATE TABLE student(Name VARCHAR2(80),ID NUMBER(9),GPA NUMBER(3,2),B_Date DATE,Major CHAR(4), UNIQUE (Name,ID) );
![Page 24: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/24.jpg)
24
PRIMARY KEY:
CREATE TABLE student
(Name VARCHAR2(80),
ID NUMBER(9) PRIMARY KEY,
GPA NUMBER(3,2),
B_Date DATE,
Major CHAR(4)
);
Integrity Constraints
![Page 25: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/25.jpg)
25
Integrity Constraints
PRIMARY KEY:
CREATE TABLE student(Name VARCHAR2(80),ID NUMBER(9),GPA NUMBER(3,2),B_Date DATE,Major CHAR(4),PRIMARY KEY(ID));
![Page 26: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/26.jpg)
26
Integrity Constraints
FOREIGN KEY:CREATE TABLE Course
(C_Num NUMBER(4,0) NOT NULL,Dept VARCHAR2(20)
REFERENCES Department(name),Title VARCHAR2(40),Credit NUMBER(1),
CONSTRAINT dep_PK PRIMARY KEY (C_Num, Dept)
);
![Page 27: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/27.jpg)
27
Integrity Constraints
CREATE TABLE Course(C_Num NUMBER(4,0) NOT NULL,Dept VARCHAR2(20)Title VARCHAR2(40),Credit NUMBER(1),
CONSTRAINT dep_PK PRIMARY KEY (C_Num, Dept) CONSTRAINT dep_FK FOREIGN KEY (dept)
REFERENCES Department(name) );
![Page 28: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/28.jpg)
28
Integrity Constraints
CREATE TABLE Course(C_Num NUMBER(4,0) NOT NULL,Dept VARCHAR2(20)
REFERENCES Department(name) ON DELETE CASCADE,
-- ON DELETE SET NULLTitle VARCHAR2(40),Credit NUMBER(1),PRIMARY KEY (C_Num, Dept) );
![Page 29: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/29.jpg)
29
• Range Constraint:CREATE TABLE student
(Name VARCHER2(80),ID NUMBER(9) CHECK(ID BETWEEN 000000000 AND 999999999),GPA NUMBER(3,2)CHECK (GPA BETWEEN 0.00 AND 4.00),B_Date DATE,Major CHAR(4));
--CHECK (grade IN(‘A’, ‘B’, ‘C’, ‘D’, ‘F’))--Constraint grade_CK CHECK (grade IN (‘A’,’B’,’C’,’D’,’F’))
Integrity Constraints
![Page 30: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/30.jpg)
30
Integrity ConstraintsCREATE TABLE student
(Name VARCHAR2(80)CONSTRAINT student_nn NOT NULL,ID NUMBER(9) NOT NULL,GPA NUMBER(3,2),B_Date DATE,Major CHAR(4),CONSTRAINT student_UQ UNIQUE
(Name,ID));
![Page 31: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/31.jpg)
31
CREATE TABLE student(Name VARCHAR2(80),ID NUMBER(9),GPA NUMBER(3,2)CHECK (GPA BETWEEN 0.00 AND 4.00),B_Date DATE,Major CHAR(4),
CONSTRAINT student_ID_CK CHECK(ID BETWEEN 000000000 AND 999999999)
);
Integrity Constraints
![Page 32: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/32.jpg)
32
ConstraintsSELECT Constraint_Name,
Constraint_Type, Table_NameFROM user_constraints;
Constraint_NameConstraint_Type Table_Name-----------------------------------------------------------------------SYS_COU2111 P studentstudent_UQ U studentstudent_ID_CK C studentdep_FK R student
![Page 33: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/33.jpg)
33
Constraints States:
• DISABLE
• ENABLE
Integrity Constraints
![Page 34: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/34.jpg)
34
Integrity ConstraintsCREATE TABLE student
(Name VARCHAR2(80)CONSTRAINT student_nn NOT NULL DISABLE,ID NUMBER(9) NOT NULL DISABLE,GPA NUMBER(3,2),B_Date DATE,Major CHAR(4),CONSTRAINT student_UQ UNIQUE (Name,ID)
DISABLE);
![Page 35: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/35.jpg)
35
DefaultCREATE TABLE Course
(C_Num NUMBER(4,0) Default ‘XXXX’ NOT NULL,
Dept VARCHAR2(20)REFERENCES Department(name)
ON DELETE CASCADE,Title VARCHAR2(40),Credit NUMBER(1).DEFAULT 3,PRIMARY KEY (C_Num, Dept));
![Page 36: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/36.jpg)
36
Default
CREATE TABLE Temp(
inserts VARCHAR2(80) DEFAULT USER,arrived DATE DEFAULT
SYSDATE,loggedInAs NUMBER(4) DEFAULT UID
);
![Page 37: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/37.jpg)
37
Create table from a table
CREATE TABLE tempstudent
AS
SELECT Name ,ID ,GPA, B_DateFROM student;
![Page 38: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/38.jpg)
38
Delete a Table:
• DROP TABLE Student;• DROP TABLE Student CASCADE CONSTRAINTS;• DROP TABLE Student PURGE;
• TRUNCATE TABLE Student;
• PURGE TABLE Student;
![Page 39: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/39.jpg)
39
– Privilege is required
– DESC student;– DROP TABLE student CASCADE CONSTRAINTS;
– SELECT *FROM RECYCLEBIN;
-- USER_RECYCLEBIN
Flash_back
![Page 40: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/40.jpg)
40
Object_Name Original_Name Operation Type TS_Name
--------------------------------------------------------------------------------------------------
RB$$48448$TABLE$0 STUDENT DROP TABLE USERS
Creation Droptime
-----------------------------------------------------------
2005-01-23:14:11:50 2005-05-01:12:30:31
Flash_back
![Page 41: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/41.jpg)
41
Restoring Tables from Recycle Bin:
FLASHBACK TABLE Student
TO BEFORE DROP
RENAME TO Student2;
![Page 42: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/42.jpg)
42
Changing an existing Table Structure1. Adding Columns:
ALTER TABLE student ADD(Address VARCHAR2(50),Phone CHAR(10));
2. Modify Table Condition:ALTER TABLE student MODIFY(ID NUMBER(10,0),Name VARCHAR2(200)); (continued)
![Page 43: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/43.jpg)
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 ;
![Page 44: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/44.jpg)
44
Renaming Table Columns
5. ALTER TABLE student
RENAME COLUMN ID TO NewID;
![Page 45: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/45.jpg)
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
![Page 46: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/46.jpg)
46
Changing the name of a table:
RENAME student TO GradStudent;
![Page 47: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/47.jpg)
47
Adding Comments to a Table:
COMMENT ON TABLE student IS ‘staff information’;
COMMENT ON COLUMN student.column IS ‘text’;
![Page 48: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/48.jpg)
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
![Page 49: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/49.jpg)
49
Querying Data
SELECT * FROM USER_TABLES;SELECT DISTINCT object_type FROM user_object;SELECT * FROM cat;
![Page 50: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/50.jpg)
50
Add a constraint to an existing table:
• ALTER TABLE student
ADD CONSTRAINT
student_pk PRIMARY KEY (ID);
![Page 51: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/51.jpg)
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;
![Page 52: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/52.jpg)
52
Constraints:
• Disable a constraint:ALTER TABLE student
DISABLE CONSTRAINT student_pk ;
• Enable a constraint:ALTER TABLE student
ENABLE CONSTRAINT student_pk;
![Page 53: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/53.jpg)
53
Constraints
ALTER TABLE student
ENABLE PRIMARY KEY
ENABLE UNIQUE (Name)
ENABLE UNIQUE (Phone)
ENABLE Student_ID_CK;
![Page 54: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/54.jpg)
54
Constraints:• You may enable or disable your Constraint:CREATE TABLE student
(Name VARCHER2(80),
DISABLE CONSTRAINT student_nnNOT NULL,
ID NUMBER(9) NOT NULL,
GPA NUMBER(3,2),B_Date DATE,Major CHAR(4),CONSTRAINT student_uk UNIQUE (Name,ID));
![Page 55: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/55.jpg)
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_constraintsWHERE table_name=‘STUDENT’;
![Page 56: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/56.jpg)
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
WHERE table_name=‘STUDENT’;
![Page 57: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/57.jpg)
57
Constraints:
• Example:SELECT *
FROM USER_COL_COMMENTS;
• DROP COMMENTS:
COMMENT ON COLUMN student.id IS ‘ ’;
![Page 58: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/58.jpg)
58
Encrypted Columns
CREATE TABLE student(Name VARCHAR2(80) PRIMARY KEY,ID NUMBER(9) ENCRYPT,GPA NUMBER(3,2),Reg_Date DATE DEFAULT (SYSDATE),Major CHAR(4))TABLE SPACE mytablesSTORAGE (INITIAL 50k);
![Page 59: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/59.jpg)
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)
![Page 60: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/60.jpg)
60
CREATE GLOBAL TEMPORARY TABLE myTable(
col1 Type1
col2 Type2
col3 Type3
)ON COMMIT PRESERVE ROWS;
Temporary Tables
![Page 61: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/61.jpg)
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.
![Page 62: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/62.jpg)
62
CREATE GLOBAL TEMPORARY TABLE student_work_area (Course1 CHAR(11) --COSC100.001Course2 CHAR(11)Course3 CHAR(11)Course4 CHAR(11)Course5 CHAR(11)Day1 CHAR(1) --TDay2 CHAR(1)Day3 CHAR(1)Day4 CHAR(1)Day5 CHAR(1)Start_Time1 NUMBER(5,2)End_Time1 NUMBER(5,2)Start_Time2 NUMBER(5,2)End_Time2 NUMBER(5,2)Start_Time3 NUMBER(5,2)End_Time3 NUMBER(5,2)Start_Time4 NUMBER(5,2)End_Time4 NUMBER(5,2)Start_Time5 NUMBER(5,2)End_Time5 NUMBER(5,2))
ON COMMIT DELETE ROWS;
![Page 63: Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)](https://reader036.vdocuments.net/reader036/viewer/2022070402/56649f265503460f94c3d885/html5/thumbnails/63.jpg)
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?