0% found this document useful (0 votes)
38 views8 pages

ADBMS - Chapter 02

This document discusses creating and working with database tables in Oracle 10g. It begins by introducing SQL and its use for creating, modifying, and dropping database tables. It then covers Oracle user schemas and defines what a database table is. Examples are provided of how to create tables using SQL commands and the CREATE TABLE syntax. The document also discusses data types that can be used as fields in tables such as character, number, date/time and large object types. Finally, it covers table constraints that restrict field values, such as primary keys, and provides an example of creating a table with a primary key constraint.

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views8 pages

ADBMS - Chapter 02

This document discusses creating and working with database tables in Oracle 10g. It begins by introducing SQL and its use for creating, modifying, and dropping database tables. It then covers Oracle user schemas and defines what a database table is. Examples are provided of how to create tables using SQL commands and the CREATE TABLE syntax. The document also discusses data types that can be used as fields in tables such as character, number, date/time and large object types. Finally, it covers table constraints that restrict field values, such as primary keys, and provides an example of creating a table with a primary key constraint.

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 PDF, TXT or read online on Scribd
You are on page 1/ 8

Advanced Database Management Systems

(DBMS)
Chapter 2
Creating Database Tables ‫إﻧﺸﺎء ﻗﻮاﻋﺪ اﻟﺒﯿﺎﻧﺎت‬
Objectives
q Use structured query language (SQL) commands to create, modify, and drop database
tables
q Explain Oracle 10g user schemas
q Define Oracle 10g database tables
q Create database tables using SQL*Plus
q View information about your database tables using Oracle 10g data dictionary views

Introduction to SQL
q 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)
Personal DBMS
q 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.)
q Examples:
– MS Access
– Oracle Personal edition. But this DBMS derives most of its features from the
Oracle C/S version

Oracle 10g Enterprise edition


q A Client/server DBMS
q User account
– Created for each user
– Identified using unique username and password
q User schema
– Area of database belonging to user
q Database objects
– Example: tables, forms, reports, …
– Also called schema objects
– Objects created by user reside in their user schema
Defining Oracle Database Tables
q 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
q Constraints
– Restrictions on data values that column can store
q 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.

q CREATE TABLE SQL syntax


CREATE TABLE tablename (
columnname1 data_type,
columnname2 data_type, …
);
q Example:
CREATE TABLE student (
s_id INT(5),
s_first VARCHAR2(20)
);

Please create the following table :‫ﯾﺮﺟﻰ إﻧﺸﺎء اﻟﺠﺪول اﻟﺘﺎﻟﻲ‬

Table Name: People


Fields: Person_ID(14), First_Name(10), Last_Name(10) , Address (30), City(10), Mobile(10)

Solution:

CREATE TABLE PEOPLE (


PERSON_ID INT(14),
FIRST_NAME VARCHAR2(10),
LAST_NAME VARCHAR2(10),
ADDRESS VARCHAR2(30),
CITY VARCHAR2(10),
MOBILE NUMBER(10));

‫ھﺬا اﻟﺠﺪول اﻟﻨﮭﺎﺋﻲ ﻟﻠﻜﻮد اﻟﺴﺎﺑﻖ‬


PERSON_ID FIRST_NAME LAST_NAME ADDRESS CITY MOBILE
q Basic data types
SQL ‫أﻧﻮاع اﻟﺒﯿﺎﻧﺎت اﻷﺳﺎﺳﯿﺔ ﻟﺤﻘﻮل اﻟﺠﺪاول اﻟﻤﺨﺘﻠﻔﺔ ﻓﻲ ﻗﺎﻋﺪة اﻟﺒﯿﺎﻧﺎت‬
• Character ● Number ● Date/time ● Large object

Character Data Types


q 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
q 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
q 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?
q Unicode coding
– Standardized technique that provides way to encode data in diverse languages
q NVARCHAR2
– Counterpart of VARCHAR2
– Uses Unicode coding
q NCHAR
– Counterpart of CHAR
– Uses Unicode encoding
Number Data Types
q NUMBER
– Used for all numeric data
– Syntax
• columnname NUMBER [([precision,] [scale])]
.‫ﯾﺴﺘﺨﺪم ﻓﻲ ﺣﺎﻟﺔ وﺟﻮد أرﻗﺎم ﻣﻊ ﻛﺴﻮر )ﺧﺎﻧﺎت ﻋﺸﺮﯾﺔ( ﻣﺜﻞ اﻟﻤﻌﺪل‬
precision: # of digits both to left and right of decimal point
scale: # of digits on the right side of decimal point

q Example:
– s_balance NUMBER (5,2) e.g. 100.10
– s_gpa NUMBER (3,2) e.g. 1.10

Number Data subtypes


q Integer number syntax
– columnname NUMBER(precision)
q Integer syntax
– columnname INT (precision)
‫ﯾﻔﻀﻞ اﺳﺘﺨﺪاﻣﮫ ﻟﻠﺤﻘﻞ اﻷﺳﺎﺳﻲ اﻟﻤﻌﺮف ﻷﻧﮫ رﻗﻢ ﺻﺤﯿﺢ‬
q Fixed-point number
– Contains specific number of decimal places
– Column declaration specifies both precision and scale
– Example: price NUMBER(5,2)
q Floating-point number
– Contains variable number of decimal places
– Decimal point may appear anywhere (.005, 2.34, etc.)
– Syntax: columnname NUMBER
– Example: s_gpa NUMBER

Date And Time Data Types


q Datetime data subtypes
– Store actual date and time values
– DATE
– TIMESTAMP
q Interval data subtypes
– Store elapsed time interval between two datetime values
– INTERVAL YEAR TO MONTH
– INTERVAL DAY TO SECOND
q 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
q TIMESTAMP
– Stores date values similar to DATE data type
– Also stores fractional seconds
– Syntax: columnname TIMESTAMP
(fractional_seconds_precision)
– Example: shipment_date TIMESTAMP(2)
q 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
q INTERVAL DAY TO SECOND
– Stores time interval expressed in days, hours, minutes, and seconds using the
following syntax:
Columnname INTERVAL DAY [(leading_precision)] TO SECOND
[(fractional_seconds_precision)]
Max allowed number of digits used
Max allowed number of digits used to express the elapsed days
to express the elapsed seconds

Large Object (LOB) Data Types


q Store binary data such as:
– Digitized sounds or images
– References to binary files from word processor or spreadsheet
q General syntax
– columnname Lob_data_type
Constraints
q Table constraint
– Restricts data value with respect to all other values in table like primary key
must be unique, not NULL.
q Column constraint
– Limits value that can be placed in specific column
– Irrespective of values that exist in other table rows
q Types of constraints:
– Integrity constraints
– Value constraints
q Constraint naming convention
– tablename_columnname_constraintid
q Constraint definitions should be placed either:
– At end of CREATE TABLE command after table columns declared
– Within each column definition CREATE TABLE location
q Example: (loc_id NUMBER(6)
CREATE TABLE location CONSTRAINT location_loc_id_pk PRIMARY KEY),
(loc_id NUMBER(6), Room VARCHAR2(6);
Room VARCHAR2(6),
CONSTRAINT location_loc_id_pk PRIMARY KEY (loc_id);

CREATE TABLE location(


loc_id NUMBER(6)
CONSTRAINT location_loc_id_pk PRIMARY KEY),
Integrity Constraints Room VARCHAR2(6)
q Primary key );
– Syntax (within table definition)
• CONSTRAINT constraint_name PRIMARY KEY
– 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));
Example:
Please create the following table :‫ﯾﺮﺟﻰ إﻧﺸﺎء اﻟﺠﺪول اﻟﺘﺎﻟﻲ‬

Table Name: Orders


Fields: Order ID (ORD_ID PK), Order Description (ORD_DES 15)

Solution:
‫ھﺬا اﻟﺤﻞ ﺑﻮﺿﻊ اﻟﻜﻮد ﻟﻠﺤﻘﻞ اﻷﺳﺎﺳﻲ ﻓﻲ آﺧﺮ ﺗﻌﺮﯾﻒ اﻟﺠﺪول‬
CREATE TABLE ORDERS(
ORD_ID INT,
ORD_DES VARCHAR2(15),
CONSTRAINT ORDERS_ORD_ID_PK PRIMARY KEY (ORD_ID));

‫ھﺬا اﻟﺤﻞ ﺑﻮﺿﻊ اﻟﻜﻮد ﻟﻠﺤﻘﻞ اﻷﺳﺎﺳﻲ ﻣﻊ ﺗﻌﺮﯾﻒ اﻟﺤﻘﻞ‬


CREATE TABLE ORDERS(
ORD_ID INT
CONSTRAINT ORDERS_ORD_ID_PK PRIMARY KEY,
ORD_DES VARCHAR2(15));

q 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) Syntax (placed within table definition)
CONSTRAINT constraint_name CONSTRAINT constraint_name
FOREIGN KEY (columnname) REFERENCES primary_key_tablename
REFERENCES primary_key_tablename (primary_key_columnname)
(primary_key_columnname)

Example of foreign key defined in the Example:


Faculty table: loc_id NUMBER(6) CONSTRAINT
CONSTRAINT faculty_loc_id_fk faculty_loc_id_fk
FOREIGN KEY (loc_id) REFERENCES location (loc_id)
REFERENCES location (loc_id)

You have the following table (HRZUJ)


‫ﻟﺪﯾﻚ ھﺬا اﻟﺠﺪول واﻟﺬي ﯾﺤﺘﻮي ﻋﻠﻰ أﺳﻤﺎء اﻟﻤﻮظﻔﯿﻦ ﻓﻲ اﻟﺠﺎﻣﻌﺔ‬
CREATE TABLE HRZUJ(
EMPID INT CONSTRAINT HRZUJ_PK PRIMARY KEY, ç
EMPNAME VARCHAR2(20),
EMPMOB NUMBER (10)
);
Employee ID Employee Name Employee Mobile
(EMPID) (EMPNAME) (EMPMOB)
PK 20 digits 10 digits
100 MOHAMMED 0791000100

And you have to create another table and link the (EMPID) with it.
‫وﯾﺘﻮﺟﺐ ﻋﻠﯿﻚ ﻋﻤﻞ ﺟﺪول آﺧﺮ ﻷﻋﻀﺎء اﻟﮭﯿﺌﺔ اﻟﺘﺪرﯾﺴﯿﺔ ﻓﻲ اﻟﻜﻠﯿﺔ ﻣﻊ رﺑﻂ اﻟﻤﻔﺘﺎح اﻟﺮﺋﯿﺴﻲ ﻓﻲ ﺟﺪول اﻟﻤﻮظﻔﯿﻦ ﻣﻊ‬
:‫أﺳﻤﺎء اﻟﮭﯿﺌﺔ اﻟﺘﺪرﯾﺴﯿﺔ ﻓﻲ اﻟﺠﺪول اﻟﺠﺪﯾﺪ أدﻧﺎه‬
Professor ID Professor Name Employee ID
(PROFID) (PROFNAME) (EMPID)
PK 20 digits FK
500 MOHAMMED 100

The solution will be as follow: ‫اﻟﺤﻞ ﻛﺘﺎﻟﻲ‬

CREATE TABLE PROFS(


PROFID INT
CONSTRAINT PROFS_PK PRIMARY KEY,
PROFNAME VARCHAR2(20),
EMPID INT CONSTRAINT PROFS_EMPID_FK ç
REFERENCES HRZUJ (EMPID));

q 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));

Example:
Please create the following table
Table Name: CAR_NUM
Fields:
Car’s plate number COMPOSIT KEY (‫( أرﻗﺎم اﻟﺴﯿﺎرات )ﻧﻤﺮة اﻟﺴﯿﺎرات‬22-40040)
Car plate number contains main number of 2 digits (‫ﺗﺤﺘﻮي ﻧﻤﺮة اﻟﺴﯿﺎرة ﻋﻠﻰ رﻗﻢ رﺋﯿﺴﻲ )اﻟﺘﺮﻣﯿﺰ‬
And a secondary number of 5 digits ‫وﻋﻠﻰ رﻗﻢ ﻓﺮﻋﻲ ﯾﺘﻜﻮن ﻣﻦ‬
‫ﯾﻤﻜﻦ أن ﯾﺘﻜﺮر اﻟﺘﺮﻣﯿﺰ ﻣﻊ رﻗﻢ ﻓﺮﻋﻲ آﺧﺮ أو أن ﯾﺘﻜﺮر اﻟﺮﻗﻢ اﻟﻔﺮﻋﻲ ﻣﻊ ﺗﺮﻣﯿﺰ آﺧﺮ‬
‫إﻧﻤﺎ ﻻ ﯾﻤﻜﻦ أن ﯾﺘﻜﺮر اﻟﺘﺮﻣﯿﺰ ﻣﻊ اﻟﺮﻗﻢ اﻟﻔﺮﻋﻲ ﻷي ﺳﯿﺎرة أﺧﺮى‬
:‫( واﻟﺤﻞ ﻛﺎﻟﺘﺎﻟﻲ‬Composite Key) ‫وﻟﻌﻤﻞ ھﺬا اﻟﺠﺪول ﯾﺠﺐ ﻋﻤﻞ ﻣﻔﺘﺎح ﻣﺮﻛﺐ‬

CREATE TABLE CAR_NUM(


P_ID INT, (‫اﻟﺮﻗﻢ اﻟﺮﺋﯿﺴﻲ )اﻟﺘﺮﻣﯿﺰ‬
S_ID INT, ‫اﻟﺮﻗﻢ اﻟﻔﺮﻋﻲ‬
CAR_T VARCHAR2(20), ‫ﺣﻘﻞ آﺧﺮ ﯾﺤﺘﻮي ﻋﻠﻰ ﻧﻮع اﻟﺴﯿﺎرة‬
CONSTRAINT CAR_NUM_PK PRIMARY KEY (P_ID,S_ID));

Primary number Secondry number Car Type


(P_ID) (S_ID) (CAR_T)
22 40040 HONDA
Value Constraints ‫ﻣﺤﺪدات ﻗﯿﻢ اﻟﺒﯿﺎﻧﺎت اﻟﻤﺪﺧﻠﺔ‬
q Value constraints
– Column-level constraints
– Restrict data values that users can enter
– Commonly used value constraints ‫اﻟﻤﺤﺪدات ﺷﺎﺋﻌﺔ اﻻﺳﺘﺨﺪام‬

• NOT NULL constraint (‫ﻟﻀﻤﺎن إدﺧﺎل اﻟﻘﯿﻤﺔ ﻓﻲ اﻟﺤﻘﻞ اﻟﻤﻄﻠﻮب )أي ﻋﺪم ﺗﺮك اﻟﻘﯿﻢ ﻓﺎرﻏﺔ‬
Syntax:
Student_Name varchar2(20) NOT NULL, ç

‫ھﻨﺎ ﯾﺠﺐ ادﺧﺎل اﻻﺳﻢ وﺑﺪون ادﺧﺎل اﻻﺳﻢ ﻟﻦ ﺗﺴﺘﻄﯿﻊ ادﺧﺎل ﺑﺎﻗﻲ اﻟﻤﻌﻠﻮﻣﺎت‬

• UNIQUE constraint (‫ﻣﺤﺪد ﻹدﺧﺎل ﻗﯿﻢ ﻓﺮﯾﺪة ﺗﺨﺘﻠﻒ ﻋﻦ اﻟﻘﯿﻢ اﻷﺧﺮى )ﻟﻜﻨﮫ ﻟﯿﺲ ﻣﻔﺘﺎح رﺋﯿﺴﻲ‬
Syntax:
Student_ID varchar2(20) NOT NULL UNIQUE, ç
‫ھﻨﺎ ﯾﺠﺐ إﺿﺎﻓﺔ رﻗﻢ اﻟﻄﺎﻟﺐ وﺑﻨﻔﺲ اﻟﻮﻗﺖ ﯾﺠﺐ أن ﺗﻜﻮن ﻓﺮﯾﺪة أي ﻏﯿﺮ ﻣﻜﺮرة‬
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(20) NOT NULL,
FirstName varchar(20),
Age int,
CONSTRAINT Person_UC UNIQUE (ID,LastName)); ç
‫اﻟﻤﺤﺪد ھﻨﺎ أن ﻻ ﯾﺘﻜﺮر رﻗﻢ واﺳﻢ اﻟﻌﺎﺋﻠﺔ ﻟﻠﺸﺨﺺ ﻣﻌﺎ أﻛﺜﺮ ﻣﻦ ﻣﺮة وأﯾﻀﺎ أن ﻻ ﺗﻜﻮن أي ﻣﻨﮭﻤﺎ ﻗﯿﻤﺘﮫ ﻓﺎرﻏﺔ‬

• CHECK conditions ‫ﻟﻔﺤﺺ اﻟﻘﯿﻢ اﻟﻤﺪﺧﻠﺔ‬

Syntax:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(20) NOT NULL,
FirstName varchar(20),
Age int CHECK (Age>=18) ç
);

• DEFAULT constraint (‫ﻹدﺧﺎل ﻗﯿﻢ ﻣﺤﺪدة أو ﺛﺎﺑﺘﺔ )اﻓﺘﺮاﺿﯿﺔ‬

Syntax:
Country varchar2(10) DEFAULT 'Jordan'
‫أو ﺗﺎرﯾﺦ إدﺧﺎل اﻟﻤﻌﻠﻮﻣﺎت ﻣﺜﻼ‬
CREATE TABLE Orders (
ID int NOT NULL,
OrderNumber int NOT NULL,
OrderDate date DEFAULT CURRENT_TIMESTAMP ç ‫ھذا اﻟﻛود ﯾﻘوم ﺑﺈدﺧﺎل ﺗﺎرﯾﺦ اﻟﯾوم‬
);

You might also like