Vous êtes sur la page 1sur 12

Creating and Modifying Database

Tables
4 Query: Command to perform database operation
4 insert
4 modify
4 delete
4 view

Oracle Database Tables


4 Table specifications
4 table name
4 field names
4 field data types
4 field sizes
4 field constraints

Oracle Table and Field Names


4 1-30 characters
4 Alphanumeric characters and restricted symbols
$ _ #

4 Must begin with a character

4 Cannot be a reserved word


4 Cannot name a table TABLE

Oracle Data Types


4 Data type: Specifies type of data stored in a field
4 Error checking
4 Efficient use of storage space

Character Data Types


4 VARCHAR2
4 Variable-length character strings
4 Maximum of 4,000 characters
4 Must specify maximum width allowed
4 No trailing blank spaces are added

4 Example declaration:
student_name VARCHAR2(30)

Character Data Types


4 CHAR
4 Fixed-length character data
4 Maximum size 255 characters
4 Must specify maximum width allowed
4 Adds trailing blank spaces to pad width

4 Example declaration:
s_gender CHAR(1)

Character Data Types


4 NCHAR
4 Supports 16-digit binary character codes
4 Used for alternate alphabets

4 LONG
4 Stores up to 2 GB of variable-length character data
4 Each table can have only one LONG field

Number Data Type


4 NUMBER
4 Stores values between 10-130 and 10126
General declaration format:

variablename NUMBER(precision, scale)

Number Data Type


4 Number type (integer, fixed point, floating point) specified
by precision and scale
4 Precision: Total number of digits on either side of the decimal point
4 Scale: Number of digits to right of decimal point

Integer Numbers
4 Whole number with no digits to right of decimal point
4 Precision is maximum width
4 Scale is omitted
4 Sample declaration:
s_age NUMBER (2)

Fixed-Point Numbers
4 Contains a specific number of decimal places
4 Precision is maximum width
4 Scale is number of decimal places
4 Sample declaration:
item_price NUMBER(5, 2)

Floating-Point Numbers

4 Contains a variable number of decimal places


4 Precision and scale are omitted
4 Sample declaration:
s_GPA NUMBER

Date Data Type


4 DATE
4 Stores dates from 1/1/4712 BC to 12/31/4712 AD

4 Default date format: DD-MON-YY


4 Example: 05-JUN-01

4 Sample declaration:
s_dob DATE

Date Data Type


4 DATE data type also stores time values
4 If no time value is given when a date is inserted, default value is
12:00:00 AM
4 If no date value is given when a time is inserted, default date is first
day of current month

Large Object (LOB) Data Types


4 BLOB: Binary LOB, up to 4 GB of binary data in database
4 CLOB: Character LOB, up to 4 GB of character data in
database
4 BFILE: Reference to binary file stored in operating system
4 NCLOB: Character LOB supporting 16-bit character codes

Format Masks
4 Specify input and output formats for data values
4 Common NUMBER format masks
Format Mask

Formatted Data

99,999

12,345

$99,999.99

$12,345.00

99,999PR

<12,345>

99,999MI

-12,345

$99,999.99PR

<$12,345.00>

Format Masks
4 Common DATE format masks
Format Mask

Formatted Data

DD-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

Integrity Constraints
4 Used to define primary and foreign keys
4 Constraint name: Internal name used by DMBS to identify
the constraint
4 Constraint name convention:
4 tablename_fieldname_constraintID

4 Constraint ID values:
4 Primary key: PK
4 Foreign key: FK

Primary Key Constraints


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

4 Example:
sid NUMBER(6)
CONSTRAINT student_sid_pk PRIMARY KEY

Primary Key Constraints


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

4 Example:
sid NUMBER(6),
course_id NUMBER(6),
grade NUMBER ,
CONSTRAINT enrollment_sid_course_id_pk PRIMARY KEY (sid,
course_id)

Foreign Key Constraints


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

4 Example:
advisorid NUMBER(6)
CONSTRAINT student_advisorid_fk
REFERENCES faculty(fid)

Value Constraints
4 Restricts data values that can be inserted into a field

4 Types
4 Check condition: Restricts to specific values
4 example: s_gender (M or F)

4 NOT NULL: Specifies that a field cannot be NULL

Defining Value Constraints


4 Check condition
4 CONSTRAINT <constraint name> CHECK <values>
s_gender CHAR(1) CONSTRAINT student_s_gender_cc CHECK
((s_gender = M) OR (s_gender = F))

4 Not NULL
4 CONSTRAINT <constraint_name> NOT NULL
s_name VARCHAR2(30) student_s_name_nn

NOT NULL

SQL*Plus
4 Oracle SQL command line utility
4 Starting SQL*Plus

SQL*Plus
4 All commands must be terminated with a semicolon

4 Use a text editor and copy and paste commands


4 Character data is case sensitive and must be in single quotes
M
Sarah

Creating a Database Table


4 CREATE TABLE <tablename>
(<field1 declaration>,
<field2 declaration, );

CREATE TABLE mystudent


(sid NUMBER(6) CONSTRAINT mystudent_sid_pk PRIMARY KEY,
s_name VARCHAR2(30) CONSTRAINT
NOT NULL);

mystudent_s_name_nn

Other Table Operations


4 Viewing a tables structure
DESCRIBE mystudent;

4 Viewing constraint information


SELECT CONSTRAINT_NAME
FROM USER_CONSTRAINTS

WHERE TABLE_NAME = MYSTUDENT;

4 Deleting a table
DROP TABLE mystudent;

Modifying Tables
4 Prohibited
4 Changing the table name
4 Changing a column name

4 Unrestricted
4 Adding a new column
4 Deleting a primary key or foreign key constraint

Modifying Tables
4 Restricted (allowed only if existing data fits new
specification)
4 Changing a columns data type, size, and default value
4 Adding a primary key constraint
4 Adding a foreign key constraint
4 Adding a CHECK CONDITION constraint
4 Adding a NOT NULL constraint

Vous aimerez peut-être aussi