java SQL语句 实例二

新建数据库名为book
create database book;
2)建一张表:图书表(book)
create table book( book_id int PK,book_name varchar(20),book_price double,
        book_author varchar(20), book_desc varchar(100),tid int );
book_id (图书id,主键) book_name (图书名称) book_price (图书价格)
book_author (图书作者)  book_desc (图书描述) tid (分类id)  外键
表二:分类表(type)
create table type(type_id int PK,type_name varchar(20));
type_id (分类id)  主键 type_name (分类名称)

select * from book,type where book.tid = type.type_id;

3)两表联查,查询出所有的图书信息
select book.*,type.* from book,type where book.tid = type.type_id;
4)查询出小说分类下的所有图书信息
select book.* from book,type where book.tid = type.type_id
        and type.type_name = '小说';
5)将图书价格在10到20之间的图书名称修改为西游记
update book set book_name = '西游记' where book_price between 10 and 20;
6)查询出图书名称或者图书描述中包含有aaaa的图书信息
select * from book where (book_name like '%aaaa%' or book_desc like '%aaaa%');
7)出分类名称是名著或者作者姓张的图书信息
select book.* from book,type where book.tid = type.type_id
        and (book.book_author like '张%' or type.type_name = '名著');
8)将张三发布的图书信息删除
delete from book where book_author = '张三';
9)查询出最贵的三件图书信息
select * from book where order by book_price desc limit 0,3 ;
10)将图书作者中包含”三“或者商品价格在2元以上的图书名称修改为红楼梦
update book set book_name = '红楼梦' where (book_author like '%三%' or book_price > 2);
11)查询出图书单价在6元以上且分类名称是散文的图书信息,从第3条取,取5条
select book.* from book,type where book.tid = type.type_id
        and (book.book_price > 6 and type.type_name = '散文') limit 2,5;
12)查询出图书名称为“水浒传”的分类为名著的图书信息
select book.* from book,type where book.tid = type.type_id
        and book.book_name = '水浒传' and type.type_name = '名著';
13)将图书价格大于20或者图书作者包含“王”的图书信息删除
delete from book where (book_price > 20 or book_author like '%王%');
14)查询出图书分类属于传记,价格在5到15之间,姓李的发布的图书信息
select book.* from book,type where book.tid = type.type_id
        and (type.type_name = '传记' and  (book.book_price between 5 and 15) and book.book_author like '李%' );
15)将图书名称中第二个字是”游“或者单价小于2的图书描述改为“aaaaa”
update book set book_desc = 'aaaaa' where (book_name like '_游' or book_price < 2);
16)查询出最便宜的图书信息,从第2条取10条
select * from book where oder by book_price limit 1,10 ;
17)查询出图书分类为散文,图书价格在2到5之间,图书名称或者图书描述包含“中国你好”的最便宜的三件商品
select book.* from book,type where book.tid = type.type_id
        and type.type_name = '散文' and book.book_price between 2 and 5
        and (book.book_name like '%中国你好%' or book.book_desc like '%中国你好%') order by book.book_price like 0,3;
18)将表数据清空
delete * from book;

你可能感兴趣的:(java SQL语句 实例二)