Chapter Four 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
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.
Advertisements

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.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
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.
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
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.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
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.
Chapter 5: Part 1: DDL STRUCTURED QUERY LANGUAGE (SQL)
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
1 Creating and Modifying Database Objects. 2 An Oracle database consists of multiple user accounts Each user account owns database objects Tables Views.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
SQL: DDL John Ortiz Cs.utsa.edu.
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Intro to SQL| MIS 2502  Spacing not relevant › BUT… no spaces in an attribute name or table name  Oracle commands keywords, table names, and attribute.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Chapter Six Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)
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.
1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.
Oracle 11g: SQL Chapter 4 Constraints.
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.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
11 Including Constraints Objectives At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints At the.
Lecture5: SQL Overview, Oracle Data Type, DDL and Constraints Ref. Chapter6 Lecture4 1.
1 SQL-2 Tarek El-Shishtawy Professor Ass. Of Computer Engineering.
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.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of.
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.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints.
Basic SQL*Plus edit and execute commands SQL*Plus buffer and built-in editor holds the last SQL statement Statements are created in free-flow style and.
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,
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)
SQL: Schema Definition and Constraints Chapter 6 week 6
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
STRUCTURED QUERY LANGUAGE
SQL data definition using Oracle
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
Oracle Data Definition Language (DDL)
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Chapter 2: Creating And Modifying Database Tables
SQL (Structured Query Language)
Presentation transcript:

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 TABLETable_Name (AttributeAttribute_Type, AttributeAttribute_Type);

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 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: NUMBER NUMBER(T,D) 1,234,567.89NUMBER 1,234, ,234,567.89NUMBER(9) 1,234,567 1,234,567.89NUMBER(9,1) 1,234, ,234, NUMBER(7,-2) 1,234,500 1,234, NUMBER(6) ??

11 Data Types: 3-Date: DATE MyBirthdate = ‘11-JAN-37’ Default time 00:00:00 A.M. Use TO_DATE()

12 DESCRIBEStudent; NameNull?Type NAMEVARCHAR2(80) IDNUMBER(9) GPA NUMBER(3,2) B_DateDATE MajorCHAR(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 (NameVARCHAR2(80)NOT NULL, IDNUMBER(9)NOT NULL, GPANUMBER(3,2), B_DateDATE, MajorCHAR(4));

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

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

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 Constraints FOREIGN KEY: FOREIGN KEY REFERENCES ON DELETE CASCADE

19 4 -Range Constraint: CREATE TABLE student (NameVARCHER2(80), IDNUMBER(9) CHECK (ID BETWEEN AND ), GPANUMBER(3,2) CHECK (GPA BETWEEN0.00 AND 4.00), B_DateDATE, MajorCHAR(4) ); Constraints

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 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 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 AND ) ); 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 record  Part 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 (AddressVARCHAR2(50), PhoneCHAR(10) ); 2.Modify Table Condition: ALTER TABLE student MODIFY (IDNUMBER(10,0), NameVARCHAR2(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 (AddressVARCHAR2(50), B_DateDate SYSDATE ); 2.Modify Table Condition: ALTER TABLE student MODIFY (IDNUMBER(10,0), NameVARCHAR2(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 (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 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 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 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_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)