Database Programming Sections 9 & 10 – DDL Data Definition Language,

Slides:



Advertisements
Similar presentations
Data Definition Language (DDL)
Advertisements

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.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Sections 10 – Constraints
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Oracle Data Definition Language (DDL)
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Dr. Chen, Oracle Database System (Oracle) 1 Chapter 4 Constraints Jason C. H. Chen, Ph.D. Professor of MIS School of Business Gonzaga University Spokane,
Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Database Programming Sections 7 & 8 – Data Manipulation Language (DML) transaction, INSERT, implicit, explicit, USER, UPDATE, DELETE, integrity constraint,
Database Programming Sections 9 – Constraints. Marge Hohly2 CONSTRAINT TYPES  NOT NULL Constraints  UNIQUE Constraints  PRIMARY KEY Constraints  FOREIGN.
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.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
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.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Managing Constraints. 2 home back first prev next last What Will I Learn? Four different functions that the ALTER statement can perform on constraints.
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Schema Objects.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
11 Including Constraints Objectives At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints At the.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
SQL CREATING AND MANAGING TABLES lecture4 1. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically.
Database Programming Sections 7–Multi-row sub queries, IN, ANY, ALL, Data Manipulation Language (DML) transaction, INSERT, implicit, explicit, USER, UPDATE,
Copyright © 2004, Oracle. All rights reserved. Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes ORACLE.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
DDL and Views. Database Objects Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage;
Database Programming Sections 7– Data Manipulation Language (DML) transaction, INSERT, implicit, explicit, USER, UPDATE, DELETE, integrity constraint,
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.
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)
Chapter 3 Table Creation and Management Oracle 10g: SQL.
Installation Oracle 11g Express 2 double click the "setup" button to install the Oracle.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Creating and Managing Tables. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically represents subsets.
Database Programming Sections 7 & 8 – Data Manipulation Language (DML) transaction, INSERT, implicit, explicit, USER, UPDATE, DELETE, integrity constraint,
2 Copyright © 2009, Oracle. All rights reserved. Managing Schema Objects.
Fundamentals of DBMS Notes-1.
Fundamental of Database Systems
Using DDL Statements to Create and Manage Tables
Including Constraints
SQL: Schema Definition and Constraints Chapter 6 week 6
SQL Creating and Managing Tables
SQL Creating and Managing Tables
SQL Creating and Managing Tables
Managing Objects with Data Dictionary Views
SQL data definition using Oracle
Managing Schema Objects
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
Oracle Data Definition Language (DDL)
Database Management System
IST 318 Database Administration
Including Constraints
Presentation transcript:

Database Programming Sections 9 & 10 – DDL Data Definition Language,

Marge Hohly2 Data Definition Language DDL  Data Definition Language DDL ALTER TABLE DROP TABLE RENAME TRUNCATE COMMENT

Marge Hohly3 ALTER TABLE  Use ALTER TABLE to: ADD a new column (Always goes at the end of the table) MODIFY an existing column (Change data type, size, or default value – see restrictions) Define a DEFAULT value for a column DROP COLUMN (Cannot drop all columns) SET UNUSED/DROP UNUSED

Marge Hohly4 ALTER TABLE Examples  ALTER TABLE copy_items ADD(price number(8));  ALTER TABLE copy_items MODIFY(price number(7));  ALTER TABLE copy_items DROP COLUMN price;  adding a new column  modifying an existing column width, datatype, default value only affects subsequent insertions  dropping a column – one at a time must have at least one column left

Marge Hohly5 ALTER TABLE Examples cont’d  ALTER TABLE copy_items SET UNUSED (qty_on_hand);  ALTER TABLE copy_items DROP UNUSED COLUMNS;  set unused so it can be dropped later – no access once set unused  reclaim disk space

Marge Hohly6 TRUNCATE  Removes all rows in a table  Releases storage space where as DROP does not release storage space  Example: TRUNCATE TABLE copy_items;

Marge Hohly7 COMMENT ON TABLE  Table Example: COMMENT ON TABLE items IS ‘Inventory items begin with a default qty_on_hand of zero’; View this comment using: SELECT * FROM USER_TAB_COMMENTS;  Column Example: COMMENT ON COLUMN copy_items.qty_on_hand IS ‘begin with a default value of one’; View this comment using DESC copy_items;

Marge Hohly8 CONSTRAINT TYPES  NOT NULL Constraints  UNIQUE Constraints  PRIMARY KEY Constraints  FOREIGN KEY Constraints  CHECK Constraints

Marge Hohly9 Defining CONSTRAINTS  What are constraints? Database rules  Constraints always have a name Given by you/DBA when constraint is created (preferred method because names are meaningful) Given by the system when constraint is created (names are not meaningful)

Marge Hohly10 Defining CONSTRAINTS  Two Ways to Define Constraints during Table Creation Table-Level  If the word CONSTRAINT is used in the CREATE TABLE statement, the constraint must be given a name  Composite-key constraints must be defined at the table-level Column-Level  NOT NULL must be defined at the Column- Level

Marge Hohly11 Defining CONSTRAINTS  Table-Level Constraints – at the bottom EXAMPLE: CREATE TABLE copy_employees( employee_id NUMBER(6), first_name VARCHAR2(20), job_id VARCHAR2(10), CONSTRAINT cemp_emp_id_pk PRIMARY KEY(employee_id), CONSTRAINT cemp_job_id_fk FOREIGN KEY(job_id) REFERENCES jobs(job_id), CONSTRAINT cemp_first_name_uk UNIQUE (first_name), CONSTRAINT cemp_emp_id_ck CHECK (employee_id<=999999)); Note: The words “Foreign Key” are Used at the table level

Marge Hohly12 NAMING at TABLE LEVEL  Constraint naming format table_col_type  CONSTRAINT constraint_name TYPE OF CONSTRAINT(column_name) CONSTRAINT cemp_emp_id_pk PRIMARY KEY(employee_id) CONSTRAINT cemp_emp_id_lname_pk PRIMARY KEY(employee_id,last_name )  CONSTRAINT constraint_name TYPE OF CONSTRAINT(column_name) REFERENCES othertablename(column_name) CONSTRAINT cemp_job_id_fk FOREIGN KEY(job_id) REFERENCES copy_jobs(job_id),

Marge Hohly13 NAMING at COLUMN LEVEL  Column Level Assigning A Constraint Name:  System Named: column_name datatype() TYPE OF CONSTRAINT employee_id NUMBER(6) PRIMARY KEY  User Named: column_name datatype() CONSTRAINT constraint name TYPE OF CONSTRAINT employee_id NUMBER(6) CONSTRAINT c2emp_emp_id_pk PRIMARY KEY  Foreign Key: column_name datatype() CONSTRAINT constraint_name TYPE OF CONSTRAINT (column it is on) REFERENCES othertablename(column_name)

Marge Hohly14 Defining CONSTRAINTS  Column-Level Constraints Example: CREATE TABLE copy2_employees( employee_id NUMBER(6) CONSTRAINT c2emp_emp_id_pk PRIMARY KEY, CONSTRAINT c2emp_emp_id_ck CHECK(employee_id<=999999), first_name VARCHAR2(20) CONSTRAINT c2emp_first_name_nn NOT NULL, last_name VARCHAR2(20) CONSTRAINT c2emp_last_name_nn NOT NULL, address VARCHAR2(20) CONSTRAINT c2emp_address_ck NOT NULL, job_id VARCHAR2(10) CONSTRAINT c2emp_job_id_fk REFERENCES copy_jobs(job_id));

Marge Hohly15 Adding Constraints AFTER Table is created:  First, create a table that does not already have constraints: CREATE TABLE copy3_employees( employee_id NUMBER(6), first_name VARCHAR2(20), last_name VARCHAR2(20), department_id NUMBER(4));

Marge Hohly16 Adding Constraints AFTER Table is created:  Secondly, add the constraints: ALTER TABLE copy3_employees ADD CONSTRAINT emp3_emp_id_pk PRIMARY KEY(employee_id); ALTER TABLE copy3_employees ADD CONSTRAINT emp3_emp_id_fk FOREIGN KEY(department_id) REFERENCES copy_departments(department_id);

Marge Hohly17 Adding Constraints AFTER Table is created:  NOTE!!! For NOT NULL constraints, use the MODIFY keyword in the ALTER TABLE statement instead of ADD ALTER TABLE copy3_employees MODIFY (first_name CONSTRAINT emp3_first_name_nn NOT NULL);  NOT NULL constraints can only be added if the column does not already contain null values

Marge Hohly18 Miscellaneous Constraint Information...  If the word CONSTRAINT is used in a CREATE TABLE statement, the constraint must be given a name  Constraints that contain more than one column are called composite key constraints and must be specified at the table level by placing a comma between the column names  There is no limit to the number of CHECK CONSTRAINTS that can be specified for a column

Marge Hohly19 Miscellaneous FK Constraints Information...  Another name for FOREIGN KEY CONSTRAINTS is REFERENCIAL INTEGRITY CONSTRAINTS  When specifying FOREIGN KEY CONSTRAINTS, the table that contains the PRIMARY KEY is called the PARENT TABLE. The table that contains the FOREIGN KEY CONSTRAINT is called the CHILD TABLE.

Marge Hohly20 DISABLING CONSTRAINTS  Constraints can be disabled Examples:  ALTER TABLE copy3_employees DISABLE CONSTRAINT emp3_emp_id_pk;  ALTER TABLE copy3_employees DISABLE CONSTRAINT emp3_emp_id_pk CASCADE; This will cause any FOREIGN KEY that references this primary key to also be disabled.

Marge Hohly21 ENABLING CONSTRAINTS  EXAMPLES: ALTER TABLE copy3_employees ENABLE CONSTRAINT emp3_emp_id_pk Note: This does not enable the foreign key in the child tables

Marge Hohly22 DROPPING CONSTRAINTS  Examples : ALTER TABLE table_name DROP CONSTRAINT TYPE (column_name)[CASCADE]; ALTER TABLE table_name DROP CONSTRAINT name[CASCADE]; ALTER TABLE c_clients DROP PRIMARY KEY CASCADE;

Marge Hohly23 Viewing Constraint  Use the DESCRIBE command to confirm its existence.  DESCRIBE can only verify is the NOT NULL constraint.  NOT NULL constraint appears in the data dictionary as a CHECK constraint.  Use a query of the USER_CONSTRAINTS table to view all constraints on your table.  SELECT constraint_name, constraint_type FROM user_constraints WHERE TABLE_NAME ='table_name';  SELECT constraint_name, constraint_type FROM user_constraints WHERE TABLE_NAME ='COPY3_EMPLOYEES‘;

Marge Hohly24 QUERY THE DATA DICTIONARY SELECT constraint_name, constraint_type, table_name, status FROM user_constraints;  Types: P = Primary Key R = Foreign Key (Referential) C = Check (Includes NOT NULL) U = Unique

Marge Hohly25 Viewing Constraint  SELECT constraint_name, column_name FROM user_cons_columns WHERE table_name = 'EMPLOYEES';

Marge Hohly26 Viewing Constraints