Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

MCA 23: Database Management System

Handout 1
The objective of this handout is to enable you to create a relational database schema in
Oracle.
To start open Oracle Database 10g Express Edition  Go to Database Home Page
Username: System
Password: mcalab1234
Opens a new page that has the options of administration, Object Browser, SQL and Utilities.
Click on SQL.
Opens a page with the options of SQL commands, SQL scripts and Query Builder. Select SQL
Commands.

The following commands will help you create, alter and insert data into the schema.
Data Types supported by Oracle.
Data Type Description
String Data Types
CHAR(size) A FIXED length string (can contain letters, numbers, and special
characters). The size parameter specifies the column length in
characters - can be from 0 to 255. Default is 1
VARCHAR(size) A VARIABLE length string (can contain letters, numbers, and special
characters). The size parameter specifies the maximum column length
in characters - can be from 0 to 65535
BINARY(size) Equal to CHAR (), but stores binary byte strings. The size parameter
specifies the column length in bytes. Default is 1
MCA 23: Database Management System
Integer Data Types
BOOL Zero is considered as false, nonzero values are considered as true.
INT(size) A medium integer. Signed range is from -2147483648 to 2147483647.
Unsigned range is from 0 to 4294967295. The size parameter specifies
the maximum display width (which is 255)
FLOAT(size, d) A floating point number. The total number of digits is specified in size.
The number of digits after the decimal point is specified in the d
parameter.
Date Data Types
DATE A date. Format: YYYY-MM-DD. The supported range is from '1000-
01-01' to '9999-12-31'
TIMESTAMP(fsp) A timestamp. TIMESTAMP values are stored as the number of
seconds since the Unix epoch ('1970-01-01 00:00:00' UTC). Format:
YYYY-MM-DD hh:mm:ss.

Create Command: -
The CREATE TABLE statement is used to create a new table in a database. The example has
the mechanism of giving a primary key in the create table command itself.
General Syntax Example
CREATE TABLE table_name ( CREATE TABLE Persons (
column1 datatype, ID int NOT NULL,
column2 datatype, LastName varchar(255) NOT NULL,
column3 datatype, FirstName varchar(255),
.... Age int,
); PRIMARY KEY (ID)
);
Creating table with a primary key and a foreign key
Orders table has a foreign key on persons CREATE TABLE Orders (
table created above OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID)
REFERENCES Persons(ID)
);
To create a FOREIGN KEY constraint on the CREATE TABLE Orders (
"Person(ID)" column when the "Orders" OrderID int NOT NULL,
table is already created using the alter table OrderNumber int NOT NULL,
command. PersonID int,
PRIMARY KEY (OrderID)
);

ALTER TABLE Orders


ADD FOREIGN KEY (PersonID)
REFERENCES Persons(ID);
MCA 23: Database Management System

Deleting the Structure of a Table


DROP TABLE table_name; Drop table Orders;
Drop Table Persons;
Note: Be careful before dropping a table.
Deleting a table will result in loss of
complete information stored in the table!
Table Structure Display: To look at the columns and datatypes of a table
DESCRIBE table_name; Describe Orders;
Desc Persons;
DESC table_name;
Insert into
The INSERT INTO command is used to INSERT INTO Persons (ID, Lastname,
insert new rows in a table. Firstname, Age)
VALUES (1001, 'Bobby', 'Brown', 29);

INSERT INTO Persons VALUES (1002,


'Stella', 'Green', 36);

INSERT INTO Orders VALUES (101, 1011,


Null);
Alter Statement to change the constraints on ALTER TABLE Persons
the table after creation ADD CONSTRAINT PK_Person
PRIMARY KEY (ID,LastName);
Update
Setting or changing the value of a particular UPDATE Orders SET Personid = 1001
row based on the primary key of that table. WHERE OrderId = 101;
Displaying the contents of a table
SELECT Select * from Persons;
The SELECT command is used to select data Select * from Orders;
from a database. The data returned is stored
in a result table, called the result set. Select orderid, ordernumber from Orders;

Using the above commands, the student is expected to complete the following assignment
MCA 23: Database Management System

Assignment
Please create the following database schema with the data in each of the tables.

The arrows depict the foreign key references and the underlined column denotes the primary
key of that table.

Create the above tables and insert the following data into the created tables.
MCA 23: Database Management System

You might also like