Chapter 2: Creating And Modifying Database Tables Guide to Oracle 10g
Objectives After completing this chapter, you should be able to: Use structured query language (SQL) commands to create, modify, and drop database tables Explain Oracle 10g user schemas Define Oracle 10g database tables Create database tables using SQL*Plus Debug Oracle 10g SQL commands and use online help resources available through the Oracle Technology Network (OTN) Guide to Oracle 10g
Objectives (continued) View information about your database tables using Oracle 10g data dictionary views Modify and delete database tables using SQL*Plus Guide to Oracle 10g
Introduction to SQL Structured query language (SQL) Standard query language for relational databases Consists of about 30 commands Enables users to create database objects and manipulate and view data SQL-99 Most recent version Guide to Oracle 10g
Introduction to SQL (continued) Structured query language (SQL) (continued) Basic categories for SQL commands Data definition language (DDL) Data manipulation language (DML) Reserved words SQL command words Guide to Oracle 10g
Oracle 10g User Accounts User account User schema Database objects Created for each user Identified using unique username and password User schema Area of database belonging to user Database objects Also called schema objects Objects in user schema Guide to Oracle 10g
Defining Oracle 10g Database Tables Primary data objects in relational database Constraints Restrictions on data values that column can store Oracle naming standard Rules that Oracle corporation has established for naming all database objects Guide to Oracle 10g
Defining Oracle 10g Database Tables (continued) Oracle naming standard One to 30 characters long Contain letters, numbers, and special symbols $, _, and # Begin with character CREATE TABLE SQL syntax CREATE TABLE tablename (columnname1 data_type, columnname2 data_type, …) Guide to Oracle 10g
Oracle 10g Data Types Data type Specifies kind of data that column stores Provides means for error checking Enable DBMS to use storage space more efficiently Basic types Character Number Date/time Large object Guide to Oracle 10g
Character Data Types VARCHAR2 CHAR Variable-length character data Syntax columnname VARCHAR2(maximum_size) CHAR Fixed-length character data columnname CHAR[(maximum_size)] Guide to Oracle 10g
Character Data Types (continued) Unicode Standardized technique that provides way to encode data in diverse languages NVARCHAR2 Counterpart of VARCHAR2 Uses Unicode coding NCHAR Counterpart of CHAR Uses Unicode encoding Guide to Oracle 10g
Number Data Types NUMBER Precision Used for all numeric data Syntax columnname NUMBER [([precision,] [scale])] Precision Total number of digits both to left and right of decimal point Guide to Oracle 10g
Number Data Types (continued) Integer number syntax columnname NUMBER(precision) Fixed-point number Contains specific number of decimal places Column declaration specifies both precision and scale Example price NUMBER(5, 2) Guide to Oracle 10g
Number Data Types (continued) Floating-point number Contains variable number of decimal places Syntax columnname NUMBER Example s_gpa NUMBER Guide to Oracle 10g
Date And Time Data Types Datetime data subtypes Store actual date and time values DATE TIMESTAMP Interval data subtypes Store elapsed time interval between two datetime values INTERVAL YEAR TO MONTH INTERVAL DAY TO SECOND Guide to Oracle 10g
Date And Time Data Types (continued) Stores dates from December 31, 4712 BC to December 31, AD 4712 Default date format DD-MON-YY Default time format HH:MI:SS AM Syntax: columnname DATE Guide to Oracle 10g
Date And Time Data Types (continued) TIMESTAMP Stores date values similar to DATE data type Also stores fractional seconds Syntax columnname TIMESTAMP (fractional_seconds_precision) Guide to Oracle 10g
Date And Time Data Types (continued) INTERVAL YEAR TO MONTH Stores time interval expressed in years and months Syntax +|– elapsed_years-elapsed_months Guide to Oracle 10g
Date And Time Data Types (continued) INTERVAL DAY TO SECOND Stores time interval expressed in days, hours, minutes, and seconds Syntax +|– elapsed_days elapsed_hours:elapsed_minutes:elapsed_seconds Guide to Oracle 10g
Large Object (LOB) Data Types Store binary data such as: Digitized sounds or images References to binary files from word processor or spreadsheet General syntax columnname Lob_data_type Guide to Oracle 10g
Large Object (LOB) Data Types (continued) Guide to Oracle 10g
Constraints Rules that restrict data values that can be entered into column Types of constraints: Integrity constraints Value constraints Table constraint Restricts data value with respect to all other values in table Guide to Oracle 10g
Constraints (continued) Column constraint Limits value that can be placed in specific column Irrespective of values that exist in other table rows Constraint definitions should be placed either: At end of CREATE TABLE command after table columns declared Within each column definition Guide to Oracle 10g
Constraints (continued) Constraint naming convention tablename_columnname_constraintid Guide to Oracle 10g
Common ConstraintID Abbreviations Guide to Oracle 10g
Integrity Constraints Primary key Syntax (within table definition) CONSTRAINT constraint_name PRIMARY KEY Syntax (at end of table definition) CONSTRAINT constraint_name PRIMARY KEY (columnname) Guide to Oracle 10g
Integrity Constraints (continued) Foreign key Column constraint Specifies that value user inserts in column must exist as primary key in referenced table Syntax (placed at end of table definition) CONSTRAINT constraint_name FOREIGN KEY (columnname) REFERENCES primary_key_tablename (primary_key_columnname) Guide to Oracle 10g
Integrity Constraints (continued) Foreign key (continued) Syntax (placed within table definition) CONSTRAINT constraint_name REFERENCES primary_key_tablename (primary_key_columnname) Composite key Syntax PRIMARY KEY (columnname1, columnname2 …) Guide to Oracle 10g
Integrity Constraints (continued) Value constraints Column-level constraints Restrict data values that users can enter Commonly used value constraints CHECK conditions NOT NULL constraint DEFAULT constraint UNIQUE constraint Guide to Oracle 10g
Creating Database Tables Using SQL*Plus Start SQL*Plus Type username and password Type SQL commands at SQL prompt End each command with semicolon (;) Press Enter to submit commands Guide to Oracle 10g
Creating Database Tables Using SQL*Plus (continued) SQL*Plus interpreter Checks command for syntax errors Submits command to database SQL commands are not case sensitive When creating database tables that contain foreign key references Must first create table in which foreign key is primary key Guide to Oracle 10g
SQL Command to Create the LOCATION Table Guide to Oracle 10g
Creating and Editing SQL Commands Using a Text Editor Good approach for entering commands: Type commands into text editor such as Notepad Copy commands, then paste into SQL*Plus Execute commands Script Text file that contains several related SQL commands Guide to Oracle 10g
Using Oracle Online Help Resources to Debug SQL Commands Syntax error SQL*Plus interpreter displays error information Line number within command that caused error Position of error within line Error code and description of error Guide to Oracle 10g
Using Oracle Online Help Resources To Debug SQL Commands (continued) Oracle 10g error codes have: 3-character prefix (such as ORA) 5-digits Causes of SQL command errors are not always readily apparent Need to retrieve more information about error Connect to Oracle Technology Network (OTN) Web Site and search for error code Guide to Oracle 10g
Using Oracle Online Help Resources To Debug SQL Commands (continued) Last resort debugging technique Create table multiple times Each time adding column declaration Repeat process until you find declaration causing error Drop table command To delete table syntax: DROP TABLE tablename Guide to Oracle 10g
Exiting SQL*Plus Type exit at SQL prompt Click File on menu bar, and then click Exit Click Close button on program window title bar Guide to Oracle 10g
Creating a Table with a Foreign Key Constraint Guide to Oracle 10g
Viewing Information about Tables DESCRIBE command View column names and data types of table Syntax DESCRIBE tablename Oracle 10g data dictionary Consists of tables that contain information about structure of database Guide to Oracle 10g
Viewing Information about Tables (continued) Oracle10g data dictionary (continued) System creates data dictionary in user schema named SYS Users do not directly manipulate data dictionary View Database object DBMS bases on actual database table Enables DBMS to present table data in different format based on needs of users Guide to Oracle 10g
Viewing Information about Tables (continued) Data dictionary views categories USER ALL DBA Syntax SELECT view_columnname1, view_columnname2 … FROM prefix_object; Guide to Oracle 10g
Database Objects with Data Dictionary Views Guide to Oracle 10g
Modifying and Deleting Database Tables Plan tables carefully to avoid having to change structure of database tables later Unrestricted action Some specifications of tables can always be modified Restricted action Table specifications that can be modified only in certain situations Guide to Oracle 10g
Unrestricted Actions when Modifying Database Tables Guide to Oracle 10g
Deleting and Renaming Existing Tables DROP TABLE command Delete table Syntax DROP TABLE tablename; DROP TABLE tablename CASCADE CONSTRAINTS; Guide to Oracle 10g
Deleting and Renaming Existing Tables (continued) RENAME TO command Syntax RENAME old_tablename TO new_tablename; Guide to Oracle 10g
Adding Columns to Existing Tables Add new column to table Syntax ALTER TABLE tablename ADD(columnname data_declaration constraints); Guide to Oracle 10g
Modifying Existing Column Data Definitions Modify existing column’s data declaration Syntax ALTER tablename MODIFY(columnname new_data_declaration); Guide to Oracle 10g
Deleting a Column Data stored in deleted column removed from database Syntax ALTER TABLE tablename DROP COLUMN columnname; Guide to Oracle 10g
Renaming a Column Syntax ALTER TABLE tablename RENAME COLUMN old_columnname TO new_columnname; Guide to Oracle 10g
Adding and Deleting Constraints Add constraint to existing table Syntax ALTER TABLE tablename ADD CONSTRAINT constraint_name constraint_definition; Remove existing constraint DROP CONSTRAINT constraint_name; Guide to Oracle 10g
Enabling and Disabling Constraints Constraint enabled DBMS enforces constraint when users attempt to add new data to database Disable existing constraint syntax ALTER TABLE tablename DISABLE CONSTRAINT constraint_name; Enable existing constraint syntax ENABLE CONSTRAINT constraint_name; Guide to Oracle 10g
Summary SQL commands include Data description language (DDL) commands Data manipulation language (DML) commands Each user account owns table and data objects in own area of database Called user schema When creating database table specify table name, column names, data type, and column sizes Guide to Oracle 10g
Summary (continued) Constraints restrict data values that users can enter into database columns When SQL commands have errors interpreter reports: Line number Position of character causing error Returns error code and description Use describe command to display table info Guide to Oracle 10g