You are on page 1of 16

EX.NO.

1 DDL COMMANDS

DATE:

AIM:

To execute and verify the DDL commands.

PROCEDURE:

STEP 1: Create the table with its essential attributes.

STEP 2: Insert attribute values into the table

STEP 3: Execute different Commands and extract information from the table.

COMMANDS:

1. Create: This command is used to create a new table.


CREATE TABLE table_ name
(
column_name1 data_type,
column_name2 data_type,
....
);

2. Alter: This command is used to add, delete, and modify columns in an existing table.

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

ALTER TABLE table_name ADD column_name data type;

To delete a column in a table, use the following syntax

ALTER TABLE table_name DROP COLUMN column_name;

To change the data type of a column in a table, use the following syntax:
ALTER TABLE table_name MODIFY column_name datatype;

3. Drop: This command is used to delete a table.


DROP TABLE table_name;

4. Truncate: This command is used to remove all the records in the table and not the table
itself.
TRUNCATE TABLE table_name;

FOR EXAMPLE

1. Create a table in the name example with the following field’s and data type
NAME varchar(10)
ROLL NUMBER number(06)

SQL> create table example(NAME varchar(10), ROLL NUMBER number(06));

SQL> Table Created successfully

SQL> Desc example // describing the table

Fields Data Type with Keys


NAME varchar(10)
ROLL NUMBER number(06)
SQL>

EXERCISE:
1. Create a table in the name employee with the following field’s and data type
EMPLOYEE NAME Varchar(15)
EMPLOYEE NO Number(10)
DEPT NAME Varchar(04)
DEPT NO Number(04)
DATE OF JOINING(DOJ) DATE
and display the table.

2. To the employee table ADD a new field with the data type mention below
EMPLOYEE SALARY Number(08)
and display it

3. Alter the table employee by dropping the field date of joining and display it.
4. Alter the table employee by changing the data type of dept.no. from integer to
varchar.

5. Truncate the table employee and drop it.

6. Drop the employee table.

EX. NO : 1 b SQL CONSTRAINTS


DATE:

A constraint is a property assigned to a column or the set of columns in a table that


prevents certain types of inconsistent data values from being placed in the column(s). Constraints
are used to enforce the data integrity. This ensures the accuracy and reliability of the data in the
database. The following categories of the data integrity exist:

1. Entity Integrity ensures that there are no duplicate rows in a table.


2. Domain Integrity enforces valid entries for a given column by restricting the type, the format,
or the range of possible values.
3. Referential integrity ensures that rows cannot be deleted, which are used by other records
(for example, corresponding data values between tables will be vital).
4. User-Defined Integrity enforces some specific business rules that do not fall into entity,
domain, or referential integrity categories.
Each of these categories of the data integrity can be enforced by the appropriate
constraints. Microsoft SQL Server supports the following constraints:

1. NOT NULL
2. UNIQUE
3. PRIMARY KEY
4. FOREIGN KEY
5. CHECK
6. DEFAULT

SQL 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:

CREATE TABLE Persons


(
P_Id int NOT NULL,
LastName varchar(15) NOT NULL,
FirstName varchar(15),
Address varchar(25),
City varchar(15)
);

SQL 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

SQL UNIQUE Constraint on CREATE TABLE

The following SQL creates a UNIQUE constraint on the "P_Id" column when the
"Persons" table is created:

CREATE TABLE Persons


(
P_Id int NOT NULL,
LastName varchar(15) NOT NULL,
FirstName varchar(15),
Address varchar(25),
City varchar(15),
UNIQUE (P_Id)
);

SQL UNIQUE Constraint on ALTER TABLE


To create a UNIQUE constraint on the "P_Id" column when the table is already created,
use the following SQL:

ALTER TABLE Persons


ADD UNIQUE (P_Id);

To drop a UNIQUE constraint, use the following SQL:

ALTER TABLE Persons


DROP INDEX uc_PersonID;

SQL 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.
Each table should have a primary key, and each table can have only ONE primary key.

SQL PRIMARY KEY Constraint on CREATE TABLE

The following SQL creates a PRIMARY KEY on the "P_Id" column when the "Persons"
table is created:

CREATE TABLE Persons


(
P_Id int NOT NULL,
LastName varchar(15) NOT NULL,
FirstName varchar(15),
Address varchar(25),
City varchar(15),
PRIMARY KEY (P_Id)
);

SQL PRIMARY KEY Constraint on ALTER TABLE


To create a PRIMARY KEY constraint on the "P_Id" column when the table is already
created, use the following SQL:

ALTER TABLE Persons


ADD PRIMARY KEY (P_Id);

To drop a PRIMARY KEY constraint, use the following SQL:

ALTER TABLE Persons


DROP PRIMARY KEY;

SQL FOREIGN KEY Constraint

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: The FOREIGN KEY constraint is used to prevent actions that would
destroy links between tables. The FOREIGN KEY constraint also prevents that 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.
Table 1.1Person’s 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

Table 1.2 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.

SQL FOREIGN KEY Constraint on CREATE TABLE

The following SQL creates a FOREIGN KEY on the "P_Id" column when the "Orders"
table is created:

CREATE TABLE Orders


(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
);

SQL FOREIGN KEY Constraint on ALTER TABLE

To create a FOREIGN KEY constraint on the "P_Id" column when the "Orders" table is
already created, use the following SQL:

ALTER TABLE Orders


ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id);

To drop a FOREIGN KEY constraint, use the following SQL:

ALTER TABLE Orders


DROP FOREIGN KEY fk_PerOrders;
SQL CHECK Constraint

The CHECK constraint is used to limit the value range that can be placed in a column. If
you define a CHECK constraint on a single column it allows only certain values for this column.
If you define a CHECK constraint on a table it can limit the values in certain columns based on
values in other columns in the row.

SQL CHECK Constraint on CREATE TABLE

The following SQL creates a CHECK constraint on the "P_Id" column when the
"Persons" table is created. The CHECK constraint specifies that the column "P_Id" must only
include integers greater than 0.

CREATE TABLE Persons


(
P_Id int NOT NULL,
LastName varchar(15) NOT NULL,
FirstName varchar(15),
Address varchar(25),
City varchar(15),
CHECK (P_Id>0)
);

SQL CHECK Constraint on ALTER TABLE

To create a CHECK constraint on the "P_Id" column when the table is already created,
use the following SQL:

ALTER TABLE Persons


ADD CHECK (P_Id>0);

To drop a CHECK constraint, use the following SQL:

ALTER TABLE Persons


DROP CONSTRAINT chk_Person;
SQL DEFAULT Constraint

The DEFAULT constraint is used to insert a default value into a column. The default
value will be added to all new records, if no other value is specified.

SQL DEFAULT Constraint on CREATE TABLE

The following SQL creates a DEFAULT constraint on the "City" column when the
"Persons" table is created:

CREATE TABLE Persons


(
P_Id int NOT NULL,
LastName varchar(15) NOT NULL,
FirstName varchar(15),
Address varchar(25),
City varchar(15) DEFAULT 'Sandnes'
);

The DEFAULT constraint can also be used to insert system values, by using functions
like GETDATE():

CREATE TABLE Orders


(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
OrderDate date DEFAULT GETDATE()
);

SQL DEFAULT Constraint on ALTER TABLE

To create a DEFAULT constraint on the "City" column when the table is already created,
use the following SQL:
ALTER TABLE Persons
ALTER City SET DEFAULT 'SANDNES';

To drop a DEFAULT constraint, use the following SQL:

ALTER TABLE Persons


ALTER City DROP DEFAULT;

EXERCISE:
CREATE AND DESCRIBE THE FOLLOWING TABLES:

1. NAME: branch

FIELDS DATATYPE

branch_name varchar2(10) primary key

branch_city varchar2(10)

assets number(8,2)

SQL> create table branch(branch_name varchar2(10) primary key, branch_city


varchar2(10), assets number(8,2));
SQL> Table created

SQL> desc branch;

FIELDS DATATYPE

branch_name varchar2(10) NOT NULL

branch_city varchar2(10)

assets number(8,2)

2. NAME: account

FIELDS DATATYPE

account_no varchar2(11) primary key

branch_name varchar2(10) references branch(branch_name)

balance number(8)
3. NAME: customer

FIELD DATATYPE

customer_id varchar2(11) primary key

customer_name varchar2(10)

customer_street varchar2(15)

customer_city varchar2(15)

4. NAME: depositor

FIELD DATATYPE

customer_id varchar2(11)references customer (customer_id)

account_no varchar2(11)references account (account_no)


5. NAME: loan

FIELDS DATATYPE

loan_no varchar2(4) primary key

branch_name varchar2(10) references branch(branch_name)

amount number(8,2)

6. NAME: borrower

FIELDS DATATYPE

customer_id varchar2(11) references customer(customer_id)

loan_no varcahr2(4) references loan(loan_no)


7.NAME:employee

FIELDS DATATYPE

ename varchar(10)

eid number(5) primary key

salary number(5) check (salary>5000)

RESULT:

You might also like