chapter four objectives introduction to sql types of sql statements concepts of ddl & dml...

40
Chapter Four 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)

Upload: constance-kerrie-newton

Post on 28-Dec-2015

244 views

Category:

Documents


1 download

TRANSCRIPT

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

Chapter Four

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 Four Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)

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 Four Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)

3

Data Definition Language (DDL) Data Manipulation Language (DML)

Types of SQL Statements

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

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

Faculty_Status(Name, Num_Faculty)(Rank, Low_salary, High_salary)

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

5

Data Definition Language (DDL) Format:

CREATE TABLE Table_Name(Attribute Attribute_Type,

Attribute Attribute_Type);

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

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 Four Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)

7

Data Definition Language

Name:User Identifiers:

1-30 charactersStart with an alphabet

Followed by alphabet, digit, _

UniqueNot reservedNot case sensitive

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

8

Data Types

Oracle Data Types:

CHAR(size) VARCHAR2(max_size) NUMBER(n,d) DATE

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

9

Data Types:

1-Character:CHAR(Size)VARCHAR2(MaxSize)

‘4321’‘19 Main St. Frostburg’‘ * ’‘Student’’s ID’

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

10

Data Types:

2-Number:NUMBERNUMBER(T,D)

1,234,567.89 NUMBER 1,234,567.891,234,567.89 NUMBER(9) 1,234,5671,234,567.89 NUMBER(9,1) 1,234,567.81,234,567.89 NUMBER(7,-2) 1,234,5001,234,567.89 NUMBER(6) ??

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

11

Data Types: 3-Date:

DATEMyBirthdate = ‘11-JAN-37’

Default time 00:00:00 A.M.

Use TO_DATE()

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

12

DESCRIBE Student;

Name Null? Type----------------------------------------------------------NAME VARCHAR2(80)ID NUMBER(9)GPA NUMBER(3,2)B_Date DATEMajor CHAR(4)

Display a Structure of a Table:

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

13

Practice:Create a table using your own data types for Customer with the attributes:

-Customer number-Customer last name -Customer first name-Customer Street -Customer balance-Customer City -Customer credit limit-Customer State -Customer zip code-Sales Rep Number

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

14

Constraints Constraints on Tables: Why?

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 15: Chapter Four Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)

15

2. PRIMARY KEY:

CREATE TABLE student(Name VARCHAR2(80),ID NUMBER(9) PRIMARY

KEY,

GPA NUMBER(3,2),B_Date DATE,Major CHAR(4));

Constraints

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

16

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 17: Chapter Four Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)

17

Constraints

3. FOREIGN KEY:CREATE TABLE Course

(C_Num NUMBER(4,0) NOT NULL,Dept VARCHAR2(20)

REFERENCES Department(name),Title VARCHAR2(40),Credit NUMBER(1),PRIMARY KEY (C_Num, Dept) );

//CONSTRAINT dep_FK FOREIGN KEY (dept)REFERENCES Department(name),

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

18

Constraints

FOREIGN KEY:

FOREIGN KEYREFERENCES ON DELETE CASCADE

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

19

4 -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));

Constraints

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

20

Constraints 5- Unique Constraint:CREATE 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_uk UNIQUE

(Name,ID));

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

21

Naming Constraint:

Example: CREATE TABLE student(Name VARCHAR2(80),ID NUMBER(9),GPA NUMBER(3,2),B_Date DATE,Major CHAR(4),CONSTRAINT Student_PK

PRIMARY KEY(ID));

Constraints

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

22

Range Constraint: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_rg CHECK(ID BETWEEN 000000000 AND 999999999)

);

Constraints

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

23

Practice:Add constraint to your table Customer.

-Customer number: Primary key-Customer last name -Customer first name-Customer Street -Customer balance-Customer City -Customer credit limit-Customer State -Customer zip code-Sales Rep Number: Foreign key to Sales Representative Table

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

24

Practice:Create a table for OrderForm with the attributes:

-Order Number: Primary Key-Order Date-Order part Number-Order part Description-Order quoted price

We need to have part number, quoted price and order number for every recordPart number is from 1000 to 9999

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

25

Practice:Create a table for Sales Representative with the attributes:

-Sales Rep Number: Primary Key-Sales Rep Name-Sales Rep Address-Sales Rep Phone Number

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

26

Delete a Table: DROP TABLE Student;

TRUNCATE TABLE Student;

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

27

Changing an existing Table Structure

1. 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 28: Chapter Four Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)

28

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 You cannot drop a column from a table

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

29

Default Value for New Attributes

1. Adding Columns:ALTER TABLE student ADD

(Address VARCHAR2(50),B_Date Date SYSDATE);

2. Modify Table Condition:ALTER TABLE student MODIFY(ID NUMBER(10,0) ,Name VARCHAR2(200)); (continued)

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

30

Changing the name of a table:

RENAME student TO GradStudent;

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

31

Adding Comments to a Table:

COMMENT ON TABLE student IS ‘staff information’;

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

32

Add a constraint to an existing table:

ALTER TABLE student ADD CONSTRAINT student_pk PRIMARY KEY

(ID);

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

33

Practice:

Add two NOT NULL constraints called custom_zip_nn and custom_state_nn to your zip code and state columns in customer table.

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

34

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 35: Chapter Four Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)

35

Constraints: Disable a constraint:

ALTER TABLE student DISABLE CONSTRAINT student_pk ;  

Enable a constraint:ALTER TABLE student ENABLE CONSTRAINT student_pk;

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

36

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 37: Chapter Four Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)

37

Constraints: NOTE: Automatic system constraint

naming:System ID SYS_C###### Viewing constraints:

SELECT constraint_name,constraint_type, search_condition

FROM user_constraints;

Viewing constraints:SELECT constraint_name,constraint_type,

search_condition

FROM user_constraintsWHERE table_name=‘student’;

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

38

Constraints:

Viewing column constraints:SELECT constraint_name, column_name

FROM user_cons_columns;

Viewing column constraints:SELECT constraint_name, column_name

FROM user_cons_columns

WHERE table_name=‘student’;;

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

39

Constraints: Example:

SELECT * FROM USER_COL_COMMENTS;

DROP COMMENTS:COMMENT ON COLUMN student.id IS ‘

’;

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

40

Practice:

Add one column to customer table called Birth_DateAdd comment to customer table “ Your name as the last person worked on this table”Add comment on customer table for customer number to indicate “ custom number is the last four digits of SS number plus the fist character of the customer name “Check to see the comments on table customerRename customer table to customer2 tableChange the data type of customer name ( last, first)