DBMS Exp 3
DBMS Exp 3
AIM: Create a database using Data Definition Language (DDL) and apply integrity
constraints for the specified System
THEORY:
Example
The following is a simple example, which will create testdb in your PostgreSQL
schema
• postgres-#
Syntax
The syntax for createdb is as shown below −
Parameters
The table given below lists the parameters with their descriptions.
1
DbnameThe name of a database to create.
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 −
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:
The DEFAULT constraint provides a default value to a column when there is no value
provided while inserting a record into a table.
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.
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.
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.
The basic syntax of an ALTER TABLE command to add a New Column in an existing
table is as follows.
The basic syntax of an ALTER TABLE command to add a NOT NULL constraint to
a column in a table is as follows.
Output:
Conclusion: