Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2: Creating And Modifying Database Tables

Similar presentations


Presentation on theme: "Chapter 2: Creating And Modifying Database Tables"— Presentation transcript:

1 Chapter 2: Creating And Modifying Database Tables
Guide to Oracle 10g

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 Number Data Types (continued)
Floating-point number Contains variable number of decimal places Syntax columnname NUMBER Example s_gpa NUMBER Guide to Oracle 10g

15 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

16 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

17 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

18 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

19 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

20 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

21 Large Object (LOB) Data Types (continued)
Guide to Oracle 10g

22 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

23 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

24 Constraints (continued)
Constraint naming convention tablename_columnname_constraintid Guide to Oracle 10g

25 Common ConstraintID Abbreviations
Guide to Oracle 10g

26 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

27 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

28 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

29 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

30 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

31 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

32 SQL Command to Create the LOCATION Table
Guide to Oracle 10g

33 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

34 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

35 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

36 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

37 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

38 Creating a Table with a Foreign Key Constraint
Guide to Oracle 10g

39 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

40 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

41 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

42 Database Objects with Data Dictionary Views
Guide to Oracle 10g

43 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

44 Unrestricted Actions when Modifying Database Tables
Guide to Oracle 10g

45 Deleting and Renaming Existing Tables
DROP TABLE command Delete table Syntax DROP TABLE tablename; DROP TABLE tablename CASCADE CONSTRAINTS; Guide to Oracle 10g

46 Deleting and Renaming Existing Tables (continued)
RENAME TO command Syntax RENAME old_tablename TO new_tablename; Guide to Oracle 10g

47 Adding Columns to Existing Tables
Add new column to table Syntax ALTER TABLE tablename ADD(columnname data_declaration constraints); Guide to Oracle 10g

48 Modifying Existing Column Data Definitions
Modify existing column’s data declaration Syntax ALTER tablename MODIFY(columnname new_data_declaration); Guide to Oracle 10g

49 Deleting a Column Data stored in deleted column removed from database
Syntax ALTER TABLE tablename DROP COLUMN columnname; Guide to Oracle 10g

50 Renaming a Column Syntax ALTER TABLE tablename
RENAME COLUMN old_columnname TO new_columnname; Guide to Oracle 10g

51 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

52 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

53 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

54 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


Download ppt "Chapter 2: Creating And Modifying Database Tables"

Similar presentations


Ads by Google