0% found this document useful (0 votes)
545 views8 pages

DBMS Exp 3

The document discusses creating a database using SQL DDL commands and applying integrity constraints. It defines key terms like CREATE DATABASE, CREATE TABLE, ALTER TABLE, constraints like PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, DEFAULT, CHECK. It provides syntax for creating a database and table, adding, modifying and dropping columns and constraints. The aim is to create a database with proper constraints to define and maintain data integrity.

Uploaded by

Aman Chavan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
545 views8 pages

DBMS Exp 3

The document discusses creating a database using SQL DDL commands and applying integrity constraints. It defines key terms like CREATE DATABASE, CREATE TABLE, ALTER TABLE, constraints like PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, DEFAULT, CHECK. It provides syntax for creating a database and table, adding, modifying and dropping columns and constraints. The aim is to create a database with proper constraints to define and maintain data integrity.

Uploaded by

Aman Chavan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

EXPERIMENT NO.

AIM: Create a database using Data Definition Language (DDL) and apply integrity
constraints for the specified System

OBJECTIVES: Define/modify database definitions with proper constraints

THEORY:

PostgreSQL is a powerful, open source object-relational database system.PostgreSQL


(pronounced as post-gress-Q-L) is an open source relational database management
system (DBMS) developed by a worldwide team of volunteers. PostgreSQL is not
controlled by any corporation or other private entity and the source code is available
free of charge.ostgreSQL provides two ways of creating a new database −

• Using CREATE DATABASE, an SQL command.

• Using createdb a command-line executable.


Syntax
The basic syntax of CREATE DATABASE statement is as follows −

• CREATE DATABASE dbname;

where dbname is the name of a database to create.

Example
The following is a simple example, which will create testdb in your PostgreSQL
schema

• postgres=# CREATE DATABASE testdb;

• postgres-#

Using createdb Command


PostgreSQL command line executable createdb is a wrapper around the SQL
command CREATE DATABASE. The only difference between this command and SQL
command CREATE DATABASE is that the former can be directly run fromthe

Department of EXTC, SIES GST


command line and it allows a comment to be added into the database, all in one
command.

Syntax
The syntax for createdb is as shown below −

createdb [option...] [dbname [description]]

Parameters
The table given below lists the parameters with their descriptions.

S. No. Parameter & Description

1
DbnameThe name of a database to create.

DescriptionSpecifies a comment to be associated with the newly created


2
database.

3 Optionscommand-line arguments, which createdb accepts.

The PostgreSQL CREATE TABLE statement is used to create a new table in any of
the given database.

Syntax
Basic syntax of CREATE TABLE statement is as follows −

CREATE TABLE table_name(


column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);

Department of EXTC, SIES GST


Constraints

Constraints enforce limits to the data or type of data that can be inserted/updated/deleted
from a table. The whole purpose of constraints is to maintain the data integrity during
an update/delete/insert into a table. In this tutorial we will learn several types of
constraints that can be created in RDBMS.

Types of constraints

• NOT NULL
• UNIQUE
• DEFAULT
• CHECK
• Key Constraints – PRIMARY KEY, FOREIGN KEY
• Domain constraints

NOT NULL:

NOT NULL constraint makes sure that a column does not hold NULL value. When we
don’t provide value for a particular column while inserting a record into a table, it
takes NULL value by default. By specifying NULL constraint, we can be sure that a
particular column(s) cannot have NULL values.

Example:

CREATE TABLE STUDENT(


ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL,
STU_AGE INT NOT NULL,
STU_ADDRESS VARCHAR (235),
PRIMARY KEY (ROLL_NO)
);

Department of EXTC, SIES GST


UNIQUE:

UNIQUE Constraint enforces a column or set of columns to have unique values. If a


column has a unique constraint, it means that particular column cannot have duplicate
values in a table.

CREATE TABLE STUDENT(


ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL UNIQUE,
STU_AGE INT NOT NULL,
STU_ADDRESS VARCHAR (35) UNIQUE,
PRIMARY KEY (ROLL_NO)
);
DEFAULT:

The DEFAULT constraint provides a default value to a column when there is no value
provided while inserting a record into a table.

CREATE TABLE STUDENT(


ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL,
STU_AGE INT NOT NULL,
EXAM_FEE INT DEFAULT 10000,
STU_ADDRESS VARCHAR (35),
PRIMARY KEY (ROLL_NO)
);
CHECK:

This constraint is used for specifying range of values for a particular column of a
table. When this constraint is being set on a column, it ensures that the specified
column must have the value falling in the specified range.

CREATE TABLE STUDENT(


ROLL_NO INT NOT NULL CHECK(ROLL_NO >1000),
STU_NAME VARCHAR (35) NOT NULL,
STU_AGE INT NOT NULL,
EXAM_FEE INT DEFAULT 10000,
STU_ADDRESS VARCHAR (35),
PRIMARY KEY (ROLL_NO)
);
In the above example we have set the check constraint on ROLL_NO column of
STUDENT table. Now, the ROLL_NO field must have the value greater than 1000.

Department of EXTC, SIES GST


Key constraints:

PRIMARY KEY:

PRIMARY KEY uniquely identifies each record in a table. It must have unique values
and cannot contain nulls. In the below example the ROLL_NO field is marked as
primary key, that means the ROLL_NO field cannot have duplicate and null values.

CREATE TABLE STUDENT(


ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL UNIQUE,
STU_AGE INT NOT NULL,
STU_ADDRESS VARCHAR (35) UNIQUE,
PRIMARY KEY (ROLL_NO)
);
FOREIGN KEY:

Foreign keys are the columns of a table that points to the primary key of another table.
They act as a cross-reference between tables.

Domain constraints:

Each table has certain set of columns and each column allows a same type of data,
based on its data type. The column does not accept values of any other data type.

ALTER TABLE command is used to add, delete or modify columns in an existing


table. You should also use the ALTER TABLE command to add and drop various
constraints on an existing table.

The basic syntax of an ALTER TABLE command to add a New Column in an existing
table is as follows.

ALTER TABLE table_name ADD column_name datatype;

The basic syntax of an ALTER TABLE command to DROP COLUMN in an existing


table is as follows.

ALTER TABLE table_name DROP COLUMN column_name;

Department of EXTC, SIES GST


The basic syntax of an ALTER TABLE command to change theDATA TYPE of a
column in a table is as follows.

ALTER TABLE table_name MODIFY COLUMN column_name datatype;

The basic syntax of an ALTER TABLE command to add a NOT NULL constraint to
a column in a table is as follows.

ALTER TABLE table_name MODIFY column_name datatype NOT NULL;

The basic syntax of ALTER TABLE to ADD UNIQUE CONSTRAINT to a table is


as follows.

ALTER TABLE table_name


ADD CONSTRAINT MyUniqueConstraint UNIQUE(column1, column2...);

The basic syntax of an ALTER TABLE command to ADD CHECK


CONSTRAINT to a table is as follows.

ALTER TABLE table_name


ADD CONSTRAINT MyUniqueConstraint CHECK (CONDITION);

The basic syntax of an ALTER TABLE command to ADD PRIMARY


KEY constraint to a table is as follows.

ALTER TABLE table_name


ADD CONSTRAINT MyPrimaryKey PRIMARY KEY (column1, column2...);

The basic syntax of an ALTER TABLE command to DROP CONSTRAINT from a


table is as follows.

ALTER TABLE table_name


DROP CONSTRAINT MyUniqueConstraint;

The basic syntax of an ALTER TABLE command to DROP PRIMARY


KEY constraint from a table is as follows.

ALTER TABLE table_name


DROP CONSTRAINT MyPrimaryKey;

Department of EXTC, SIES GST


IMPLEMENTATION DETAILS (Problem Statement, Query and Screenshots of
Results):

Output:

Conclusion:

We successfully create a database using Data Definition Language (DDL) and


apply integrity constraints for the specified System.

Name: Aman Chavan


PRN: 120A7016
Branch: SE - ECS

Department of EXTC, SIES GST


Department of EXTC, SIES GST

You might also like