某宾馆其关系模式如下:
Room(房间编号,房间类型,价格)
Customer(顾客编号,顾客姓名,年龄,电话)
RC(房间号,顾客编号,入住日期,入住天数)
1 create database Hotel DEFAULT CHARSET 'utf8'; 2 use Hotel; 3 create table Room( 4 r_id varchar(3) primary key, 5 r_type varchar(10), 6 r_price int 7 ); 8 create table Customer( 9 c_id varchar(6) , 10 c_name varchar(10), 11 c_sex varchar(2), 12 c_phone varchar(7) 13 ); 14 create table RC( 15 r_id varchar(3), 16 c_id varchar(6), 17 rcdate date, 18 rcday int 19 );
试用SQL语句实现下列操作
1. 向Customer表插入一条新记录:顾客编号为011319,姓名为张三,电话为9000186。
insert into Customer(c_id,c_name,c_phone) values('011319','张三','9000186');
2. 查询没有使用过的房间的编号。
select r_id from Room where r_id not in(select r_id from RC);
3.查询入住天数大于2天的顾客姓名和电话。
select c_phone,c_name from Customer,RC where Customer.c_id=RC.c_id and datediff(curdate(),rcdate)>2;
4.建立视图VW:2010年1月1日后入住的所有顾客的姓名,房间类别,入住天数。
create view rc1(c_name,r_type,rcday) as select c_name,r_type,(select datediff(curdate(),rcdate)as rcday )from Room,Customer,RC where Room.r_id=RC.r_id and Customer.c_id=RC.c_id and rcdate>'2010-01-01';
5.查询累计入住天数超过2天的顾客编号,累计入住天数。
select c_id ,sum(rcday) as sumdays from RC where rcday=(select datediff(curdate(),rcdate)as rcday from RC) having sum(rcday)>2;