Download presentation
Presentation is loading. Please wait.
1
1 A GUIDE TO ORACLE8 CHAPTER 2: Creating and ModifyingDatabaseTables 2
2
2 Creating and Modifying Database Tables 2 Query: Command to perform database operation insert modify delete view Structured Query Language (SQL) Standard query language for relational databases
3
3 Oracle Database Tables Table specifications table name field names field data types field sizes field constraints 2
4
4 Oracle Table and Field Names 2 1-30 characters Alphanumeric characters and restricted symbols $ _ # Must begin with a character Cannot be a reserved word
5
5 Oracle Data Types Data type: Specifies type of data stored in a field Error checking Efficient use of storage space 2
6
6 Character Data Types 2 VARCHAR2 Variable-length character strings Maximum of 4,000 characters Must specify maximum width allowed No trailing blank spaces are added Example declaration: student_name VARCHAR2(30)
7
7 Character Data Types CHAR Fixed-length character data Maximum size 255 characters Must specify maximum width allowed Adds trailing blank spaces to pad width Example declaration: s_gender CHAR(1) 2
8
8 Character Data Types 2 NCHAR Supports 16-digit binary character codes Used for alternate alphabets LONG Stores up to 2 GB of variable-length character data Each table can have only one LONG field
9
9 Number Data Type NUMBER Stores values between 10 -130 and 10 126 General declaration format: variablename NUMBER(precision, scale) 2
10
10 Number Data Type Number type (integer, fixed point, floating point) specified by precision and scale Precision: Total number of digits on either side of the decimal point Scale: Number of digits to right of decimal point 2
11
11 Integer Numbers Whole number with no digits to right of decimal point Precision is maximum width Scale is omitted Sample declaration: s_age NUMBER (2) 2
12
12 Fixed-Point Numbers Contains a specific number of decimal places Precision is maximum width Scale is number of decimal places Sample declaration: item_price NUMBER(5, 2) 2
13
13 Floating-Point Numbers Contains a variable number of decimal places Precision and scale are omitted Sample declaration: s_GPA NUMBER 2
14
14 Date Data Type DATE Stores dates from 1/1/4712 BC to 12/31/4712 AD Default date format: DD-MON-YY Example: 05-JUN-01 Sample declaration: s_dob DATE 2
15
15 Date Data Type 2 DATE data type also stores time values If no time value is given when a date is inserted, default value is 12:00:00 AM If no date value is given when a time is inserted, default date is first day of current month
16
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 codes 2
17
17 Format Masks 2 Specify input and output formats for data values Common NUMBER format masks Format MaskFormatted Data 99,99912,345 $99,999.99$12,345.00 99,999PR 99,999MI-12,345 $99,999.99PR
18
18 Format Masks Common DATE format masks Format MaskFormatted Data DD-MON-YY05-JUN-01 DD-MON-YYYY05-JUN-2001 MM/DD/YY06/05/2001 HH:MI AM02:30 PM MONTH DAY, YYYYJUNE 5, 2001 MM/DD/YY HH:MI AM06/05/01 02:30 PM 2
19
19 Format Masks 2 Common character format masks with embedded characters Format MaskFormatted Data Social Security Number: FM999”-”999”-”9999555-555-5555 Phone Number: FM”(“999”) “999”-”9999(715) 555-5555
20
20 Integrity Constraints Used to define primary and foreign keys Constraint name: Internal name used by DMBS to identify the constraint Constraint name convention: tablename_fieldname_constraintID Constraint ID values: Primary key: PK Foreign key: FK 2
21
21 Primary Key Constraints Defining a primary key: CONSTRAINT PRIMARY KEY Example: sid NUMBER(6) CONSTRAINT student_sid_pk PRIMARY KEY 2
22
22 Primary Key Constraints Defining a composite primary key: CONSTRAINT 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) 2
23
23 Foreign Key Constraints Defining a foreign key: CONSTRAINT REFERENCES ( ) Example: advisorid NUMBER(6) CONSTRAINT student_advisorid_fk REFERENCES faculty(fid) 2
24
24 Value Constraints Restricts data values that can be inserted into a field Types Check condition: Restricts to specific values example: s_gender (M or F) NOT NULL: Specifies that a field cannot be NULL 2
25
25 Defining Value Constraints Check condition CONSTRAINT CHECK s_gender CHAR(1) CONSTRAINT student_s_gender_cc CHECK ((s_gender = ‘M’) OR (s_gender = ‘F’)) Not NULL CONSTRAINT NOT NULL s_name VARCHAR2(30) student_s_name_nn NOT NULL 2
26
26 SQL*Plus 2 Oracle SQL command line utility Starting SQL*Plus
27
27 SQL*Plus 2 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
28 Creating a Database Table CREATE TABLE (, <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); 2
29
29 Other Table Operations Viewing a table’s structure DESCRIBE mystudent; Viewing constraint information SELECT CONSTRAINT_NAME FROM USER_CONSTRAINTS WHERE TABLE_NAME = ‘MYSTUDENT’ ; Deleting a table DROP TABLE mystudent; 2
30
30 Modifying Tables Prohibited Changing the table name Changing a column name Unrestricted Adding a new column Deleting a primary key or foreign key constraint 2
31
31 Modifying Tables Restricted (allowed only if existing data fits new specification) Changing a column’s data type, size, and default value Adding a primary key constraint Adding a foreign key constraint Adding a CHECK CONDITION constraint Adding a NOT NULL constraint 2
32
32 Exiting SQL*Plus Type exit at SQL> prompt or Click Close button 2
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.