Вы находитесь на странице: 1из 10

DROP:- To drop something from the table or to delete something from the table.

INPUT
drop Primary key
select * from sir

OUTPUT

AIM: to study FOREIGN KEY.


A foreign key is a key used to link two tables together. This is sometimes also called as a
referencing key.A Foreign Key is a column or a combination of columns whose values
match a Primary Key in a different table.
The relationship between 2 tables matches the Primary Key in one of the tables with a
Foreign Key in the second table.
If a table has a primary key defined on any field(s), then you cannot have two records
having the same value of that field(s).

EXECUTION CODE:
create table akanksha000(id int,name varchar(20),roll_no varchar(22),
constraint key1 primary key(name))
desc akanksha000
create table foreign441(f_id int, name varchar(20) primary key ,p_name varchar(20),
roll1_no varchar(21),
constraint nrewkey_tyu Foreign key(name)
References akanksha000(name) );
insert into akanksha000(id, name,roll_no)
values(1,’AKANKSHA','01')
insert into akanksha000(id, name, roll_no)
values(2,’TANISHA','02')
select * from akanksha000

OUTPUT

insert into foreign441(f_id,name,p_name,roll1_no)


values(1,’AKANKSHA','abcd','32')
select * from FOREIGN441;

OUTPUT

LAB ASSIGNMENT 5
AIM: To study ‘ORDER BY’.
The SQL ORDER BY clause is used to sort the data in ascending or descending order,
based on one or more columns. Some databases sort the query results in an ascending
order by default.

SYNTAX: The basic syntax of the ORDER BY clause is as follows −


SELECT column-list
FROM table_name
[WHERE condition]
[ORDER BY column1, column2, .. columnN] [ASC | DESC];

EXECUTION CODE:
create table hellooo(
name varchar(20),
section varchar(10),
roll_no int)

desc hellooo

insert into hellooo values('Ayan','A',12);


insert into hellooo values('Aman','B',15);
insert into hellooo values('Riya','C',1);
insert into hellooo values('Maria','D',30);
select * from hellooo

OUTPUT

1. SORTING IN ACSENDING ORDER:


select name,section,roll_no
from hellooo
order by roll_no asc
OUTPUT

2. SORTING IN DESCENDING ORDER:

select name,section,roll_no
from hellooo
order by name desc

OUTPUT

select name,section,roll_no
from hellooo
order by roll_no desc

OUTPUT

3. SORTING ROWS WITH NULL VALUES:


desc hellooo
select * from hellooo
select name,section,roll_no
from hellooo
order by section ASC NULLS FIRST
OUTPUT

AIM: to study UNIQUE constraint.

The UNIQUE constraint ensures that all values in a column are different.

Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness
for a column or set of columns.A PRIMARY KEY constraint automatically has a
UNIQUE constraint. However, you can have many UNIQUE constraints per table, but
only one PRIMARY KEY constraint per table.

EXECUTION CODE:

1.CREATE CONSTRAINT

create table school


(
s_id varchar(20) not null,
s_name varchar(20),
contact varchar(30),
constraint s_unique
unique(s_id)
)

desc school
insert into school values('6','kunal','234')
insert into school values('5','maya','67')
insert into school values('14','sunil','79')

select * from school


insert into school values('6','aashish','90')

OUTPUT

2. ALTER CONSTRAINT
alter table school
add constraint sl_unique
unique (s_name,contact)
insert into school values('0','kunal12','4')
insert into school values('50','maya12','7')
insert into school values('1','sunil12','9')
select * from school

OUTPUT

alter table school


disable constraint
s_unique

OUTPUT
3. ENABLE CONSTRAINT
alter table school
enable constraint
s_unique

OUTPUT

3. DROP CONSTRAINT
alter table school
drop constraint s_unique

OUTPUT

AIM: to study CHECK constraint.

The CHECK constraint is used to limit the value range that can be placed in a column.If
you define a CHECK constraint on a single column it allows only certain values for this
column.

If you define a CHECK constraint on a table it can limit the values in certain columns
based on values in other columns in the row.

EXECUTION CODE:

CREATE CHECK CONSTRAINT


create table college44
(
c_name varchar(20),
c_id int,
contact int,
constraint c check(c_id between 1 and 10)
);
insert into college44 values('tina',34,657)
OUTPUT

insert into college44 values('meena',4,23)


insert into college44 values('sunny',1,345)
select * from college44
OUTPUT

LAB ASSIGNMENT 6

AIM: To study JOINS in DBMS.


As the name shows, JOIN means to combine something. In case of SQL, JOIN means "to
combine two or more tables".In SQL, JOIN clause is used to combine the records from
two or more tables in a database.
Types of SQL JOIN
1. INNER JOIN
2. LEFT JOIN
3. RIGHT JOIN
4. FULL JOIN
EXECUTION CODE:

create table veg


(name varchar(20),
price int )
insert into veg values('onion',40)
insert into veg values('tomato',30)
select * from veg
OUTPUT

create table fruit


(name varchar(20),
price int )
insert into fruit values('apple',80)
insert into fruit values('orange',30)
select * from fruit

OUTPUT

select fruit.name,veg.price
from fruit inner join veg
on fruit.price=veg.price

OUTPUT

select fruit.price,veg.name
from fruit inner join veg
on fruit.price=veg.price

OUTPUT

Вам также может понравиться