CH 6

advertisement

CH 7 (Summary):

SQL

--a standard for relational DB

--used as DDL, DML

--portable

Benefits:

.reduced training costs

.enhanced productivity

.application portability

.multiple vendors

.application longevity

.cross systems communications

Creating DB Structure: steps

.

create SCHEMA

.create TABLES

.create VIEWS

.create indexes etc.

SCHEMA: an overall description of database structure where Tables will reside

CREATE SCHEMA name of schema: or

CREATE DATABASE dbname;

ORACLE schema is created for you for home work assignments

==

CREATE TABLEs:..SEE Pg 222 [RC]; ORACLE

HANDOUT; and lesson 17 (BF)

..define fields

..field type

..length

..null/not null

..unique

..primary key(s)

..foreign keys

Format:

CREATE TABLE tablename

(col name datatype col_constraints,

.

.

.

[primary key block]

[referential constraint]

.

.

.); constraints in Create table statements:

Constraint clause can constrain a single column or a group of columns in a table

It helps maintain data integrity

Primary key

Foreign key

Check conditions

Two ways to specify constraints:

..part of col def

(col constraint)

..at the end of the table

(table constraint)

Primary key:

A table can have only ONE primary key

P.K. can not have NULL values for single-column primary key, it can be defined with a column constraint ex:

WORKER (w_name, age, lodging)

Create table WORKER

(w_name varchar(25) Primary key , age number,

Lodging varchar(15)); or

Create table WORKER ( w_name varchar(25), age number,

Lodging varchar(15), primary key (name)); ex: Concatenated or composite PK

ENROLL(st_name, cl_name, grade)

PK is st_name, cl_name

Create table Enrollment (

ST_name varchar(30) not null, cl_name varchar(25) not null, grade smallint primary key (st_name, cl_name));

NOTE: A concatenated PK must be defined at the END

Foreign Key

: (SEE ORACLE HANDOUT AND PAGE 225-227 (RC),

LESSON 22 upto page 186 only (bf) known as referential integrity constraint ex:

WORKER (name, age, lodging)

LODGING (lodging_name , No_of_rooms, lodging_Address)

Lodging in table WORKER is a FK t o lodging_name in table LODGING

Create table WORKER( name... age... lodging..

REFERENCES LODGING );

will automatically reference PK of LODGING relation or foreign key(lodging) references LODGING(lodging_name)

CHECK constraint: lesson 22 upto page 186 only(bf)

many cols must have a value within certain range or to satisfy certain conditions, with a check constraint we can do that for every value in that column format: col type CHECK (cod) cod is any valid expression that tests True/false. ex: if age should be between 18 & 65 create table WORKER

(name.. age number

check

(age between 18 and 65), lodging...); or age...check (age > 12), ex: class char(20) not null check (class in ('SO', 'jr', 'sn') )

Data type:

(see ORACLE handout)

NUMBER

INTEGER

INTEGER(size)

CHAR (size)

LONG

DATE

VARCHAR2(size)

VARCHAR(size)

NUMBER: upto 40 digits, plus a space for a sign and dec. can be represented in regular notation, i.e., 0-9, +/- and a decimal or SCIENTIFIC notation

...NUMBER(size) col of specified size

...NUMBER(size,d) specified size with d digits after decimal.

NUMBER(5,2) can have nothing larger than (999.99)

INTEGER: same as NUMBER but does not accept decimal digits

INTEGER(size):integer of specified size)

CHAR(size): fixed-length character data. max size is 255

LONG: char data of variable size upto 2Gb long. Only one LONG column may be defined per table. Can not be used in subqueries, function expressions, where clauses or indexes

VARCHAR2(size):variable length char with max of size bytes

(upto 2000)

DATE: valid dates range from jan 1, 4712 B.C. to dec 31, 4712 A.D.

ORACLE default format:

01-Jan-05

can work with date and time

Blob: binary large objects

Can store multimedia applications like graphics, audio, video etc..

DATA NAMES:

. can be upto 30 char. long

.begin with a letter

.not case sensitive

.underscores are allowed

.no reserved words

DDL commands:

.

CREATE TABLE

.DROP TABLE

.ALTER TABLE

.CREATE INDEX

.DROP INDEX

.CREATE VIEW

.DROP VIEW

.CREATE SYNONYM

.DROP SYNONYM

.CREATE CLUSTER

.DROP CLUSTER

Drop Table: allows user to delete a table from the schema format:

DROP TABLE table-name ex:

ORDER(O#, O_date, Promised_date, C#)

CUSTOMER(C#, name, address, discount)

CREATE TABLE ORDER

(ORDERNO INTEGER NOT NULL,

ORDERDATE DATE , PROMISED_DATE DATE,

CUSTOMER_NO INTEGER NOT NULL REFERENCES CUSTOMER (c_no),

PRIMARY KEY(ORDERNO));

ADDITIONAL TABLE FEATURES:

CREATE SYNONYM:

FORMAT:

CREATE SYNONYM synname for tablename ex: create synonym MIS_STUDENT for STUDENT;

DROP synonym MIS_STUDENT

COMMIT/ROLLBACK

: used after insert, delete or update operations. Important when an error is discovered

commit: saves work of the session rollback: restores database to its original content since last commit

Oracle allows AUTOCOMMIT set autocommit show autocommit autocommit off: (default)

Altering Table structure(see ORACLE Handout), lesson 17

p 247- (RC) all changes in table structure are done thru ALTER command

.MODIFY allows changing existing column

.ADD allows adding new fields

NOTE: DELETEs are NOT allowed

ALTER format:

ALTER TABLE tablename

[ ADD (col name..., colname...)] or

ALTER TABLE tablename

[ MODIFY (col name..)] rules for ADDition:

.can add a col anytime if not null is not specified

.can add multiple col at one time

.can add a NULL column in three steps:

.add the col without not null

.fill every row

.modify col to not null

ex: want to ADD a new column ship_add to table ORDER

ALTER table ORDER

ADD (ship_add char(30) not null); will give errors, since nOT NULL is NOT allowed getting around the ADD problem: step 1: alter table order add (ship_add char(30)); step2: update table order set ship_add = 'UB'; step 3: alter table order modify (ship_add not null );

Entering Data in tables: lesson 15 (BF), p230(RC) and oracle handout

format:

INSERT INTO table-name VALUES ( val1, val2, val3...) values must be in the same order as the cols in the table structure char must be in single quotes date must be in quotes and default format (see ORACLE handout) ex: insert into order values (61396,’6-jun-97', '29-jun-97',1256); you can also insert a NULL value in a column..col will be left empty for this row insert into order values(61396,'6-jun-97', NULL,1256);

==========================

Changing Table contents (NOT TABLE STRUCTURE): lesson 17(BF), p234-(RC)

DELETE..used to remove rows from table

UPDATE: changes specific values for each column we wish to change delete format:

DELETE FROM tablename condition; delete from order where cust_num =

1234; you can rollback to undo this

UPDATE format:

UPDATE tablename

SET values

WHERE conditions;

UPDATE ORDER

SET Promised_date='12-jul-97'

Where cust_no=1256);

QUERIES:

SQL can be used as a DML to manipulate data: format:

SELECT col names

FROM table names

[Where condition(s)]

[Group BY..][HAVING.]

[UNION\INTERSECT\MINU

S

[ORDER BY....]

Additional readings

Http://otn.oracle.com

http://www.sqlcourse.com/

http://www.w3schools.com/sql/default.asp

Download