Download presentation
Presentation is loading. Please wait.
Published byBriana Ramsey Modified over 8 years ago
1
ITBIS373 Database Development Lecture 2 - Chapter 2 Creating And Modifying Database Tables
2
Guide to Oracle 10g2 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 user schemas Define and create Oracle database tables Debug Oracle SQL commands Use online help resources available through the Oracle Technology Network (OTN)
3
Guide to Oracle 10g3 Objectives (continued) View information about your database tables using Oracle 10g data dictionary views Modify and delete database tables using SQL*Plus
4
Guide to Oracle 10g4 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
5
Guide to Oracle 10g5 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
6
Guide to Oracle 10g6 Oracle 10g User Accounts User account 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
7
Guide to Oracle 10g7 Defining Oracle 10g Database Tables 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
8
Guide to Oracle 10g8 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, …)
9
Guide to Oracle 10g9 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
10
Guide to Oracle 10g10 Character Data Types VARCHAR2 Variable-length character data Syntax columnname VARCHAR2(maximum_size) CHAR Fixed-length character data Syntax columnname CHAR[(maximum_size)]
11
Guide to Oracle 10g11 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
12
Guide to Oracle 10g12 Number Data Types NUMBER Used for all numeric data Syntax columnname NUMBER [([precision,] [scale])] Precision Total number of digits both to left and right of decimal point
13
Guide to Oracle 10g13 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)
14
Guide to Oracle 10g14 Number Data Types (continued) Floating-point number Contains variable number of decimal places Syntax columnname NUMBER Example s_gpa NUMBER
15
Guide to Oracle 10g15 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
16
Guide to Oracle 10g16 Date And Time Data Types (continued) DATE 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
17
Guide to Oracle 10g17 Date And Time Data Types (continued) TIMESTAMP Stores date values similar to DATE data type, except it also stores fractional seconds in addition to the century, year, month, day, hour, minute, and second. Syntax columnname TIMESTAMP (fractional_seconds_precision) Example 04-SEP-06 09.26.01.123975 DD-Mon-YY HH.MM.SS.FFFFFF If you omit the fractional_seconds_precision specification, the default value is six decimal places.
18
Guide to Oracle 10g18 Date And Time Data Types (continued) INTERVAL YEAR TO MONTH Stores time interval expressed in years and months Syntax +|– elapsed_years-elapsed_months Ex:+02-11 : specifies a positive time interval of 2 years and 11 months. You use the following command to declare an INTERVAL YEAR TO MONTH data column: INTERVAL YEAR [(year_precsion)] TO MONTH If you omit the Year_precsion value in the column declaration, the default value is 6, which enables the column to display a maximum interval 0f 999,999 years.)
19
Guide to Oracle 10g19 CREATE TABLE student (s_id VARCHAR2(6), s_last VARCHAR2(30), s_first VARCHAR2(30), s_mi CHAR(1), s_address VARCHAR2(25), s_city VARCHAR2(20), s_state CHAR(2), s_zip VARCHAR2(10), s_phone VARCHAR2(10), s_class CHAR(2), s_dob DATE, s_pin NUMBER(4), f_id NUMBER(6), time_enrolled INTERVAL YEAR TO MONTH, CONSTRAINT student_s_id_pk PRIMARY KEY (s_id), CONSTRAINT student_f_id_fk FOREIGN KEY (f_id) REFERENCES faculty(f_id)); NSERT INTO student VALUES ('JO100', 'Jones', 'Tammy', 'R', '1817 Eagleridge Circle', 'Tallahassee', 'FL', '32811', '7155559876', 'SR', TO_DATE('07/14/1985', 'MM/DD/YYYY'), 8891, 1, TO_YMINTERVAL('3-2'));
20
Guide to Oracle 10g20 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_se conds
21
Guide to Oracle 10g21 The following data value expresses a negative time interval of 4 days, 3 hours, 20 minutes and 32 seconds: -04 03:20:32.00 The basic syntax to define an INTERVAL DAY TO SECOND data column is as follows: Columnname INTERVAL DAY [( leading_precision)] TO SECOND{(fractional_seconds_precision)] The default values for these expressions are 2 and 6 respectively.
22
Guide to Oracle 10g22 CREATE TABLE COURSE_SECTION (c_sec_id NUMBER(6), course_no VARCHAR2(7) CONSTRAINT course_section_courseid_nn NOT NULL, term_id NUMBER(6) CONSTRAINT course_section_termid_nn NOT NULL, sec_num NUMBER(2) CONSTRAINT course_section_secnum_nn NOT NULL, f_id NUMBER(6), c_sec_day VARCHAR2(10), c_sec_time DATE, c_sec_duration INTERVAL DAY TO SECOND, loc_id NUMBER(6), max_enrl NUMBER(4) CONSTRAINT course_section_maxenrl_nn NOT NULL, CONSTRAINT course_section_csec_id_pk PRIMARY KEY (c_sec_id), CONSTRAINT course_section_cid_fk FOREIGN KEY (course_no) REFERENCES course(course_no), CONSTRAINT course_section_loc_id_fk FOREIGN KEY (loc_id) REFERENCES location(loc_id), CONSTRAINT course_section_termid_fk FOREIGN KEY (term_id) REFERENCES term(term_id), CONSTRAINT course_section_fid_fk FOREIGN KEY (f_id) REFERENCES faculty(f_id));
23
Guide to Oracle 10g23 --- inserting records into COURSE_SECTION INSERT INTO course_section VALUES (1, 'MIS 101', 4, 1, 2, 'MWF', TO_DATE('10:00 AM', 'HH:MI AM'), TO_DSINTERVAL('0 00:50:00.00'), 1, 140); INSERT INTO course_section VALUES (2, 'MIS 101', 4, 2, 3, 'TR', TO_DATE('09:30 AM', 'HH:MI AM'), TO_DSINTERVAL('0 01:15:00.00'), 7, 35); INSERT INTO course_section VALUES (3, 'MIS 101', 4, 3, 3, 'MWF', TO_DATE('08:00 AM', 'HH:MI AM'), TO_DSINTERVAL('0 00:50:00.00'), 2, 35); INSERT INTO course_section VALUES (4, 'MIS 301', 4, 1, 4, 'TR', TO_DATE('11:00 AM', 'HH:MI AM'), TO_DSINTERVAL('0 01:15:00.00'), 6, 35);
24
Guide to Oracle 10g24 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
25
Guide to Oracle 10g25 Large Object (LOB) Data Types (continued)
26
Guide to Oracle 10g26 CREATE TABLE faculty (f_id NUMBER(6), f_last VARCHAR2(30), f_first VARCHAR2(30), f_mi CHAR(1), loc_id NUMBER(5), f_phone VARCHAR2(10), f_rank VARCHAR2(9), f_super NUMBER(6), f_pin NUMBER(4), f_image BLOB, or f_image Bfile CONSTRAINT faculty_f_id_pk PRIMARY KEY(f_id), CONSTRAINT faculty_loc_id_fk FOREIGN KEY (loc_id) REFERENCES location(loc_id)); INSERT INTO faculty VALUES (1, 'Marx', 'Teresa', 'J', 9, '4075921695', 'Associate', 4, 6338, EMPTY_BLOB()); INSERT INTO faculty VALUES (2, 'Zhulin', 'Mark', 'M', 10, '4073875682', 'Full', NULL, 1121, brown.jpg ); INSERT INTO faculty VALUES (3, 'Langley', 'Colin', 'A', 12, '4075928719', 'Assistant', 4, 9871, sealy.gpg) ; INSERT INTO faculty VALUES (4, 'Brown', 'Jonnel', 'D', 11, '4078101155', 'Full', NULL, 8297, EMPTY_BLOB()) ; INSERT INTO faculty VALUES (5, 'Sealy', 'James', 'L', 13, '4079817153', 'Associate', 1, 6089, EMPTY_BLOB()) ;
27
Guide to Oracle 10g27 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. An example of a table constraint is a primary key constraint, which specifies that a column value must be unique and cannot appear in this column in more than one table row.
28
Guide to Oracle 10g28 Constraints (continued) Column constraint Limits value that can be placed in specific column. Example of column constraints are value constraints, which specify that a certain value or set of values must be used, and NOT NULL constraint s which specify that a value cannot be NULL. Constraint definitions should be placed either: At end of CREATE TABLE command after table columns declared Within each column definition
29
Guide to Oracle 10g29 Constraints (continued) Constraint naming convention tablename_columnname_constraintID
30
Guide to Oracle 10g30 Common ConstraintID Abbreviations
31
Guide to Oracle 10g31 Integrity Constraints Primary key Syntax (within a column declaration) CONSTRAINT constraint_name PRIMARY KEY Syntax (at end of table definition) CONSTRAINT constraint_name PRIMARY KEY (columnname)
32
Guide to Oracle 10g32 Integrity Constraints (continued) Foreign key is a column constraint that the value a 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)
33
Guide to Oracle 10g33 CREATE TABLE COURSE_SECTION (c_sec_id NUMBER(6), course_no VARCHAR2(7) CONSTRAINT course_section_courseid_nn NOT NULL, term_id NUMBER(6) CONSTRAINT course_section_termid_nn NOT NULL, sec_num NUMBER(2) CONSTRAINT course_section_secnum_nn NOT NULL, f_id NUMBER(6), c_sec_day VARCHAR2(10), c_sec_time DATE, c_sec_duration INTERVAL DAY TO SECOND, loc_id NUMBER(6), max_enrl NUMBER(4) CONSTRAINT course_section_maxenrl_nn NOT NULL, CONSTRAINT course_section_c _ sec_id_pk PRIMARY KEY (c_sec_id), CONSTRAINT course_section_course_no_fk FOREIGN KEY (course_no) REFERENCES course(course_no), CONSTRAINT course_section_loc_id_fk FOREIGN KEY (loc_id) REFERENCES location(loc_id), CONSTRAINT course_section_termid_fk FOREIGN KEY (term_id) REFERENCES term(term_id), CONSTRAINT course_section_fid_fk FOREIGN KEY (f_id) REFERENCES faculty(f_id));
34
Guide to Oracle 10g34 Integrity Constraints (continued) Foreign key (continued) Syntax (placed within table definition) CONSTRAINT constraint_name REFERENCES primary_key_tablename (primary_key_columnname) Composite key Syntax CONSTRAINT constraint_name PRIMARY KEY (columnname1, columnname2 …)
35
Guide to Oracle 10g35 EX OF Composite KEY CREATE TABLE ENROLLMENT (s_id VARCHAR2(6), c_sec_id NUMBER(6), grade CHAR(1), CONSTRAINT enrollment_s_id_c_sec_id_pk PRIMARY KEY (s_id, c_sec_id), CONSTRAINT enrollment_sid_fk FOREIGN KEY (s_id) REFERENCES student(s_id), CONSTRAINT enrollment_csecid_fk FOREIGN KEY (c_sec_id) REFERENCES course_section (c_sec_id));
36
Guide to Oracle 10g36 Value Constraints Value constraints: Commonly used value constraints include: CHECK conditions Ex: CONSTRAINT course_credits_cc CHECK (credits >0) AND (credits < 12)) NOT NULL constraint Ex: S_last VARCHAR2(30) CONSTRAINT student_s_last_nn NOT NULL
37
Guide to Oracle 10g37 DEFAULT constraint Ex: s_state CHAR(2) DEFAULT ‘BH’ UNIQUE constraint: it basically the same as primary key constraint, except NULL value are allowed in the column. CONSTRAINT term_term_desc_uk UNIQUE (term_desc )
38
Guide to Oracle 10g38 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
39
Guide to Oracle 10g39 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
40
Guide to Oracle 10g40 SQL Command to Create the LOCATION Table
41
Guide to Oracle 10g41 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
42
Guide to Oracle 10g42 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
43
Guide to Oracle 10g43 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
44
Guide to Oracle 10g44 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 from dictionary syntax: DROP TABLE tablename
45
Guide to Oracle 10g45
46
Guide to Oracle 10g46 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
47
Guide to Oracle 10g47 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 - When the DBA creates a new database, the system creates data dictionary in user schema named SYS The Oracle 10g DBMS automatically updates the data dictionary tables as users create, update, and delete database objects.
48
Guide to Oracle 10g48 Viewing Information about Tables (continued) View Database object DBMS bases on actual database table Enables DBMS to present table data in different format based on needs of users
49
Guide to Oracle 10g49 Viewing Information about Tables (continued) Data dictionary views are divided into three general categories USER- shows the objects in the current user’s name. ALL- shows both the objects in the current user’s scheme and the objects that the user has privileges to manipulate. DBA- Allows users who are DBAs to view information about all database objects. Syntax SELECT view_columnname1, view_columnname2 … FROM prefix_object;
50
Guide to Oracle 10g50 In this syntax, View_Columnname1, View_Column2 reference the names of the columns in the view that you wish to retrieve. Prefix: specfies the view category, and can have the value USER,DBA, or ALL. Object is the type of database object you are examining, such as TABLES, or CONSTRAINTS.
51
Guide to Oracle 10g51
52
Guide to Oracle 10g52 Database Objects with Data Dictionary Views
53
Guide to Oracle 10g53 You can retrieve information from these views using commands similar to the ones you used to retrieve information about your database table, the command to retrieve the names of all of your database objects is: SELECT object_name FROM user_objects ; All the data dictionary views have different columns. To determine the names of the columns in specific database views, you use the DESCRIBE command along with the view name. Fig 2-9 shows a description of the column names in the USER_CONSTRAINTS data dictionary views.
54
Guide to Oracle 10g54
55
Guide to Oracle 10g55 To View your table Constraints
56
Guide to Oracle 10g56 To List the FACULTY table constraints:
57
Guide to Oracle 10g57 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
58
Guide to Oracle 10g58 Unrestricted Actions when Modifying Database Tables
59
Guide to Oracle 10g59
60
Guide to Oracle 10g60 Deleting and Renaming Existing Tables DROP TABLE command Delete table Syntax DROP TABLE tablename; DROP TABLE tablename CASCADE CONSTRAINTS;
61
Guide to Oracle 10g61 To drop a table that contains columns that other tables reference as foreign keys, you have two options. One option is to first drop all the tables that contain the foreign key references. For example, to delete the LOCATION table, you could first delete the FACULTY table, and then you could delete the LOCATION table. The second option is first to delete all of the foreign key constraints that reference a table, you use the CASCADE CONSTRAINTS option in the DROP TABLE command, which has the following syntax: DROP TABLE tablename CASCADE CONSTRAINTS;
62
Guide to Oracle 10g62
63
Guide to Oracle 10g63 Deleting and Renaming Existing Tables (continued) RENAME TO command Syntax RENAME old_tablename TO new_tablename; Ex: RENAME faculty TO nw_faculty;
64
Guide to Oracle 10g64 Adding Columns to Existing Tables Add new column to table Syntax ALTER TABLE tablename ADD(columnname data_declaration constraints); Ex: ALTER TABLE faculty ADD (start_date DATE);
65
Guide to Oracle 10g65 Modifying Existing Column Data Definitions Modify existing column’s data declaration Syntax ALTER TABLE tablename MODIFY(columnname new_data_declaration); EX: ALTER TABLE faculty MODIFY (f_rank CHAR(4));
66
Guide to Oracle 10g66 Deleting a Column Data stored in deleted column removed from database Syntax ALTER TABLE tablename DROP COLUMN columnname;
67
Guide to Oracle 10g67 Renaming a Column Syntax ALTER TABLE tablename RENAME COLUMN old_columnname TO new_columnname; Ex: ALTER TABLE faculty RENAME COLUMN f_rank TO faculty_rank ;
68
Guide to Oracle 10g68 Adding and Deleting Constraints Add constraint to existing table Syntax ALTER TABLE tablename ADD CONSTRAINT constraint_name constraint_definition; EX ALTER TABLE faculty ADD CONSTRAINT faculty_f_pin_uk UNIQUE(f_pin); Remove existing constraint Syntax ALTER TABLE tablename DROP CONSTRAINT constraint_name;
69
Guide to Oracle 10g69 Remove existing constraint Syntax ALTER TABLE tablename DROP CONSTRAINT constraint_name; EX ALTER TABLE faculty DROP CONSTRAINT faculty_f_pin_uk;
70
Guide to Oracle 10g70 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 ALTER TABLE tablename ENABLE CONSTRAINT constraint_name;
71
Guide to Oracle 10g71 EX : ALTER TABLE faculty DISABLE CONSTRAINT faculty_loc_id_fk; ALTER TABLE faculty ENABLE CONSTRAINT faculty_loc_id_fk;
72
Guide to Oracle 10g72 Summary SQL commands include Data defintion 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
73
Guide to Oracle 10g73 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.