0% found this document useful (0 votes)
8 views

Dbms Exp1

Uploaded by

Shreya Gokhale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Dbms Exp1

Uploaded by

Shreya Gokhale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

EXPERIMENT NO.

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

THEORY:
Command with Syntax and Example:
DDL Commands:
CREATE:
Definition: Creates a new table
Syntax: create table table_name (column1 datatype, column2 datatype, ...);
Example:
create table wildlife_reserve (
animal_id int primary key,
species varchar(10),
habitat varchar(10),
population int
);

ALTER:
Definition: Modifies an existing table structure.
Syntax: alter table table_name add column_name datatype;
Example:
alter table wildlife_reserve add diet varchar(10);

DROP:
Definition: Deletes a table
Syntax: drop table table_name;
Example:
drop table wildlife_reserve;
TRUNCATE:
Definition: Removes all records from a table but retains the table structure.
Syntax: truncate table table_name;
Example:
truncate table wildlife_reserve;

RENAME:
Definition: Changes the name of an existing table.
Syntax: rename table old_table_name to new_table_name;
Example:
rename table wildlife_reserve TO animal_habitat;

Constraints:
NOT NULL:
Definition: Ensures a column cannot have a NULL value.
Syntax: column_name datatype NOT NULL
Example:
species varchar(10) NOT NULL

UNIQUE:
Definition: Ensures all values in a column are unique.
Syntax: column_name datatype UNIQUE
Example:
tracking_tag varchar(10) UNIQUE

DEFAULT:
Definition: Sets a default value for a column.
Syntax: column_name datatype DEFAULT default_value
Example:
status varchar(10) DEFAULT 'Unknown'

CHECK:
Definition: Ensures all values in a column satisfy a specific condition.
Syntax: column_name datatype CHECK (condition)
Example:
population int CHECK (population >= 0)

PRIMARY KEY:
Definition: Uniquely identifies each record in a table.
Syntax: column_name datatype PRIMARY KEY
Example:
animal_id int PRIMARY KEY

FOREIGN KEY:
Definition: Ensures referential integrity by linking two tables.
Syntax: column_name datatype REFERENCES other_table (other_column)
Example:
habitat_id int REFERENCES habitats (id)

ALTER TABLE:
Add Column:
Definition: Adds a new column to an existing table.
Syntax: alter table table_name add column_name datatype;
Example:
alter table wildlife_reserve add conservation_status VARCHAR(10);

Delete Column:
Definition: Removes a column from an existing table.
Syntax: alter table table_name drop column column_name;
Example:
alter table wildlife_reserve drop column diet;

Change Datatype:
Definition: Changes the data type of a column.
Syntax: alter table table_name modify column_name new_datatype;
Example:
alter table wildlife_reserve modify population varchar(10);

Add Constraint to Column:


Definition: Adds a constraint to an existing column.
Syntax: alter table table_name modify column_name datatype constraint;
Example:
alter table wildlife_reserve modify tracking_tag varchar(10) UNIQUE;

Add Table Level Constraint:


Definition: Adds a constraint that applies to the entire table.
Syntax: alter table table_name add constraint constraint_name constraint_type
(column_name);
Example:
alter table wildlife_reserve add constraint chk_population CHECK (population >= 0);

Case study 1 Car accidents:


person (driver_id, name, address, age)
car (license, model, year,chassis number)
accident (report number, date, location)
owns (driver id, license)
participated (report number, license, driver_id, damage_amount, claim _settled (y/n))
car _claim(license, claim_date )
1. Create Constraint :
1.person name can not be null,
2. default year 2023,
3.check age should be greater than 18
4. Chassis numbers are unique.
2. Alter : delete address column, change datatype of model from varchar[5] to [7], add
address column with default value mumbai, rename column name to pname.
3. Rename table car_claim to claims_filed
4. Delete schema claims_filed.

Creating tables :
person table :

car table :
accident table :

owns table :

participated table :
car _claim table :

Alter :

1. Delete address column


2. Change datatype of model from varchar[5] to [7]

3. Add address column with default value Mumbai

4. Rename column name to pname.

Rename table car_claim to claims_filed :


Delete schema clams_filed

You might also like