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

Creating Databases with DDL Constraints

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)
360 views8 pages

Creating Databases with DDL Constraints

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
  • Experiment No. 3: Database Creation
  • Syntax and Parameters
  • Constraints
  • Unique and Key Constraints
  • ALTER Table Command
  • Implementation Details

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 [Link]


(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 [Link] 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

Common questions

Powered by AI

Integrity constraints in a database ensure data integrity by enforcing rules that limit the data type and entry into database columns. In PostgreSQL, constraints such as NOT NULL, UNIQUE, CHECK, and key constraints like PRIMARY and FOREIGN KEYS maintain data integrity by preventing the entry of invalid data . For example, NOT NULL constraints prevent the insertion of NULL values where not allowed, UNIQUE constraints ensure that all values in a column are different, and CHECK constraints allow only values that meet a specified condition . These constraints work together to ensure that database operations do not lead to inconsistencies or loss of data integrity .

The CREATE TABLE statement in PostgreSQL allows for the definition of various constraints directly within the SQL statement to enforce data integrity. When creating a table, constraints such as NOT NULL, UNIQUE, DEFAULT values, CHECK, and PRIMARY or FOREIGN KEYS can be included. These constraints ensure that the data entered into the table meets specific requirements, such as a column not accepting null values or only allowing unique entries . For example, using CREATE TABLE with a UNIQUE constraint ensures no duplicates in a specified column, while a CHECK constraint can enforce that entries fall within certain parameters, such as ensuring a student's roll number is above 1000 .

NOT NULL and UNIQUE constraints in PostgreSQL enforce different data integrity rules within a table. The NOT NULL constraint ensures that a column must always have a value by disallowing NULL entries, thus ensuring that every record in the table contains meaningful data in that specific column . On the other hand, the UNIQUE constraint requires all values in a column to be distinct from others in the same column, eliminating duplicate values. This constraint ensures that each entry remains unique, which is crucial for fields that serve as identifiers or keys . While a NOT NULL constraint ensures presence, a UNIQUE constraint enforces uniqueness of data, both upholding complementary facets of data integrity but in different contexts .

Defining complex constraints using PostgreSQL’s ALTER TABLE command involves altering existing table definitions to enforce or adjust rules like primary keys, uniqueness, or checking conditions. This process allows for flexible schema evolution by adding or modifying constraints post-creation as business rules change . The impact on database operations can be significant: adding constraints like PRIMARY KEY or CHECK can ensure additional data integrity and alignment with business requirements, but might also affect performance, especially in large datasets, due to increased processing during data modifications . Thus, while they are vital for maintaining data consistency and validation, the use of complex constraints requires careful planning regarding database performance and operational impact .

The ALTER TABLE command in PostgreSQL is significant for modifying the structure of existing tables without dropping them. This command allows users to add, delete, or modify columns and constraints, making it possible to adjust the database schema as needs evolve . For instance, ALTER TABLE can be used to add a new NOT NULL constraint to an existing column, ensuring data integrity is maintained as the schema changes . Additionally, constraints like UNIQUE can be added to ensure data uniqueness across updated tables, or removed if no longer needed, demonstrating the flexibility and adaptability of PostgreSQL in dynamic data environments .

Primary and foreign keys are fundamental in maintaining relational integrity within a database system in PostgreSQL. A primary key uniquely identifies each record in a table and cannot contain null values, guaranteeing distinct and accessible records . Foreign keys, on the other hand, establish relationships between tables by linking a column or group of columns to a primary key in another table, enforcing referential integrity by ensuring that referenced records exist . This relational linkage prevents orphaned records and supports the cohesion of related data across tables, allowing for complex queries and integrity across relational databases. The use of these keys ensures data accuracy and reliability, preventing inconsistencies and upholding relationships between datasets within the database system .

In PostgreSQL, both the SQL command CREATE DATABASE and the command-line tool createdb serve to create new databases, but they have differences in execution context and functionality. The CREATE DATABASE SQL command is used within SQL environments and requires a connection to a PostgreSQL server with suitable permissions to execute the creation . Conversely, the createdb command-line executable allows database creation from the terminal, providing an option to include comments about the newly created database easily within the same command . This command-line tool can be more intuitive for users accustomed to scripting and using shell commands as part of their workflow, permitting the creation process outside of interactive SQL environments .

CHECK constraints in PostgreSQL add a layer of data validation by ensuring that all values in a column meet specified criteria, thereby preventing invalid data entries. For example, in a table storing student information, a CHECK constraint could be applied to the ROLL_NO column to ensure that all roll numbers are greater than 1000 with the syntax: CHECK(ROLL_NO > 1000). This ensures that any attempt to insert a roll number less than or equal to 1000 fails, maintaining data validity and enforcing business rules directly within the database structure, thus providing a robust method to enforce data integrity along with other constraints .

In PostgreSQL, the DEFAULT constraint plays a crucial role in automating data entry by providing a fallback value when no specific value is supplied for a column. This constraint ensures that when new records are inserted, each field will have appropriate content even if not manually specified by the user . By setting a default, like 'EXAM_FEE INT DEFAULT 10000' in a table, PostgreSQL automatically assigns the value 10000 to the EXAM_FEE column unless a different value is explicitly provided during data insertion. This guarantees that all records contain necessary data for consistency and reduces the likelihood of entry errors or uninitialized fields .

PostgreSQL's open-source nature significantly influences its widespread adoption due to several factors. First, the absence of licensing costs makes it accessible to a broad user base, including small businesses and startups that may not afford proprietary database solutions . Furthermore, its development by a global team of volunteers ensures a wide array of features, robust updates, and a wealth of community support, contributing to its reliability and performance . Being open-source also means that PostgreSQL can be customized and enhanced according to specific use-case requirements, offering flexibility that many closed-source alternatives do not. This adaptability, combined with a strong backing community for troubleshooting and innovation, makes PostgreSQL a favorable choice in diverse database management scenarios .

Department of EXTC, SIES GST 
 
EXPERIMENT NO. 3 
 
AIM:  Create a database using Data Definition Language (DDL) an
Department of EXTC, SIES GST 
 
command line and it allows a comment to be added into the database, all in one 
com
Department of EXTC, SIES GST 
 
Constraints 
Constraints enforce limits to the data or type of data that can be ins
Department of EXTC, SIES GST 
 
UNIQUE:  
UNIQUE Constraint enforces a column or set of columns to have unique valu
Department of EXTC, SIES GST 
 
Key constraints: 
PRIMARY KEY: 
PRIMARY KEY uniquely identifies each record in a ta
Department of EXTC, SIES GST 
 
The basic syntax of an ALTER TABLE command to change theDATA TYPE of a 
column in a
Department of EXTC, SIES GST 
 
IMPLEMENTATION DETAILS (Problem Statement, Query and Screenshots of 
Results):
 
 
 
 
 
Department of EXTC, SIES GST

You might also like