1 a guide to oracle8 chapter 2: creating and modifyingdatabasetables 2

32
1 A GUIDE TO ORACLE8 CHAPTER CHAPTER 2: 2: Creating and Creating and Modifying Modifying Database Database Tables Tables 2 2

Post on 19-Dec-2015

229 views

Category:

Documents


3 download

TRANSCRIPT

1

A GUIDE TO

ORACLE8

CHAPTERCHAPTER 2:2:Creating andCreating andModifyingModifyingDatabaseDatabaseTablesTables

22

2

Creating and Modifying Database Tables

22

Query: Command to perform database operationinsertmodifydelete view

Structured Query Language (SQL)Standard query language for relational

databases

3

Oracle Database Tables

Table specificationstable namefield namesfield data typesfield sizesfield constraints

22

4

Oracle Table and Field Names

22

1-30 charactersAlphanumeric characters and

restricted symbols$ _ #

Must begin with a characterCannot be a reserved word

5

Oracle Data Types

Data type: Specifies type of data stored in a fieldError checkingEfficient use of storage space

22

6

Character Data Types

22

VARCHAR2Variable-length character stringsMaximum of 4,000 charactersMust specify maximum width allowedNo trailing blank spaces are added

Example declaration:student_name VARCHAR2(30)

7

Character Data Types

CHARFixed-length character dataMaximum size 255 charactersMust specify maximum width allowedAdds trailing blank spaces to pad width

Example declaration:s_gender CHAR(1)

22

8

Character Data Types

22

NCHARSupports 16-digit binary character codesUsed for alternate alphabets

LONGStores up to 2 GB of variable-length

character dataEach table can have only one LONG

field

9

Number Data Type

NUMBERStores values between 10-130 and 10126

General declaration format:variablename NUMBER(precision, scale)

22

10

Number Data Type

Number type (integer, fixed point, floating point) specified by precision and scalePrecision: Total number of digits on

either side of the decimal pointScale: Number of digits to right of

decimal point

22

11

Integer Numbers

Whole number with no digits to right of decimal point

Precision is maximum widthScale is omittedSample declaration:

s_age NUMBER (2)

22

12

Fixed-Point Numbers

Contains a specific number of decimal places

Precision is maximum widthScale is number of decimal placesSample declaration:

item_price NUMBER(5, 2)

22

13

Floating-Point Numbers

Contains a variable number of decimal places

Precision and scale are omittedSample declaration:

s_GPA NUMBER

22

14

Date Data Type

DATEStores dates from 1/1/4712 BC to

12/31/4712 AD

Default date format: DD-MON-YYExample: 05-JUN-01

Sample declaration:s_dob DATE

22

15

Date Data Type

22

DATE data type also stores time valuesIf no time value is given when a date is

inserted, default value is 12:00:00 AMIf no date value is given when a time is

inserted, default date is first day of current month

16

Large Object (LOB) Data Types

BLOB: Binary LOB, up to 4 GB of binary data in database

CLOB: Character LOB, up to 4 GB of character data in database

BFILE: Reference to binary file stored in operating system

NCLOB: Character LOB supporting 16-bit character codes22

17

Format Masks

22

Specify input and output formats for data values

Common NUMBER format masks

Format Mask Formatted Data99,999 12,345

$99,999.99 $12,345.00

99,999PR <12,345>

99,999MI -12,345

$99,999.99PR <$12,345.00>

18

Format Masks

Common DATE format masks

Format Mask Formatted DataDD-MON-YY 05-JUN-01

DD-MON-YYYY 05-JUN-2001

MM/DD/YY 06/05/2001

HH:MI AM 02:30 PM

MONTH DAY, YYYY JUNE 5, 2001

MM/DD/YY HH:MI AM 06/05/01 02:30 PM

22

19

Format Masks

22

Common character format masks with embedded characters

Format Mask Formatted DataSocial Security Number:

FM999”-”999”-”9999 555-555-5555Phone Number:

FM”(“999”) “999”-”9999 (715) 555-5555

20

Integrity Constraints

Used to define primary and foreign keysConstraint name: Internal name used by DMBS

to identify the constraintConstraint name convention:

tablename_fieldname_constraintIDConstraint ID values:

Primary key: PKForeign key: FK22

21

Primary Key Constraints

Defining a primary key:CONSTRAINT <constraint name> PRIMARY KEY

Example:sid NUMBER(6)

CONSTRAINT student_sid_pk PRIMARY KEY

22

22

Primary Key Constraints

Defining a composite primary key:CONSTRAINT <constraint name> PRIMARY KEY (field1, field2)

Example:sid NUMBER(6),

course_id NUMBER(6),

grade NUMBER ,

CONSTRAINT enrollment_sid_course_id_pk PRIMARY KEY (sid, course_id)22

23

Foreign Key Constraints

Defining a foreign key:CONSTRAINT <constraint name> REFERENCES <table where field is a PK>(<field name>)

Example:advisorid NUMBER(6)

CONSTRAINT student_advisorid_fk

REFERENCES faculty(fid)

22

24

Value Constraints

Restricts data values that can be inserted into a field

TypesCheck condition: Restricts to specific values

example: s_gender (M or F)

NOT NULL: Specifies that a field cannot be NULL

22

25

Defining Value Constraints

Check conditionCONSTRAINT <constraint name> CHECK <values>

s_gender CHAR(1) CONSTRAINT student_s_gender_cc CHECK

((s_gender = ‘M’) OR (s_gender = ‘F’))

Not NULLCONSTRAINT <constraint_name> NOT NULL

s_name VARCHAR2(30) student_s_name_nn NOT NULL22

26

SQL*Plus

22

Oracle SQL command line utilityStarting SQL*Plus

27

SQL*Plus

22

All commands must be terminated with a semicolon

Use a text editor and copy and paste commands

Character data is case sensitive and must be in single quotes‘M’

‘Sarah’

28

Creating a Database Table

CREATE TABLE <tablename>(<field1 declaration>,

<field2 declaration, …);

CREATE TABLE mystudent

(sid NUMBER(6) CONSTRAINT mystudent_sid_pk PRIMARY KEY,

s_name VARCHAR2(30) CONSTRAINT mystudent_s_name_nn NOT NULL);22

29

Other Table Operations

Viewing a table’s structureDESCRIBE mystudent;

Viewing constraint informationSELECT CONSTRAINT_NAME

FROM USER_CONSTRAINTS

WHERE TABLE_NAME = ‘MYSTUDENT’;

Deleting a tableDROP TABLE mystudent;22

30

Modifying Tables

ProhibitedChanging the table nameChanging a column name

UnrestrictedAdding a new columnDeleting a primary key or foreign key constraint

22

31

Modifying Tables

Restricted (allowed only if existing data fits new specification)Changing a column’s data type, size, and default

valueAdding a primary key constraintAdding a foreign key constraintAdding a CHECK CONDITION constraintAdding a NOT NULL constraint22

32

Exiting SQL*Plus

Type exit at SQL> promptor

Click Close button

22