Chapter Four Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating...

Post on 28-Dec-2015

233 views 1 download

Transcript of Chapter Four Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating...

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)

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)

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

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

5

Data Definition Language (DDL) Format:

CREATE TABLE Table_Name(Attribute Attribute_Type,

Attribute Attribute_Type);

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

7

Data Definition Language

Name:User Identifiers:

1-30 charactersStart with an alphabet

Followed by alphabet, digit, _

UniqueNot reservedNot case sensitive

8

Data Types

Oracle Data Types:

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

9

Data Types:

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

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

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) ??

11

Data Types: 3-Date:

DATEMyBirthdate = ‘11-JAN-37’

Default time 00:00:00 A.M.

Use TO_DATE()

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:

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

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

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

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

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),

18

Constraints

FOREIGN KEY:

FOREIGN KEYREFERENCES ON DELETE CASCADE

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

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

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

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

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

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

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

26

Delete a Table: DROP TABLE Student;

TRUNCATE TABLE Student;

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)

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

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)

30

Changing the name of a table:

RENAME student TO GradStudent;

31

Adding Comments to a Table:

COMMENT ON TABLE student IS ‘staff information’;

32

Add a constraint to an existing table:

ALTER TABLE student ADD CONSTRAINT student_pk PRIMARY KEY

(ID);

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.

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;

35

Constraints: Disable a constraint:

ALTER TABLE student DISABLE CONSTRAINT student_pk ;  

Enable a constraint:ALTER TABLE student ENABLE CONSTRAINT student_pk;

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

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’;

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’;;

39

Constraints: Example:

SELECT * FROM USER_COL_COMMENTS;

DROP COMMENTS:COMMENT ON COLUMN student.id IS ‘

’;

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)