Oracle8 - The Complete Reference. Koch & Loney1 Chapter 17 Creating, Dropping, and Altering Tables and Views Presented by Victor Matos.

Slides:



Advertisements
Similar presentations
9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
Advertisements

Data Definition Language (DDL)
Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
SQL - Subqueries and Schema Chapter 3.4 V3.0 Napier University Dr Gordon Russell.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Constraints We have discussed three types of integrity constraints: primary keys, not null constraints, and unique constraints. CREATE TABLE Movies ( title.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Integrity 7. 1 CSE2316/3316 Database Management Systems Database Integrity.
Introduction to Structured Query Language (SQL)
SQL Components DML DDL DAL. Overview u Getting the records onto the disk - mapping u Managing disk space u SQL Modes u Ceating database.
Introduction to Structured Query Language (SQL)
Introduction to SQL  SQL or sequel  It is a standardised language with thousands of pages in the standard  It can be in database system through GUI,
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.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
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.
SQL (DDL & DML Commands)
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
10-1 Copyright  Oracle Corporation, All rights reserved. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Copyright  Oracle Corporation, All rights reserved. 10 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.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
11-1 Copyright  Oracle Corporation, All rights reserved. What Are Constraints? Constraints enforce rules at the table level. Constraints prevent.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
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.
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;
Creating and Managing Tables 14. ObjectivesObjectives After completing this lesson, you should be able to do the following: After completing this lesson,
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.
CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows.
Chapter 3 Table Creation and Management Oracle 10g: SQL.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
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.
Creating and Managing Tables. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically represents subsets.
2 Copyright © 2009, Oracle. All rights reserved. Managing Schema Objects.
Fundamentals of DBMS Notes-1.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Including Constraints
SQL Creating and Managing Tables
SQL Creating and Managing Tables
SQL Creating and Managing Tables
Oracle Data Definition Language (DDL)
Presentation transcript:

Oracle8 - The Complete Reference. Koch & Loney1 Chapter 17 Creating, Dropping, and Altering Tables and Views Presented by Victor Matos

Oracle8 - The Complete Reference. Koch & Loney2 Goals Create tables containing integrity rules. Use different datatypes for the columns of a table. Populate tables using the “insert into table” command. Populate tables using the subquery option. Create/drop views.

Oracle8 - The Complete Reference. Koch & Loney3 Data Structures in Oracle8 A database can include one or many: –TableStores data –ViewVirtual tables facilitating process –SequenceAutomatic serial numbers. –IndexImprove performance. –ConstraintDefine data behavior.

Oracle8 - The Complete Reference. Koch & Loney4 Tables Can be created at any time using SQL-DML. Do not need to have a pre-allocated storage. Can be modify on real-time. All data in a relational database is stored in tables.

Oracle8 - The Complete Reference. Koch & Loney5 Tables Each column is given a –column name, –a datatype (defining characteristics of the data to be entered in the column), and a –width (quantity of space to allocate for data to be entered into the column).

Oracle8 - The Complete Reference. Koch & Loney6 Tables Usually in a relational database, some of the columns in different tables contain the same information. In this way, the tables can refer to one another.

Oracle8 - The Complete Reference. Koch & Loney7 Create Table Command Syntax create table [schema.]tableName (column1datatype [DEFAULT expr] [column_constraint],... [table_Constraints] );

Oracle8 - The Complete Reference. Koch & Loney8 Create Table Command The components of the command are: –schemasame as the owner’s name. –tableNameis the name of the table. –columnis the name of the column. –datatypeOracle’s datatype and size. –column_contraintfield validation rule. –table_constraintintegrity rule on the table.

Oracle8 - The Complete Reference. Koch & Loney9 Create Table Command Example-1: Make the BADWEATHER table. drop table BADWEATHER / create table BADWEATHER ( CITY VARCHAR2(13) NOT NULL, SAMPLEDATE DATE NOT NULL, NOON NUMBER(4,1), MIDNIGHT NUMBER(4,1), PRECIPITATION NUMBER ) / remove previous table

Oracle8 - The Complete Reference. Koch & Loney10 Create Table Command Example-2: Create an EMPLOYEE table. CREATE TABLE scott.emp (empno NUMBER CONSTRAINT pk_emp PRIMARY KEY, ename VARCHAR2(10) CONSTRAINT nn_ename NOT NULL CONSTRAINT upper_ename CHECK (ename = UPPER(ename)), job VARCHAR2(9), mgr NUMBER CONSTRAINT fk_mgr REFERENCES scott.emp(empno), hiredate DATE DEFAULT SYSDATE, sal NUMBER(10,2) CONSTRAINT ck_sal CHECK (sal > 500), comm NUMBER(9,0) DEFAULT NULL, deptno NUMBER(2) CONSTRAINT nn_deptno NOT NULL CONSTRAINT fk_deptno REFERENCES scott.dept(deptno) ) PCTFREE 5 PCTUSED 75;

Oracle8 - The Complete Reference. Koch & Loney11 Referencing Another User’s Tables Constraints should reference objects in the current database. If the referred table does not belong to the user making the constraint, the owner’s name must be used to prefix the table name... references JANE.CUSTOMERS...

Oracle8 - The Complete Reference. Koch & Loney12 DEFAULT Option Indicates a default value to be used when inserting a record.... OrderDate DATE DEFAULT SysDate... Use only literal values (“...”), numbers, expressions, SQL functions such as USER, or SYSDATE.

Oracle8 - The Complete Reference. Koch & Loney13 Rules for Naming a Table TableName must begin with a letter. Up to 30-chars long. Alphabetic, digits, #, $, _ symbols are ok. Can not duplicate the name of another object already created by the user.

Oracle8 - The Complete Reference. Koch & Loney14 Oracle Datatypes

Oracle8 - The Complete Reference. Koch & Loney15 Deciding on a Proper Width A character column that is not wide enough for the data you want to put in it will cause an insert to fail and result in this message Error...ORA-1401:inserted value too large for column Maximum CHAR allocation is 2,000 chars. VARCHAR2 can store up to 4,000 chars.

Oracle8 - The Complete Reference. Koch & Loney16 NUMBER Precision Make a NUMBER field wide enough for your actual data, otherwise –Oracle will report an error “value too large”, or –Precision will be lost on some of the least significant digits. Decimals are rounded to the next valid representation.

Oracle8 - The Complete Reference. Koch & Loney17 Table Constraints Enforce maintenance-table behavior. Rules can be checked when a record is –inserted –deleted –updated Prevent the removal of referenced tables.

Oracle8 - The Complete Reference. Koch & Loney18 Table Constraints

Oracle8 - The Complete Reference. Koch & Loney19 The Candidate Key A candidate key is a combination of one or more columns, the values of which make a unique identifier for the row. drop table BADWEATHER; create table BADWEATHER ( CITY VARCHAR2(13) NOT NULL, SAMPLEDATE DATE NOT NULL, NOON NUMBER(4,1), MIDNIGHT NUMBER(4,1), PRECIPITATION NUMBER, UNIQUE (City, SampleDate) ) /

Oracle8 - The Complete Reference. Koch & Loney20 The Primary Key A primary key is one of the candidate keys. Only one primary-key per table is allowed. drop table BADWEATHER; create table BADWEATHER ( CITY VARCHAR2(13) NOT NULL, SAMPLEDATE DATE NOT NULL, NOON NUMBER(4,1), MIDNIGHT NUMBER(4,1), PRECIPITATION NUMBER, PRIMARY KEY (City, SampleDate) ) /

Oracle8 - The Complete Reference. Koch & Loney21 The Primary Key An in-line primary key is an immediate definition applying on only one column. drop table WORKER2; create table WORKER2 ( NameVARCHAR2(25) PRIMARY KEY, Age NUMBER, AddressVARCHAR(15) ) /

Oracle8 - The Complete Reference. Koch & Loney22 The Foreign Key A foreign-key is one or more columns which refer to the key of another table. drop table WORKER2; create table WORKER2 ( NameVARCHAR2(25) PRIMARY KEY, Age NUMBER, AddressVARCHAR(15) REFERENCES LODGING(Lodging) ) /

Oracle8 - The Complete Reference. Koch & Loney23 The Check Constraint Checks state that values of a column must be within certain valid ranges. drop table WORKER2; create table WORKER2 ( Name VARCHAR2(25) PRIMARY KEY, Age NUMBER CHECK (Age between 18 and 65), Address VARCHAR(15) ) / The condition must be TRUE for new records to be inserted

Oracle8 - The Complete Reference. Koch & Loney24 Naming Constraints All constraints are stored in the data dictionary. Name constraints are easy to manipulate –create / drop –enable / disable –alter.

Oracle8 - The Complete Reference. Koch & Loney25 Named Constraints Use a table_guide name combination, where guide is –table_PKPrimary Key –table_FK Foreign Key –table_UKUnique Key –table_Field_CK Check rule on Field.

Oracle8 - The Complete Reference. Koch & Loney26 Named Constraints drop table WORKER2 / create table WORKER2 ( Name VARCHAR2(25) constraint WORKER2_PK PRIMARY KEY, Age NUMBER constraint WORKER2_AGE_CK CHECK (Age Between 18 and 65), Address VARCHAR(15) constraint WORKER2_Address_FK REFERENCES LODGING(Lodging) ) /

Oracle8 - The Complete Reference. Koch & Loney27 Altering Tables Tables can be altered in two ways –changing a column’s definition (modify), or –adding a new column to an existing table (add) alter table LODGING add ( constraint Lodging_PK PRIMARY KEY (Lodging) ) NOTE: Use the DESC operator to see the current table structure.

Oracle8 - The Complete Reference. Koch & Loney28 Rules for Add/Modify Table When adding a column do not include the NOT NULL clause. You may add a NOT NULL in 3 steps –1. Add the column –2. Fill every possible row with data. –3. Modify the column to be NOT NULL. Increase CHAR/VARCHAR2 width at any time. Add more digits to a NUMBER at any time.

Oracle8 - The Complete Reference. Koch & Loney29 Rules for Views Views based on a single table can insert, delete, and update the underlying table. You can not insert if the base table has any not null columns not included in the view. You can not insert, update if columns in the view are generated with functions or calculations. You can not maintain the base table if the view includes: group by, distinct, rowNum.

Oracle8 - The Complete Reference. Koch & Loney30 Read-Only Views create or replace view RAIN as select City, Precipitation from BADWEATHER with read only; Comment: Read-only views are also called SNAP-SHOTS

Oracle8 - The Complete Reference. Koch & Loney31 Create a Table from a Table Use the create table... as select-statm. drop table RAIN; create table RAIN as select * from BADWEATHER; You can use the name of an existing object.

Oracle8 - The Complete Reference. Koch & Loney32 Create a Table from a Table To copy only the structure -and no data- do as in the example below create table RAIN as select * from BADWEATHER where true = false; The condition is false and results in no row selected

Oracle8 - The Complete Reference. Koch & Loney33 Index-Only Tables An index-only table is stored as if the entire table were an index. They can be used in order to increase performance enforce uniqueness of data Only one key can be used to index the table. Good idea only when data is very stable.

Oracle8 - The Complete Reference. Koch & Loney34 Index-Only Tables drop table WORKER3; create table WORKER3 ( Name VARCHAR2(25), Age NUMBER, Address VARCHAR(15) constraint WORKER3_PK PRIMARY KEY (Name) ) ORGANIZATION INDEX; /

Oracle8 - The Complete Reference. Koch & Loney35 Partitioned Tables Large tables could be split into more than one physical table-location. Partitioning may improve performance. create table WORKER5 ( Name VARCHAR2(25), Age NUMBER, Address VARCHAR(15), constraint WORKER5_PK PRIMARY KEY (Name) ) PARTITION by range (Name) (partition PART1 values less than ('F') tablespace Ohio1_TS, partition PART2 values less than (MAXVALUE) tablespace Ohio2_TS);

Oracle8 - The Complete Reference. Koch & Loney36 Summary SQL tables are created using the command create table [schema.]tablname ( column datatype [col_constraint],... ) [table_Constraint]; Constructing a table requires Table name, Column names, datatypes, lengths, Integrity constraints.

Oracle8 - The Complete Reference. Koch & Loney37 Summary Constraint types –NOT NULL –UNIQUE –PRIMARY KEY –FOREIGN KEY –CHECK UNIQUE indexes are created automatically with –PRIMARY KEY and –UNIQUE constraints