Dbms Exp1
Dbms Exp1
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);
Creating tables :
person table :
car table :
accident table :
owns table :
participated table :
car _claim table :
Alter :