views - SQL update table via join? -
i have following database , wish update room
table. table room
lists room type either single, double or king, price price of each room per night , name in hotel name of hotel.
what need change double rooms king rooms in scotty hotel , increase price 10%.
i know how update price , type when in single table, seems here need join hotel , room on hno , update. nested query maybe?
create table hotel ( hno char(4), name varchar(20) not null, address varchar(50), constraint pk_hotel primary key (hno) ); create table room ( rno char(4), hno char(4), type char(6) not null, price decimal (7,2), constraint pk_room primary key (hno, rno), constraint fk_room foreign key (hno) references hotel (hno) ); create table guest ( gno char(4), name varchar(20) not null, address varchar(50), constraint pk_guest primary key (gno) ); create table booking ( hno char(4), gno char(4), datefrom date, dateto date, rno char(4), constraint pk_booking primary key (hno, gno, datefrom), constraint fk_booking foreign key (gno) references guest (gno), constraint fk_booking_room foreign key (hno, rno) references room (hno, rno), constraint fk_booking_hotel foreign key (hno) references hotel (hno) );
i have 2 main questions:
is possible update table, requires join?
i want list guests via view. can create view contains hotel name , number of distinct guests have ever stayed in hotel?
first, idea select ensure search/filter criteria correct:
select h.name, r.rno, r.type, r.price room r inner join hotel h on h.hno = r.hno h.name = 'scotty hotel' , r.type = 'double' ;
if targets correct rows, have @ 'em update query using same filter criteria, follows.
hint, pays copy entire select
query , edit form new update
query. eg mysql, replace from
update
(and leave contents of original from
clause form new update
clause), leave where
in place, , use contents of select
list original query basis on form new set
clause (immediately prior where
clause).
this code mysql:
update room r inner join hotel h on h.hno = r.hno set r.type = 'king', r.price = r.price * 1.1 h.name = 'scotty hotel' , r.type = 'double' ;
this code ms sql server:
update r set r.type = 'king', r.price = r.price * 1.1 room r inner join hotel h on h.hno = r.hno h.name = 'scotty hotel' , r.type = 'double' ;
Comments
Post a Comment