0% found this document useful (0 votes)
21 views

RDBMS 2

The document describes how to create tables in SQL with constraints and provides examples of creating tables with primary keys, foreign keys, unique constraints and not null constraints. It also provides examples of SQL queries to alter tables and drop tables. As an exercise, it lists several tables to create with the specified columns and constraints.

Uploaded by

DEEKSHITH R
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

RDBMS 2

The document describes how to create tables in SQL with constraints and provides examples of creating tables with primary keys, foreign keys, unique constraints and not null constraints. It also provides examples of SQL queries to alter tables and drop tables. As an exercise, it lists several tables to create with the specified columns and constraints.

Uploaded by

DEEKSHITH R
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Experiment No.

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.2 Prerequisite: - Fundamental knowledge of DDL instructions and the SQL

A.3 Outcome:
After successful completion of this experiment students will be able to

1. Design the relational model with basic constrains.


2. Apply the DDL commands on the developed relational models.
3. Enlist the needs of Business logic /constrains in Databases.

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

city state high low

Phoenix Arizona 105 90

Tucson Arizona 101 92


tablestatement:

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.

Here are the most common Data types:


char(size) Fixed-length character string. Size is specified in parenthesis. Max 255 bytes.

varchar(size) Variable-length character string. Max size is specified in parenthesis.

number(size) Number value with a max number of column digits specified in parenthesis.

date Date value

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.

Format of create table if you were to use optional constraints:

create table "tablename" ("column1" "data type" [constraint],

"column2" "data type" [constraint],

"column3" "data type" [constraint]);

1. Primary key constraint:

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

Accounts(acc_no number(5), balance number(8,2), primary key(acc_no));

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.

alter table Accounts add primary key(acc_no);

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)


unique, address varchar(100)); Or

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.

alter table person add unique(mobile_no);

3. NOT NULL constraint:


The NOT NULL constraint enforces a column to NOT accept NULL values. The NOT NULL
constraint enforces a field to always contain a value. This means that you cannot insert a new
record, or update a record without adding a value to this field.

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:

The "Persons" table:

P_Id LastName FirstName Address City

1 Hansen Ola Timoteivn 10 Sandnes

2 Svendson Tove Borgvn 23 Sandnes

3 Pettersen Kari Storgt 20 Stavanger

The "Orders" table:

O_Id OrderNo P_Id

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.

alter table orders add foreign key(p_id) references persons(p_id);

Drop Table Command:


It is used to completely delete table along with all its rows from the memory.

Syntax: drop table table

name; drop table

orders;

A.5 Task: Create below tables along with constraints:


Create following tables:

1. category_header : cat_code number(5), cat_desc varchar(20), cat_code will be primary


key

2. route_header: route_id number(5), route_no number(5), cat_code number(5), origin


varchar(20), destination varchar(20), fare number(7,2), distance number(3), capacity
number(3) , cat_code will be foreign key, route_id will be primary key

3. place_header: place_id number(5), place_name varchar(20), place_address varchar(50),


bus_station varchar(10), place_id will be primary key, place_name not null

4. fleet_header: fleet_id number(5), day date, route_id number(5), cat_code number(5),


fleet_id primary key, route_id foreign key, cat_code foreign key

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)

7. route_detail: route_id number(5), place_id number(5), nonstop char(1)


Solve below queries after creating tables:

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.

5. Write a query to rename Student_JAIN table to Stu_JAIN.

6. Write a query to add birth date column to stu_JAIN table.

7. Write a query to delete column student phone number from Stu_JAIN table.

8. Write a query to delete 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)

USN No. 22BTRCO045 Name: S.J. Sharmas vali


Program : CSE-IOT Division: A
Batch: 2 Date of Experiment:
Date of Submission: 20-09-2023 Grade :
B.1 Tasks given in PART A to be completed here (Students must write the answers of the
task(s) given in the PART A )

B.1.1 : Execution of each SQL query used to complete the task given in PART A :

1. ALTER TABLE category_header MODIFY cat_desc VARCHAR(50); 2.


create table Student_JAIN (student_number number(5),student_name
varchar(50),student_address varchar(100),student_phone_number
varchar(15));
3. alter table Student_JAIN add primary key (student_number);
4. create table Student_JGI AS SELECT student_number, student_name from
Student_JAIN;
5. alter table Student_JAIN rename to Stu_JAIN;
6. ALTER TABLE Stu_JAIN ADD birth_date DATE;
7. CREATE TABLE Stu_JAIN_New AS SELECT student_number, student_name,
birth_date FROM Stu_JAIN;
8. DROP TABLE Stu_JAIN;

B.1.2 : Screenshots of the tables designed with constrains:

Tables:

1.

2.

3.

4.
5.

6.

7.

Screenshots :

1.
2.

3.

4.

5.

6.

7.
8. Table will be deleted.

B.2 Observations and Learning:


We have learned the basics of databases, which are used to store and manage data. We learned how to
create and manage tables, define data types, and create constraints. We also discussed the importance
of databases in our daily lives, such as tracking our bank accounts and managing online stores.
In even simpler terms, databases are like digital filing cabinets that help us to organize and keep track of
our information. We can use databases to store all sorts of data, such as customer information, product
inventory, and financial records..

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.

**************

You might also like