Download presentation
Presentation is loading. Please wait.
Published byConstance Kerrie Newton Modified over 9 years ago
1
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
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
3 Data Definition Language (DDL) Data Manipulation Language (DML) Types of SQL Statements
4
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
5 Data Definition Language (DDL) Format: CREATE TABLETable_Name (AttributeAttribute_Type, AttributeAttribute_Type);
6
6 Data Definition Language (DDL) Example: SQL> CREATE TABLE student 2(NameVARCHAR2(80), 3IDNUMBER(9), 4GPANUMBER(3,2), 5B_DateDATE, 6MajorCHAR(4) 7);
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
8 Data Types Oracle Data Types: CHAR(size) VARCHAR2(max_size) NUMBER(n,d) DATE
9
9 Data Types: 1-Character: CHAR(Size) VARCHAR2(MaxSize) ‘4321’ ‘19 Main St. Frostburg’ ‘ * ’ ‘Student’’s ID’
10
10 Data Types: 2-Number: NUMBER NUMBER(T,D) 1,234,567.89NUMBER 1,234,567.89 1,234,567.89NUMBER(9) 1,234,567 1,234,567.89NUMBER(9,1) 1,234,567.8 1,234,567.89 NUMBER(7,-2) 1,234,500 1,234,567.89 NUMBER(6) ??
11
11 Data Types: 3-Date: DATE MyBirthdate = ‘11-JAN-37’ Default time 00:00:00 A.M. Use TO_DATE()
12
12 DESCRIBEStudent; NameNull?Type ---------------------------------------------------------- NAMEVARCHAR2(80) IDNUMBER(9) GPA NUMBER(3,2) B_DateDATE MajorCHAR(4) Display a Structure of a Table:
13
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
14 Constraints Constraints on Tables:Why? 1.NOT NULL: CREATE TABLE student (NameVARCHAR2(80)NOT NULL, IDNUMBER(9)NOT NULL, GPANUMBER(3,2), B_DateDATE, MajorCHAR(4));
15
15 2. PRIMARY KEY: CREATE TABLE student (NameVARCHAR2(80), IDNUMBER(9) PRIMARY KEY, GPANUMBER(3,2), B_DateDATE, MajorCHAR(4) ); Constraints
16
16 Constraints PRIMARY KEY: CREATE TABLE student (NameVARCHAR2(80), IDNUMBER(9), GPANUMBER(3,2), B_DateDATE, MajorCHAR(4), PRIMARY KEY(ID) );
17
17 Constraints 3.FOREIGN KEY: CREATE TABLE Course (C_NumNUMBER(4,0) NOT NULL, DeptVARCHAR2(20) REFERENCES Department(name), TitleVARCHAR2(40), CreditNUMBER(1), PRIMARY KEY (C_Num, Dept) ); //CONSTRAINT dep_FK FOREIGN KEY (dept) REFERENCES Department(name),
18
18 Constraints FOREIGN KEY: FOREIGN KEY REFERENCES ON DELETE CASCADE
19
19 4 -Range Constraint: CREATE TABLE student (NameVARCHER2(80), IDNUMBER(9) CHECK (ID BETWEEN 000000000 AND 999999999), GPANUMBER(3,2) CHECK (GPA BETWEEN0.00 AND 4.00), B_DateDATE, MajorCHAR(4) ); Constraints
20
20 Constraints 5- Unique Constraint: CREATE TABLE student (NameVARCHAR2(80) CONSTRAINT student_nnNOT NULL, IDNUMBER(9) NOT NULL, GPANUMBER(3,2), B_DateDATE, MajorCHAR(4), CONSTRAINT student_uk UNIQUE (Name,ID) );
21
21 Naming Constraint: Example: CREATE TABLE student (NameVARCHAR2(80), IDNUMBER(9), GPANUMBER(3,2), B_DateDATE, MajorCHAR(4), CONSTRAINT Student_PK PRIMARY KEY(ID)); Constraints
22
22 Range Constraint: CREATE TABLE student (NameVARCHAR2(80), IDNUMBER(9), GPANUMBER(3,2) CHECK (GPA BETWEEN0.00 AND 4.00), B_DateDATE, MajorCHAR(4), CONSTRAINT student_rg CHECK(ID BETWEEN 000000000 AND 999999999) ); Constraints
23
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
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 record Part number is from 1000 to 9999
25
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
26 Delete a Table: DROP TABLE Student; TRUNCATE TABLE Student;
27
27 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)
28
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
29 Default Value for New Attributes 1.Adding Columns: ALTER TABLE student ADD (AddressVARCHAR2(50), B_DateDate SYSDATE ); 2.Modify Table Condition: ALTER TABLE student MODIFY (IDNUMBER(10,0), NameVARCHAR2(200) ); (continued)
30
30 Changing the name of a table: RENAME student TO GradStudent;
31
31 Adding Comments to a Table: COMMENT ON TABLE student IS ‘staff information’;
32
32 Add a constraint to an existing table: ALTER TABLE student ADD CONSTRAINT student_pk PRIMARY KEY (ID);
33
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
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
35 Constraints: Disable a constraint: ALTER TABLE student DISABLE CONSTRAINT student_pk ; Enable a constraint: ALTER TABLE student ENABLE CONSTRAINT student_pk;
36
36 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) );
37
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_constraints WHEREtable_name=‘student’;
38
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 WHEREtable_name=‘student’;;
39
39 Constraints: Example: SELECT * FROM USER_COL_COMMENTS; DROP COMMENTS: COMMENT ON COLUMN student.id IS‘ ’;
40
40 Practice: Add one column to customer table called Birth_Date Add 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 customer Rename customer table to customer2 table Change the data type of customer name ( last, first)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.