0% found this document useful (0 votes)
21 views5 pages

Buoi4 SQL

The document contains SQL statements to create tables, insert data, update data, alter tables, and drop tables. The tables created include regions, rooms, types, computers, software, and installation records. Constraints are added and data is manipulated between the tables.

Uploaded by

ndtruongxh1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

Buoi4 SQL

The document contains SQL statements to create tables, insert data, update data, alter tables, and drop tables. The tables created include regions, rooms, types, computers, software, and installation records. Constraints are added and data is manipulated between the tables.

Uploaded by

ndtruongxh1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

--Tao bang Khu vuc

create table Khuvuc(


IP varchar(15) primary key,
tenKhuvuc varchar(30) not null,
tang varchar(10)
);

--Them du lieu Khu vuc


insert into Khuvuc values('130.120.80','Brin RDC','');
insert into Khuvuc values('130.120.81','Brin RDC tang 1','');
insert into Khuvuc values('130.120.82','Brin RDC tang 2','');

--Tao bang Phong


create table Phong(
MP varchar(10) primary key,
tenphong varchar(30) not null,
somay int not null,
IP varchar(15),
foreign key(IP) references Khuvuc(IP)
);

--Them du lieu Phong


insert into Phong values ('s01','Salle 1',3,'130.120.80');
insert into Phong values ('s02','Salle 2',2,'130.120.80');
insert into Phong values ('s03','Salle 3',2,'130.120.80');
insert into Phong values ('s11','Salle 11',2,'130.120.81');
insert into Phong values ('s12','Salle 12',3,'130.120.81');
insert into Phong values ('s21','Salle 21',2,'130.120.82');

insert into Phong values ('s22','Salle 22',0,'130.120.82'); -- sua ip 130.120.83 thành


130.120.82
insert into Phong values ('s23','Salle 23',0,'130.120.82'); -- sua ip 130.120.83 thành
130.120.82

--Tao bang Loai


create table Loai(
idloai varchar(10) primary key,
tenloai varchar(30) not null
);

--Them du lieu Loai


insert into Loai values ('TX','Terminal X-Window');
insert into Loai values ('UNIX','Système Unix');
insert into Loai values ('PCNT','PC Windows NT');
insert into Loai values ('PCWS','PC Windows');
insert into Loai values ('NC','Network Computer');
--Tao bang May
create table May(
idMay varchar(5) primary key,
tenmay varchar(30) not null,
IP varchar(15) not null,
ad smallint check(0<=ad and ad<=255),
idloai varchar(10) not null,
MP varchar(10) not null,
foreign key(IP) references Khuvuc(IP),
foreign key(idloai) references Loai(idloai),
foreign key(MP) references Phong(MP)
);

--Them du lieu May


insert into May values ('p1','Poste 1','130.120.80',01,'TX','s01');
insert into May values ('p2','Poste 2','130.120.80',02,'UNIX','s01');
insert into May values ('p3','Poste 3','130.120.80',03,'TX','s01');
insert into May values ('p4','Poste 4','130.120.80',04,'PCWS','s02');
insert into May values ('p5','Poste 5','130.120.80',05,'PCWS','s02');
insert into May values ('p6','Poste 6','130.120.80',06,'UNIX','s03');
insert into May values ('p7','Poste 7','130.120.80',07,'TX','s03');
insert into May values ('p8','Poste 8','130.120.81',01,'UNIX','s11');
insert into May values ('p9','Poste 9','130.120.81',02,'TX','s11');
insert into May values ('p10','Poste 10','130.120.81',03,'UNIX','s12');
insert into May values ('p11','Poste 11','130.120.82',01,'PCNT','s21');
insert into May values ('p12','Poste 12','130.120.82',02,'PCWS','s21');

--Tao bang Phan mem


create table Phanmem(
idPM varchar(10) primary key,
tenPM varchar(30) not null,
ngaymua date,
version varchar(5) not null,
idloai varchar(10) not null,
gia smallint check(gia>=0),
foreign key(idloai) references Loai(idloai)
);

--Them du lieu Phan mem


insert into Phanmem values('log1','Oracle 6','05-13-1995','6.2','UNIX',3000);
insert into Phanmem values('log2','Oracle 8','09-15-1999','8i','UNIX',5600);
insert into Phanmem values('log3','SQL Server','04-12-1998','7','PCNT',2700);
insert into Phanmem values('log4','Front Page','06-03-1997','5','PCWS',500);
insert into Phanmem values('log5','WinDev','05-12-1997','5','PCWS',750);
insert into Phanmem values('log6','SQL*Net','','2.0','UNIX',500);
insert into Phanmem values('log7','I. I. S.','04-12-2002','2','PCNT',810);
insert into Phanmem values('log8','DreamWeaver','09-21-2003','2.0','PCWS',1400);--BeOS
không có trong Loai => sua BeOS thành PCWS

--Tao bang Cai dat


create table Caidat(
id varchar(5) primary key,
idMay varchar(50) not null,
idPM varchar(10) not null,
ngaycai date default sysdate,
foreign key(idMay) references May(idMay),
foreign key(idPM) references Phanmem(idPM)
);

--Them du lieu Cai dat


insert into Caidat values ('1','p2','log1','05-15-2003');
insert into Caidat values ('2','p2','log2','09-17-2003');
insert into Caidat(id,idMay,idPM) values ('3','p4','log5');
insert into Caidat values ('4','p6','log6','05-20-2003');
insert into Caidat values ('5','p6','log1','05-20-2003');
insert into Caidat values ('6','p8','log2','05-19-2003');
insert into Caidat values ('7','p8','log6','05-20-2003');
insert into Caidat values ('8','p11','log3','04-20-2003');
insert into Caidat values ('9','p12','log4','04-20-2003');
insert into Caidat values ('10','p11','log7','04-20-2003');
insert into Caidat values ('11','p7','log7','04-01-2002');

--Cau 3
update Khuvuc set tang='0' where IP='130.120.80';
update Khuvuc set tang='1' where IP='130.120.81';
update Khuvuc set tang='2' where IP='130.120.82';

--Cau 4
update Phanmem set gia=gia-0.1*gia where idloai='PCNT';

--Cau 5
-- Thêm cOt nbLog vào bAng Máy
alter table May add nbLog smallint ;
update May set nbLog=0 where idMay='p1';
update May set nbLog=2 where idMay='p2';
update May set nbLog=0 where idMay='p3';
update May set nbLog=1 where idMay='p4';
update May set nbLog=0 where idMay='p5';
update May set nbLog=2 where idMay='p6';
update May set nbLog=1 where idMay='p7';
update May set nbLog=2 where idMay='p8';
update May set nbLog=0 where idMay='p9';
update May set nbLog=0 where idMay='p10';
update May set nbLog=2 where idMay='p11';
update May set nbLog=1 where idMay='p12';

--Thêm cOt nbInstall vào bang Phan mem


alter table Phanmem add nbInstall smallint;
update Phanmem set nbInstall=2 where idPM='log1';
update Phanmem set nbInstall=2 where idPM='log2';
update Phanmem set nbInstall=1 where idPM='log3';
update Phanmem set nbInstall=1 where idPM='log4';
update Phanmem set nbInstall=1 where idPM='log5';
update Phanmem set nbInstall=2 where idPM='log6';
update Phanmem set nbInstall=2 where idPM='log7';

-- Cau 6
create table PhanmemUNIX(
idPM varchar(10) ,
tenPM varchar(30) not null,
ngaymua date,
version varchar(5) not null
);

-- Cau 7
alter table PhanmemUNIX add primary key(idPM);

--Cau 8
alter table PhanmemUNIX add gia int;

-- Cau 9
alter table PhanmemUNIX modify version varchar(15);

--Cau 10
alter table PhanmemUNIX add unique (tenPM);

-- Cau 11
insert into PhanmemUNIX(idPM,tenPM,ngaymua,version,gia) select
idPM,tenPM,ngaymua,version,gia from Phanmem;

--Cau 12
alter table PhanmemUNIX drop column version;

--CAU 13
delete from Phanmem where gia>5000; -- Xóa không duoc vì nó có tham chieu khóa ngoai
la idloai

--Cau 14
delete from PhanmemUNIX where gia>5000; -- Xóa duoc vì nó không có tham chieu khóa
ngo?i

--Cau 15
drop table Phanmem; -- xóa không duoc vì khóa duy nhat/chính trong bang duoc tham chieu
bang khóa ngoai

-- Cau 16
drop table PhanmemUNIX; -- xóa duoc vì không tham chieu khóa ngo?i

--Cau 17
alter table May drop column nbLog;
alter table Phanmem drop column nbInstall;

You might also like