+ All Categories
Home > Engineering > Oracle sql developer essentials

Oracle sql developer essentials

Date post: 18-Jul-2015
Category:
Author: alok-vishwakarma
View: 88 times
Download: 9 times
Share this document with a friend
Embed Size (px)
of 33 /33
Oracle SQL Developer Essentials TABLES AND INDEXES
Transcript

Oracle SQL Developer Essentials

Oracle SQL Developer EssentialsTables and indexesBASIC CREATE TABLE SYNTAXCREATE TABLE courses(department_code VARCHAR2(2) NOT NULL,course_number NUMBER(3,0) NOT NULL,course_title VARCHAR2(64) NOT NULL,course_description VARCHAR2(512) NOT NULL,credits NUMBER(3,1) NOT NULL,CONSTRAINT pk_coursesPRIMARY KEY (department_code, course_number),CONSTRAINT fk_courses_department_code FOREIGN KEY (department_code) REFERENCES departments (department_code)CONSTRAINT ck_courses_course_number CHECK (course_numberBETWEEN 100 AND 999))TABLESPACE usersPCTFREE 75;Column Naming rules30 characters maximumNames must be unique withing a tableNames can be reused in other table

Number of columns per table Maximum of 1000 columnsOver 255 columns result in row chaining

Column Definition RulesMinimum InformationNames and datatypes must be specifiedNull and default clauses are optionalCREATE TABLE students(student_id NUMBER(6) NOT NULL,first_name VARCHAR2(30) NOT NULL,last_name VARCHAR2(30) NOT NULL,email_address VARCHAR2(128) NOT NULL,phone VARCHAR2(20) NULL,city VARCHAR2(30),state VARCHAR2(2) ,status_code VARCHAR2(1) DEFAULT A NOT NULL,CONSTRAINT pk_studentsPRIMARY KEY (student_id));Null column valuesIf not specified , default it will accept null valuesCREATE TABLE orders(order_id NUMBER(10) NOT NULL,order_date DATE NOT NULL,customer_id NUMBER(10) NOT NULL,subtotal NUMBER(10,2),tax NUMBER(10,2),shipping NUMBER(10,2),grand_total NUMBER(10,2) AS (subtotal + tax + shipping) VIRTUAL);Virtual ColumnVirtual columns are not stored in physical disk spaceYou cannot perform insert or update operation on virtual column

Value is computed in the result set of the queryCannot insert or update virtual columnsCan only make use of columns in the same tableIndex can be created over virtual column valuesCREATE TABLE students(student_id NUMBER(6) NOT NULL,first_name VARCHAR2(30) NOT NULL,last_name VARCHAR2(30) NOT NULL,email_address VARCHAR2(128) NOT NULL,phone VARCHAR2(20) NULL,city VARCHAR2(30) NULL,state VARCHAR2(2) NULL,CONSTRAINT pk_students PRIMARY KEY (student_id));Primary KeyPrimary Key Types

1. Natural KeyCombination of naturally occurring columns that forms a unique key2. Surrogate Key- A unique value is generated for each row

Uniquely Identifies each rows in a columnCan be defined over one or more columnCannot contain a null value in primary key columnCREATE TABLE courses(department_code VARCHAR2(2) NOT NULL,course_number NUMBER(3,0) NOT NULL,course_title VARCHAR2(64) NOT NULL,course_description VARCHAR2(512) NOT NULL,credits NUMBER(3,1) NOT NULL,CONSTRAINT pk_coursesPRIMARY KEY (department_code, course_number));Primary Key Using constraint clauseAdding Primary Key to an Existing table

ALTER TABLE coursesCONSTRAINT pk_coursesADD PRIMARY KEY (department_code, course_number);SQL> SELECT rowid, zip_code, city, state2 FROM zip_codes;ROWID ZIP_C CITY ST----------------------- --------------------------------AAAXoNAAGAAAJzeAAA11201 Brooklyn NYAAAXoNAAGAAAJzeAAB75201 Dallas TXAAAXoNAAGAAAJzeAAC80401 Golden COAAAXoNAAGAAAJzeAAD92101 San Diego CAOracle ROWIDIt is not intended to use ROWID as a replacement for primary key.Foreign Key

Foreign Key ContinuesCREATE TABLE courses(department_code VARCHAR2(2) NOT NULL,course_number NUMBER(3,0) NOT NULL,course_title VARCHAR2(64) NOT NULL,course_description VARCHAR2(512) NOT NULL,credits NUMBER(3,1) NOT NULL,CONSTRAINT pk_coursesPRIMARY KEY (department_code, course_number),CONSTRAINT fk_courses_department_codeFOREIGN KEY (department_code) REFERENCES departments (department_code),);Add a Foreign Key to an Existing Table

ALTER TABLE coursesCONSTRAINT fk_courses_department_code ADD FOREIGN KEY (department_code)REFERENCES departments (department_code);On Delete CascadeCREATE TABLE courses(department_code VARCHAR2(2) NOT NULL,course_number NUMBER(3,0) NOT NULL,course_title VARCHAR2(64) NOT NULL,course_description VARCHAR2(512) NOT NULL,credits NUMBER(3,1) NOT NULL,CONSTRAINT pk_coursesPRIMARY KEY (department_code, course_number), CONSTRAINT fk_courses_department_code FOREIGN KEY (department_code) REFERENCES departments (department_code) ON DELETE CASCADE);Creating foreign key with ON DELETE CASCADEIn Case of ON DELETE CASCADE, if you delete any primary key in parent table, it will delete the corresponding record from the child table automatically.On Delete Cascade continues DELETE FROM courses WHERE department_code= PH;DELETE FROM departments WHERE department_code= PH;COMMIT;Deleting without ON DELETE CASCADETo delete a primary key value, no child records can existDEFERRED CONSTRAINTCREATE TABLE courses(department_code VARCHAR2(2) NOT NULL,course_number NUMBER(3,0) NOT NULL,course_title VARCHAR2(64) NOT NULL,course_description VARCHAR2(512) NOT NULL,credits NUMBER(3,1) NOT NULL,CONSTRAINT pk_courses PRIMARY KEY (department_code, course_number),CONSTRAINT fk_courses_department_code FOREIGN KEY (department_code) REFERENCES departments (department_code) DEFERRABLEINITIALLY IMMEDIATE-- INITIALLY DEFERRABLE);Creating foreign key with DEFERRED CONSTRAINTThis way we can tell oracle to defer enforcing the constraint until the transaction is commited not as soon as statement is executed.DEFERRED CONSTRAINT continues CREATE TABLE courses(department_code VARCHAR2(2) NOT NULL,course_number NUMBER(3,0) NOT NULL,course_title VARCHAR2(64) NOT NULL,course_description VARCHAR2(512) NOT NULL,credits NUMBER(3,1) NOT NULL,CONSTRAINT pk_courses PRIMARY KEY (department_code, course_number),CONSTRAINT fk_courses_department_code FOREIGN KEY (department_code) REFERENCES departments (department_code) DEFERRABLEINITIALLY IMMEDIATE);DEFERRED CONSTRAINT with INITIALLY IMMEDIATE If we use INITIALLY IMMEDIATE, we tell the oracle to enforce the constraint at statement level like Normal unless we specifically instruct the oracle to defer checking of the constraint inside of our transaction.

DEFERRED CONSTRAINT continues CREATE TABLE courses(department_code VARCHAR2(2) NOT NULL,course_number NUMBER(3,0) NOT NULL,course_title VARCHAR2(64) NOT NULL,course_description VARCHAR2(512) NOT NULL,credits NUMBER(3,1) NOT NULL,CONSTRAINT pk_courses PRIMARY KEY (department_code, course_number),CONSTRAINT fk_courses_department_code FOREIGN KEY (department_code) REFERENCES departments (department_code) DEFERRABLEINITIALLY DEFERRABLE);DEFERRED CONSTRAINT with INITIALLY DEFERRABLE If we use INITIALLY DEFERRABLE, we tell the oracle to enforce the constraint until the transaction commits.DEFERRED CONSTRAINT continues set constraint fk_courses_depatment_code deferred;

UPDATE departmentsSET department_code = COWHERE department_code = CS;

UPDATE coursesSET department_code = COWHERE department_code = CS;

COMMIT;Using DEFERRED CONSTRAINTCheck ConstraintsCREATE TABLE courses(department_code VARCHAR2(2) NOT NULL,course_number NUMBER(3,0) NOT NULL,course_title VARCHAR2(64) NOT NULL,course_description VARCHAR2(512) NOT NULL,credits NUMBER(3,1) NOT NULL,CONSTRAINT pk_courses PRIMARY KEY (department_code, course_number),CONSTRAINT fk_courses_department_codeFOREIGN KEY (department_code) REFERENCES departments (department_code),CONSTRAINT ck_courses_course_number CHECK (course_number BETWEEN 100 AND 999));

Check Constraints continues CREATE TABLE states(state_code VARCHAR2(2) NOT NULL,state_name VARCHAR2(30) NOT NULL,region VARCHAR2(2) NOT NULL,CONSTRAINT pk_states PRIMARY KEY (state_code),CONSTRAINT ck_state_regions CHECK (region IN (NE, SE, MW, SC, NW, SW)));To Check if a value is within a rangeCheck Constraints continues CREATE TABLE students(student_id NUMBER(6) NOT NULL,first_name VARCHAR2(30) NOT NULL,last_name VARCHAR2(30) NOT NULL,email_address VARCHAR2(128) NOT NULL,likes_ice_cream NUMBER(1) NULL,CONSTRAINT pk_students PRIMARY KEY (student_id),CONSTRAINT ck_studens_ice_creamCHECK (likes_ice_cream IN (0,1)));To emulate a Boolean columnCheck Constraints continues CREATE TABLE zip_codes(zip_code VARCHAR2(5) NOT NULL,city VARCHAR2(30) NOT NULL,state_code VARCHAR2(30) NOT NULL,CONSTRAINT pk_codesPRIMARY KEY (state_code),CONSTRAINT ck_zip_code_formatCHECK (REGEXP_LIKE (zip_code, '^[0-9]{5}$') ) );Validate Format Using a Regular ExpressionAdd a Constraint to an Existing Table

ALTER TABLE zip_codesADD CONSTRAINT ck_zip_codes_formatCHECK (REGEXP_LIKE (zip_code, '^[0-9]{5}$') );Disabling and Enabling ConstraintsALTER TABLE coursesDISABLE CONSTRAINT fk_courses_department_code;Disabling a constraintEnabling a constraint

ALTER TABLE coursesENABLE CONSTRAINT fk_courses_department_code;Check Constraints continues CREATE TABLE zip_codes(zip_code VARCHAR2(5) NOT NULL,city VARCHAR2(30) NOT NULL,state_code VARCHAR2(30) NOT NULL,CONSTRAINT pk_codesPRIMARY KEY (state_code),CONSTRAINT ck_zip_code_formatCHECK (REGEXP_LIKE (zip_code, '^[0-9]{5}$') ) );Validate Format Using a Regular ExpressionAdd a Constraint to an Existing Table

ALTER TABLE zip_codesADD CONSTRAINT ck_zip_codes_formatCHECK (REGEXP_LIKE (zip_code, '^[0-9]{5}$') );Table Storage OptionsOracle stores the tables and views in Database blocks

A Database block is like a shipping container

Table Storage OptionsWhat is a block ?

Block HeaderBlock type informationObject assignmentRow directoryRowActual data in our tabeFree spaceAllows for updates to rows.Table Storage OptionsHow Oracle Performs Data Access Operation ?

Full Table ScanIndex operation with Row Lookup.Full Table ScanOracle scans through every block in the tableComparable to a linear search in an array

Table Storage OptionsIndex Operation with Row lookup

Table Storage OptionsTable Data and Sort Order

Data in a table has no implied order.Oracle will insert the row wherever they fitsPhysical order in disk != Order data was inserted.

Data returned from queries is not sortedOrder of results is not order data was returnedTwo different executions can return same results in different order.

Table Storage OptionsTablespace Introductions

Table Storage OptionsTablespace Block Size

Row Chaining is when a row in table is so long that it will not fit in a single data block.So oracle has to chain the row and store it in two or more data block.It increases the amount of IO operation while performing database o/p.Table Storage OptionsSegment space management

Table Storage OptionsOracle Contains Multiple Tablespaces

Every user has a Default tablespaceSome objects may be created in and alternate tablespace

Table Storage OptionsSpecifying a tablespace for a tableCREATE TABLE course_enrollments(course_enrollment_id NUMBER NOT NULL,course_offering_id NUMBER(20) NOT NULL,student_id NUMBER(10) NOT NULL,grade_code VARCHAR2(1) NULL,CONSTRAINT pk_course_enrollments PRIMARY KEY (course_enrollment_id),CONSTRAINT fk_enrollments_offeringsFOREIGN KEY (course_offering_id) REFERENCES course_offerings(course_offering_id),CONSTRAINT fk_enrollments_studentFOREIGN KEY (student_id) REFERENCES students (student_id),CONSTRAINT fk_enrollments_grades FOREIGN KEY (grade_code) REFERENCES grades (grade_code))

TABLESPACE student_data;Table Storage OptionsHigh Water MarkHighest numbered block that has ever contained data in a table

For a newly created table, the block is at the left of the table because no data has ever been inserted into the table.


Recommended