You are on page 1of 6

Experiment No 1

Title : Create the following Tables along with their Constraints

classroom (building, room number, capacity)


department (dept name, building, budget)
course (course id, title, credits)
instructor (ID, name, salary)
student (ID, name, tot cred)
teaches (ID, course id, sec id, semester, year)
takes (ID, course id, sec id, semester, year, grade)
sec course (course id, sec id, semester, year)
course_dept (course_id,dept_name)

Objective: To understand the table structure or schema

Key Concepts: create, alter, drop, truncate, rename, primary key, foreign key, not null, check, unique and
default

DDL Commands:

1. Create:
-Creates a new table, a view of a table, or other object in database.

Syntax:

create table table_name(column_name1 data_type(size),column_name2 data_type(size),


column_name3 data_type(size),....);

SQL Constraints

SQL constraints are used to specify rules for the data in a table.

If there is any violation between the constraint and the data action, the action is aborted by the constraint.

Constraints can be specified when the table is created (inside the CREATE TABLE statement) or after the
table is created (inside the ALTER TABLE statement).
SQL CREATE TABLE + CONSTRAINT

Syntax

create table table_name(column_name1 ata_type(size) constraint_name,column_name2


data_type(size) constraint_name,column_name3 data_type(size) constraint_name,....);

In SQL, we have the following constraints:

PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures that a column (or combination
of two or more columns) have a unique identity which helps to find a particular record in a table more
easily and quickly

FOREIGN KEY - Ensure the referential integrity of the data in one table to match values in another table

NOT NULL - Indicates that a column cannot store NULL value

UNIQUE - Ensures that each row for a column must have a unique value

CHECK - Ensures that the value in a column meets a specific condition

DEFAULT - Specifies a default value for a column

Primary Key Constraint

- Primary key constraint uniquely identifies each record in a database.

- A PRIMARY KEY constraint automatically has a UNIQUE & NOT NULL constraint defined on
it.

Syntax:

create table table_name(column_name1 data_type(size) primary key,column_name2


data_type(size) ,column_name3 data_type(size),....);
For making more than one attribute as primary key

Syntax:
create table table_name(column_name1 data_type(size) column_name2
data_type(size) ,),primary key(column_name1,column_name2)....);

Foreign Key Constraint

Syntax: Table 1

Table 2

create table table_name1(column_name1 data_type(size) primary key,column_name2


data_type(size) ,column_name3 data_type(size)....);

create table table_name2(column_name1 data_type(size) primary key,column_name2


data_type(size) ,column_name3 data_type(size)foreign key references table_name1....);

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.

The following SQL enforces the "P_Id" column and the "LastName" column to not accept NULL values:

Example

create table personsnotnull(p_id int not null,lastname varchar(255) not null,


firstname varchar(255),address varchar(255),city varchar(255))

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.

Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per
table.

create table persons(p_id int not null unique,lastname varchar(255) not null,
firstname varchar(255),address varchar(255),city varchar(255))

To allow naming of a UNIQUE constraint, and for defining a UNIQUE constraint on multiple columns,
use the following SQL

syntax:

create table persons(p_id int not null,lastname varchar(255) not null,firstname


varchar(255),address varchar(255),city varchar(255),constraint uc_personid unique
(p_id,lastname))
Check constraint

The CHECK Constraint enables a condition to check the value being entered into a record. If the condition
evaluates to false, the record violates the constraint and isn't entered the table.

Example:

create table customers( id int not null, name varchar (20) not null, age int
not null check (age >= 18), address char (25) , salary decimal (18, 2), primary key (id));
Default Constraint

The DEFAULT constraint provides a default value to a column when the INSERT INTO statement does not
provide a specific value.
Example:

2. al
create table customers (id int not null, name varchar (20) not null, age int not t
drop table table_name
null, address char (25) , salary decimal (18, 2) default 5000.00, primary key (id)); e
r
:
- Modifies an existing database object, such as a table.

Syntax:

To add a column in a table, use the following syntax:

alter table table_name add column_name datatype

To delete a column in a table, use the following syntax (notice that some database systems don't allow
deleting a column):

To change the data type of a column in a table, use the following syntax:

My SQL / Oracle (prior version 10G):

alter table table_name drop column_name

3.
3.
drop
- Deletes an entire table, a view of a table or other object in the database.
Syntax:

ALTER TABLE table_name


MODIFY column_name datatype
The DROP TABLE statement is used to delete a table.
The DROP DATABASE statement is used to delete a database.

What if we only want to delete the data inside the table, and not the table itself?

Then, use the TRUNCATE TABLE statement:

4.
drop database database_name 4.
4.
4.
truncate

- deletes the data inside the table, and not the table itself

truncate table table_name

Conclusion:

Thus DDL Commands are successfully studied.

You might also like