0% found this document useful (0 votes)
31 views23 pages

ADB Ch02

No, there is no syntax error in the CREATE TABLE statements. The second statement correctly defines the primary key constraint both within the column definition and at the end of the table definition.

Uploaded by

ddddd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views23 pages

ADB Ch02

No, there is no syntax error in the CREATE TABLE statements. The second statement correctly defines the primary key constraint both within the column definition and at the end of the table definition.

Uploaded by

ddddd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

‫‪Creating Database Tables‬‬

‫إنشاء جداولق واعد ا لبيانات‬


Objectives

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
View information about your database tables
using Oracle 10g data dictionary views

2
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, SQL-2003, and SQL-2008
• Most recent versions
• Most vendors do not fully comply with SQL-2008
(but comply with SQL-92, SQL-99)
– Basic categories for SQL commands
• Data definition language (DDL)
• Data manipulation language (DML)
3
CH2:42-58
Personal DBMS
With personal DBMS …
– You are usually the only user
– You start the database application
– You create a new database
– The DBMS saves the database file in your
workstation’s file system
– You create database objects (tables, etc.)
Examples:
– MS Access
– Oracle Personal edition. But this DBMS derives
most of its features from the Oracle C/S version
4
CH2:42-58
Oracle 10g Enterprise edition
A Client/server DBMS
User account
– Created for each user
– Identified using unique username and password
User schema
– Area of database belonging to user
Database objects
– Example: tables, forms, reports, …
– Also called schema objects
– Objects created by user reside in their user schema
5
CH2:42-58
Defining Oracle Database Tables
Tables
– Primary data objects in relational database
– When you create a new table, you must specify…
• Table name ● Each field (i.e. column) name

• Each field data type/size ● You may specify constraints

Constraints
– Restrictions on data values that column can store
Oracle naming standard
– Rules established by Oracle corp. for naming objects
– One to 30 characters long
– Contain letters, numbers, and special symbols $, _, and #
– Begin with character
– Example: S_ID, f_id, PRICE, PRICE$, Not #ITEM.
6
CH2:42-58
Defining Oracle Database Tables (cont.)
CREATE TABLE SQL syntax
CREATE TABLE tablename
(columnname1 data_type,
columnname2 data_type, …);
Example:
CREATE TABLE student
(s_id CHAR(5),
s_first VARCHAR2(20));
Basic data types
• Character ● Number ● Date/time ● Large object
7
CH2:42-58
Character Data Types
VARCHAR2
– Variable-length character data (up to 4000 characters)
– Syntax: columnname VARCHAR2(maximum_size)
– If user enters data value less than maximum_size, DBMS
only stores actual character values
CHAR
– Fixed-length character data (default = 2000)
– Syntax: columnname CHAR(maximum_size)
– If user enters data value less than maximum_size, DBMS
adds trailing blank spaces to the end of entry
Oracle stores CHAR and VARCHAR2 data using the ASCII
coding
Q: s_last CHAR(20) was used to define the data type for s_last in the Student table. How many
characters will Oracle save to the disk if the user enters illia as the student’s last name? 8
Character Data Types (continued)
Unicode coding
– 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

9
CH2:42-58
Number Data Types
NUMBER
– Used for all numeric data # of digits both to left and
right of decimal point
– Syntax
• columnname NUMBER [([precision,]
[scale])] # of digits on the right side of decimal point

Example:
– s_balance NUMBER (5,2)
– s_gpa NUMBER (3,2)

10
CH2:42-58
Number Data subtypes
Integer number syntax Just the precision. No scale specified

– columnname NUMBER(precision)
Fixed-point number
– Contains specific number of decimal places
– Column declaration specifies both precision and scale
– Example: price NUMBER(5,2)
Floating-point number
– Contains variable number of decimal places
– Decimal point may appear anywhere (.005, 2.34, etc.)
– Syntax: columnname NUMBER No precision, no scale
– Example: s_gpa NUMBER
11
CH2:42-58
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

12
CH2:42-58
Date And Time Data Types (continued)
DATE
– Stores dates from Dec 31, 4712 BC to Dec 31, AD 4712
– Default date format: DD-MON-YY
– Default time format: HH:MI:SS AM
– Syntax: columnname DATE
TIMESTAMP
– Stores date values similar to DATE data type
– Also stores fractional seconds If omitted, default is 6 decimal place
– Syntax: columnname TIMESTAMP
(fractional_seconds_precision)
– Example: shipment_date TIMESTAMP(2) 13
Date And Time Data Types (continued)
INTERVAL YEAR TO MONTH
– Stores time interval expressed in years and months
using the following syntax:
+|– elapsed_years-elapsed_months
– Example: +02-11 specifies 2 years and 11 months
– Example:
time_enrolled INTERVAL YEAR TO MONTH

14
CH2:42-58
Date And Time Data Types (continued)
INTERVAL DAY TO SECOND
– Stores time interval expressed in days, hours, minutes,
and seconds using the following syntax:
Max allowed number of digits used to
express the elapsed days

Columnname INTERVAL DAY [(leading_precision)]


TO SECOND [(fractional_seconds_precision)]
Max allowed number of digits used to
express the elapsed seconds

15
CH2:42-58
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

16
Constraints
Table constraint
– Restricts data value with respect to all other values in
table like primary key must be unique, not NULL.
Column constraint
– Limits value that can be placed in specific column
– Irrespective of values that exist in other table rows
Types of constraints:
– Integrity constraints
– Value constraints

17
CH2:42-58
Constraints (cont.)

Constraint naming convention


– tablename_columnname_constraintid
Constraint definitions should be placed either:
– At end of CREATE TABLE command after table
columns declared
– Within each column definition
Example: CREATE TABLE location
(loc_id NUMBER(6),
Room VARCHAR2(6),
CREATE TABLE location CONSTRAINT location_loc_id_pk PRIMARY KEY (loc_id);
(loc_id NUMBER(6)
CONSTRAINT location_loc_id_pk PRIMARY KEY),
Room VARCHAR2(6);

Q: Is there any syntax error (or something missing in the above CREATE TABLE statements?
18
Integrity Constraints
Primary key
– Syntax (within table definition)
• CONSTRAINT constraint_name PRIMARY
KEY
CREATE TABLE location
(loc_id NUMBER(6)
CONSTRAINT location_loc_id_pk PRIMARY KEY),
Room VARCHAR2(6));

– Syntax (at end of table definition)


• CONSTRAINT constraint_name PRIMARY
KEY (columnname)
CREATE TABLE location
(loc_id NUMBER(6),
Room VARCHAR2(6),
CONSTRAINT location_loc_id_pk PRIMARY KEY (loc_id));
19
CH2:42-58
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)
– Example of foreign key defined in the Faculty table:
CONSTRAINT faculty_loc_id_fk
FOREIGN KEY (loc_id)
REFERENCES location (loc_id)
20
CH2:42-58
Integrity Constraints (continued)
Foreign key (continued)
– Syntax (placed within table definition)
CONSTRAINT constraint_name
REFERENCES primary_key_tablename
(primary_key_columnname)
– Example:
loc_id NUMBER(6) CONSTRAINT faculty_loc_id_fk
REFERENCES location (loc_id)

21
CH2:42-58
Integrity Constraints (continued)
Composite key
– Syntax
CONSTRAINT constraint_name
PRIMARY KEY (columnname1, columnname2 …)
– Example:

CREATE TABLE enrollment


(s_id NUMBER(5) CONSTRAINT enrollment_s_id_fk REFERENCES student(s_id),
c_sec_id NUMBER(8) CONSTRAINT enrollment_c_sec_id_fk REFERENCES
course_section(c_sec__id),
CONSTRAINT enrollment_s_id_c_sec_id_pk PRIMARY KEY (s_id, c_sec_id));

22
CH2:42-58
Value Constraints
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

CONSTRAINT student_s_class_cc
CHECK ((s_class = ‘FR’) OR (s_class = ‘SO’)
OR (s_class = ‘JR’) OR (s_class = ‘SR’))
s_state CHAR(2) DEFAULT ‘FL’

CONSTRAINT course_credits_cc
CHECK ((credits > 0) AND (credits < 12))

S_last VARCHAR2(30)
CONSTRAINT student_s_last_nn NOT NULL 23

You might also like