he quan tri CSDL

Màu nền
Font chữ
Font size
Chiều cao dòng

3. Đặt các ràng buộc khóa chính, khóa ngoại sao cho người dùng không thể xóa được thông tin về khách hàng nếu như khách hàng đã mua một sản phẩm bất kỳ

alter table customers

add constraint pk_customers primary key(customerid)

alter table customeritem

add constraint fk_customeritem foreign key(customerid)

references customers(customerid)

4. Hiển thị tổng số tiền mà cửa hàng đã thu được từ các khách hàng trên

select sum(customeritem.quantity*price) from customeritem inner join items on

customeritem.itemid = items.itemid

5. Hiển thị tên, số tiền đã mua của người khách hàng đã trả tiền cho cửa hàng nhiều nhất

select top 1 customername,sum(ci.quantity*price) from customers c inner join customeritem ci on c.customerid = ci.customerid inner join items i on i.itemid =ci.itemid

group by customername

order by sum(ci.quantity*price) desc

6. Kiểm tra xem người khách có số điên thoại 2468888 có mua mặt hàng Tủ lạnh không? Nếu có mua hiện ra dòng chũ “Có mua”, ngược lại “Không mua”

if exists(select itemname from items inner join customeritem on items.itemid = customeritem.itemidinner join customers on customers.customerid = customeritem.customerid

where itemname = 'tu lanh' and tel = '2468888')

select 'co mua'

else

select 'khong mua'

7. Hiển thị danh sách những người mua nhiều hơn một mặt hàng.

select customername from customers

where customerid in (select customerid from customeritem

group by customerid

having count(itemid)>1)

3. Đặt các ràng buộc khóa chính, khóa ngoại sao cho người dùng không thể xóa được thông tin về khách hàng nếu như khách hàng đã mua một sản phẩm bất kỳ

alter table customers

add constraint pk_customers primary key(customerid)

alter table customeritem

add constraint fk_customeritem foreign key(customerid)

references customers(customerid)

4. Tính tổng số hàng hóa và tổng tiền còn lại trong kho (Số còn lại bằng tổng số trừ đi số đã bán)

select sum(items.quantity-customeritem.quantity) as conlai,

sum(customeritem.quantity*price) as tienlai from items inner join customeritem on

items.itemid = customeritem.itemid

5. Hiển thị danh sách 3 mặt hàng bán chạy nhất (số lượng bán nhiều nhất)

select top 3 itemname as top3 from items inner join customeritem on items.itemid = customeritem.itemid group by itemname

order by sum(customeritem.quantity) desc go

6. Hiển thị tất cả các mặt hàng mà chưa bán được một cái nào.

select itemname as chuabanduoc from items where itemid not in (select itemid from customeritem)

7. Hiển thị danh sách những người mua hàng có số lượng nhiều hơn một cái.

select customername from customers

where customerid in(select customerid from customeritem

group by customerid having sum(quantity) > 1)

Bạn đang đọc truyện trên: Truyen2U.Pro