Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
32 views16 pages

Prasiddha Acharya Lab3

The document is a lab report on Database Management Systems submitted by Prasiddha Acharya, detailing SQL constraints such as NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, and DEFAULT. It includes practical exercises for creating tables, inserting records, and applying various SQL commands to demonstrate the use of these constraints. The report also covers data manipulation techniques and constraints management in SQL.

Uploaded by

automatedtailor
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)
32 views16 pages

Prasiddha Acharya Lab3

The document is a lab report on Database Management Systems submitted by Prasiddha Acharya, detailing SQL constraints such as NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, and DEFAULT. It includes practical exercises for creating tables, inserting records, and applying various SQL commands to demonstrate the use of these constraints. The report also covers data manipulation techniques and constraints management in SQL.

Uploaded by

automatedtailor
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/ 16

NEPAL COLLEGE OF INFORMATION TECHNOLOGY

Balkumari, Lalitpur

LAB REPORT ON DATABASE MANAGEMENT SYSTEMS

Report No. 3
Submited By: Submited To:
Name: Prasiddha Acharya Instructor: Manil Vaidhya
Roll no: 37 Department of So�ware
Faculty: So�ware Engineering
Engineering Submission Date: 22
Semester: 4th December, 2023
FAMILIARIZATION WITH VARIOUS CONSTRAINTS IN SQL (NULL VALUES,
PRIMARY KEY, UNIQUE KEY, DEFAULT VALUE, FOREIGN KEY, CHECK and
wild card characters)

Theory:
- SQL constraints are used to specify rules for data in a table
- Constraints can be specified when the table is created with the
CREATE TABLE statement, or a�er the table is created with the
ALTER TABLE statement
- Constraints are used to limit the type of data that can go into a
table. This ensures the accuracy and the reliability of the data in
the table. If there is any viola�on between the constraint and the
data ac�on, the ac�on is aborted
- Constraints can be column level or table level.
- The following constraints are commonly used in SQL:

1. NOT NULL: ensures that a column does not have a NULL value
2. UNIQUE: ensures that all values in a column are different
3. PRIMARY KEY: a combina�on of NOT NULL and UNIQUE. Uniquely
iden�fies each row in a table
4. FOREIGN KEY: prevents ac�ons that would destroy links between
tables. The table with the foreign key is called the child table and
the table with the primary key is called the referenced table or the
parent table.
5. CHECK: ensures that the values in a column sa�sfies a specific
condi�on
6. DEFAULT: sets a default value for a column if no value is specified
7. CREATE INDEX: used to create and retrieve data from the database
very quickly
Prac�cal:
1. Create a table that has id (primary key), name, age, mobile
(unique, not null), and address. Some of the restric�ons are
provided that age must be less than 20 and not null, address is
default ‘ktm’.
create table student (id int primary key, name varchar(30), age int
not null check(age<20), mobile bigint unique not null, address
varchar(30) default 'ktm');

2. Create another table stud with sid as primary key which has id as a
foreign key from table student and fee_pay and fee_due.
create table stud(sid int primary key, id int, fee_pay float, fee_due date,
foreign key(id) references student(id));

3. Add new column gender to student table which is not null and
default value ‘male’.
alter table student add column gender varchar(5) not null default
'male';

4. Insert any 5 records in table (1st insert valid result and screenshot
of every violated record).
Invalid:
insert into student (id,name,age,mobile,gender) values (21, "ram", 21,
980, "male");

insert into student values (12, "sam", NULL, 980, 'pok', NULL);
Valid:
insert into student values (12, "sam", 17, 980, 'pok', "male");
insert into student values (13, "sam", 19, 981, 'poke', "male"), (14,
"abc", 13, 678, "tre", 'fmale'), (45, "eret", 10, 467,"euwyi", "fmale"),
(37, "bnsdg",11, 7232, "kihkweu", "male"), (23, "asrf", 14, 8908, "owiu",
"male");
5. Display all table info.
select * from student;

6. Display dis�nct name.


SELECT dis�nct(name) FROM `student`;
7. Select name and id of ‘ram’.
insert into student values (34, "ram", 9, 876, "er", "male"), (65, "ram",
12, 345, "345", "male");
select name, id from student where name="ram";

8. Select info where id>5.


select * from student where id>5;

9. Select info from table where name is ‘ram’ and address are ‘ktm’.
update student set address = "ktm" where name='ram' and id = 34;
select * from student where name='ram' and address = 'ktm';
10. Display name star�ng with R only.
select name from student where name like 'r%';

11. Display info of male.


select * from student where gender='male';
12. Display info of female.
select * from student where gender = 'fmale';

13. Display name which last leter is e.


insert into student values (89,"trame",11, 456, "sda", "fmale" );
select name from student where name like '%e';

14. Display name and age whose age is 19.


select name, age from student where age=19;

15. Display info of student whose mobile no. has 0 at end.


select * from student where mobile like '%0';
16. Display dis�nct age.
select dis�nct(age) from student;

17. Display age between 15 and 20.


select age from student where age between 15 and 20;
18. Display student informa�on which address is star�ng with k and
ending with u.
select * from student where address like 'k%u';

19. Display student informa�on which address is ending with u, n, a.


select * from student where address like '%u' or address like '%e' or
address like '%m';

20. Display student informa�on which name is not star�ng with a, c,


d.
select * from student where name not like "a%" and name not like "c%"
and name not like "d%";
21. Add new column marks and add check constraints marks>50.
alter table student add column marks int, add constraint stu_marks
check(marks>50);

22. Create new table teacher (�d, tname, taddress, tgender, tage).
Create table teacher (�d int primary key, tname varchar (30), taddress
varchar (30), tgender varchar (6), tage int);
23. Add constraint primary key to id, unique to name, check to
gender and default to address as ktm.
alter table teacher add constraint primary key (�d), add constraint uniq
unique(tname), add constraint check_gen check (tgender='male' or
tgender='female');
alter table teacher alter column taddress set default 'ktm';

24. Drop primary key


alter table teacher drop primary key;
25. Drop foreign key
show create table stud;

alter table stud drop constraint stud_ib�_1;


OR
Alter table stud drop foreign key stud_ib�_1;
26. Drop check constraint
show create table teacher;

Alter table teacher drop constraint check_gen;

27. Drop unique constraint


Alter table teacher drop constraint uniq;
Show create table teacher;

You might also like