RDBMS 2
RDBMS 2
02
PART A
(PART A: TO BE REFFERED BY STUDENTS)
A.1 Aim: Apply Data Definition language [DDL] on the relational model designed and apply
certain constrains on the relational model designed.
A.3 Outcome:
After successful completion of this experiment students will be able to
A.4 Theory:
SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to
communicate with a database. According to ANSI (American National Standards Institute), it is
the standard language for relational database management systems. SQL statements are used to
perform tasks such as update data on a database, or retrieve data from a database. Some common
relational database management systems that use SQL are: Oracle, Sybase, Microsoft SQL
Server, Access, Ingres, etc. Although most database systems use SQL, most of them also have
their own additional proprietary extensions that are usually only used on their system. However,
the standard SQL commands such as "Select", "Insert", "Update", "Delete", "Create", and "Drop"
can be used to accomplish almost everything that one needs to do with a database.
A relational database system contains one or more objects called tables. The data or information
for the database are stored in these tables. Tables are uniquely identified by their names and are
comprised of columns and rows. Columns contain the column name, data type, and any other
attributes for the column. Rows contain the records or data for the columns. Here is a sample
table called "weather". City, state, high, and low are the columns. The rows contain the data for
this table:
Weather
create table "tablename"("column1" "data type", "column2" "data type", "column3" "data type");
To create a new table, enter the keywords create table followed by the table name, followed by
an open parenthesis, followed by the first column name, followed by the data type for that
column, followed by any optional constraints, and followed by a closing parenthesis. It is
important to make sure you use an open parenthesis before the beginning table, and a closing
parenthesis after the end of the last column definition. Make sure you seperate each column
definition with a comma. All SQL statements should end with a ";".
The table and column names must start with a letter and can be followed by letters, numbers, or
underscores - not to exceed a total of 30 characters in length. Do not use any SQL reserved
keywords as names for tables or column names (such as "select", "create", "insert", etc).
Data types specify what the type of data can be for that particular column. If a column called
"Last_Name", is to be used to hold names, then that particular column should have a "varchar"
(variable-length character) data type.
number(size) Number value with a max number of column digits specified in parenthesis.
number(size,d) Number value with a maximum number of digits of "size" total, with a maximum
number of "d" digits to the right of the decimal.
Constraints:
What are constraints? When tables are created, it is common for one or more columns to have
constraints associated with them. A constraint is basically a rule associated with a column that
the data entered into that column must follow. For example, a "unique" constraint specifies that
no two records can have the same value in a particular column. They must all be unique. The
other two most popular constraints are "not null" which specifies that a column can't be left
blank, and "primary key". A "primary key" constraint defines a unique identification of each
record (or row) in a table.
The PRIMARY KEY constraint uniquely identifies each record in a database table. Primary keys
must contain UNIQUE values. A primary key column cannot contain NULL values. Most tables
should have a primary key, and each table can have only ONE primary key.
create table Accounts(acc_no number(5) primary key, balance number(8,2)); or create table
or adding primary key constraint using alter command – can be used only if table has been
already created and during creation time constraint was not given.
2. Unique constraint:
The UNIQUE constraint uniquely identifies each record in a database table. The UNIQUE and
PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of
columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.
Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY
constraint per table.
create table person(p_id number(4) primary key, pname varchar(40), mobile_no number(10),
address varchar(100), unique(mobile_no));
or adding unique key using alter command – can be used only if table is already created and at
the time of creation of table constraint was not applied.
create table person(p_id primary key, pname varchar(4) not null, mobile_no number(10), address
varchar(100));
4. Foreign key:
A FOREIGN KEY in one table points to a PRIMARY KEY in another table. Let's illustrate the
foreign key with an example. Look at the following two tables:
1 77895 3
2 44678 3
3 22456 2
4 24562 1
Note that the "P_Id" column in the "Orders" table points to the "P_Id" column in the "Persons"
table. The "P_Id" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
The "P_Id" column in the "Orders" table is a FOREIGN KEY in the "Orders" table. The
FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.
The FOREIGN KEY constraint also prevents invalid data from being inserted into the foreign
key column, because it has to be one of the values contained in the table it points to.
create table orders(o_id number(3) primary key, orderno number not null, p_id foreign key
references persons(p_id));
Adding foreign key with alter table command: Applicable only if table is already created and at
the time of creation foreign key was not given.
orders;
5. ticket_header: fleet_id number(5), ticket_no number(5), doi date, dot date, time_travel
char(8), board_place varchar(20), origin varchar(40), destination varchar(40), adult
number(3), children number(3), total_fare number(7,2), route_id number(5)
fleet_id foreign key, ticket_no primary key, doi- not null, dot-not null, route_id – foreign
key
6. ticket_detail: ticket_no number(5), name varchar(20), sex char(1), age number(3), fare
number(5,2)
1. Write a query to increase size of category description column in category header table
from varchar(20) to varchar(50).
2. Create a table Student_JAIN with columns student number, student name, student
address, student phone number with appropriate data types.
3. Write a query to add primary key column to above created Student_JAIN table.
4. Write a query to create new table Student_JGI with columns Student number, Student
name selected from table Student_JAIN.
7. Write a query to delete column student phone number from Stu_JAIN table.
PART B
(Students must submit the soft copy as per following segments within two hours of the practical.
The soft copy must be uploaded on the student portal or emailed to the concerned lab in charge
faculties at the end of the practical in case the there is no student portal access available)
B.1.1 : Execution of each SQL query used to complete the task given in PART A :
Tables:
1.
2.
3.
4.
5.
6.
7.
Screenshots :
1.
2.
3.
4.
5.
6.
7.
8. Table will be deleted.
B.3 Conclusion:
To sum up, we have learnt all the things to access the tables in different ways.
B.4Question of curiosity:
1. Comment the statement, “Primary key is no different than Foreign key” a primary key
uniquely identifies records within a single table, while a foreign key establishes
relationships between tables by referencing the primary key of another table. They have
distinct roles and characteristics in a database schema.
2. Which other SQL DDL Commands can be applied on the tables generated?
In sql, DDL commands are used to define, manage, and modify the structure of database
objects such as tables. We can use many commands after using CREATE TABLE
command. Some are ALTER TABLE, DROP TABLE, TRUNCATE TABLE, CREATE
INDEX, DROP INDEX, RENAME TABLE, COMMENT ON TABLE, RENAME
COLUMN, ALTER COLUMN, ADD CONSTRAINT, DROP CONSTRAINT, SET
DEFAULT, DISABLE/ENABLE CONSTRAINT and ALTER TABLESPACE etc.
**************