Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Oracle8 - The Complete Reference. Koch & Loney1 Chapter 17 Creating, Dropping, and Altering Tables and Views Presented by Victor Matos."— Presentation transcript:

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

2 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.

3 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.

4 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.

5 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).

6 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.

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

8 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.

9 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

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

11 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...

12 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.

13 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.

14 Oracle8 - The Complete Reference. Koch & Loney14 Oracle Datatypes

15 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.

16 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.

17 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.

18 Oracle8 - The Complete Reference. Koch & Loney18 Table Constraints

19 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) ) /

20 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) ) /

21 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) ) /

22 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) ) /

23 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

24 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.

25 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.

26 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) ) /

27 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.

28 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.

29 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.

30 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

31 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.

32 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

33 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.

34 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; /

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

36 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.

37 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


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

Similar presentations


Ads by Google