MySQL数据库入门练习200句

原生SQL语句大集合

SQL数据库入门练习200句

//重建数据库

101, create database testdatabase;use database testdatabase;

102, create table tt1(id int, name varchar(20),age int,sex boolean);

103, show tables;desc tt1;

//插入

104, insert into tt1 values(1,"zhang",25,0);

105, insert into tt1 values(2,"wang",25,1);

106, select* from tt1;

107, insert into tt1(id,name,age,sex) values(3,"li",28,1);

108, insert into tt1(id,name,sex,age) values(4,"sun",0,22);

109, insert into tt1(id,name,sex) values(5,"zhao",30,1);

110, insert into tt1(id,age,name) values(6,"he",47,0);

111, insert into tt1(id,age,name) values(7,"chen",22,1),(7,"zhang",22,1),(7,"xie",32,1);

112, select* from tt1;

//修改

113, update tt1 set id=10,name="new",age=100,sex=0 where id=1; select* from tt1;

114, update tt1 set id=11,name="new" where id=2,age=25; select*from tt1;

115, update tt1 set id=12,sex=1 where id=7; select* from tt1;

116, update tt1 set sex=1 where id>3;

117, update tt1 set sex=0 where id<4;

//删除

118, delete from tt1 where id=1;select* from tt1;

119, delete from tt1 where id=12;select* from tt1;

//查询

120, alter table tt1 add address varchar(30);

121, update tt1 set address="Beijing" where sex=1;

122, update tt1 set address="Shanghai" where sex=0;

//简单查询

123, select id from tt1;

124, select id,name from tt1;

125, select id,name,address from tt1;

//条件查询

126, select id,name,address from tt1 where address="Beijing";

127, select* from tt1 where id in(2,3,4);

128, select* from tt1 where id not in(2,3,4);

129, select* from tt1 where id between2 and 5;

130, select* from tt1 where id not between2 and 5;

131, select* from tt1 where address like"beijing";

132, select* from tt1 where address like"bei";

133, select* from tt1 where address like"bei%";

134, select* from tt1 where address not like"bei%";

135, select* from tt1 where address is null;

136, select* from tt1 where address is not null;

137, select* from tt1 where age<20 and sex=1;

138, select* from tt1 where sex=0 or age>30;

//查询结果不重复

139, select distinct address from tt1;

//查询结果排序

140, select* from tt1 order by age;

141, select* from tt1 order by age asc;

142, select* from tt1 order by age desc;

//分组查询

143, select* from tt1 group by sex;//单独使用group by 只会选择每个分组的一条记录

//group by 与 group_concat结合使用

144, select group_concat(name),sex from tt1 group by sex;

145, select group_concat(name),group_concat(age),sex from tt1 group by sex;

146, select group_concat(name,age),sex from tt1 group by sex;

//group by与集合函数结合使用

147, select sex,count(sex) from tt1 group by sex;

148, select sex,count(sex) from tt1 group by sex having sex>2;//having用于对分组后的结果加限制条件

149, select sex,count(sex) from tt1 group by sex having count(sex)>2;

//with rollup

150, select sex,count(sex) from tt1 group by sex with rollup;//在显示记录的最后一行加一条,记录是上面所有记录的和,通常用于合计数量

//limit显示限制

151, select* from tt1;

152, select* from tt1 limit2;

153, select* from tt1 limit3;

154, select* from tt1 limit0,2;

155, select* from tt1 limit1,2;

//使用集合函数查询

//为了更好的理解本问题,新建一个表

156, create table grade(id int,name varchar(10),subject varchar(10),score int,sex boolean);

157, insert into grade values(1,"wang","math",100,1),(1,"wang","english",96,1),(1,"wang","physics",90,1);

    insert into grade values(2,"li","math",96,1),(2,"li","english",85,1),(2,"li","physics",99,1);

    insert into grade values(3,"sun","math",85,0),(3,"sun","english",98,0),(3,"sun","physics",80,0);

158, select* from grade;

159, select count(*) from grade;

160, select id,name,sum(score) from grade where id=1;

161, select id,name,sun(score) from grade group by id;

162, select id,name,sum(score) from grade group by id order by sum(score) desc;

163, select id,name,avg(score) from grade where id=2;

164, select id,name,avg(score),sum(score) from grade where id=3;

165, select id,name,avg(score) from grade group by id;

166, select subject,avg(score) from grade group by subject;* from

167, select subject,avg(score) from grade group by subject order by avg(score);

168, select name,max(score) from grade where subject="math";

169, select name,max(score) from grade where subject="english";

//连接查询

//内连接

170, create table stu(id int,name varchar(10),age int,sex boolean);

171, insert into stu values(1,"wang",25,1),(2,"li",23,1),(3,"sun",23,0),(4,"zhou",27,1),(5,"zhang",22,0);

172, select id,name,age,sex,score from stu,grade where stu.id=grade.id;

173, select stu.id,stu.name,stu.age,stu.sex,score from stu,grade where stu.id=grade.id;

174, select stu.id,stu.name,stu.age,stu.sex,score from stu,grade where stu.id=grade.idand score>90;

    //外连接

175, select stu.id,stu.name,stu.age,stu.sex,score from stu left join grade on stu.id=grade.id;

176, select stu.id,stu.name,stu.age,stu,sex,score from stu right join grade on stu.id=grade.id;

177, insert into grade values(4,"hu","math",99,1),(5,"liu","english",97,0);

178, select stu.id,stu.name,stu.age,stu.sex,subject,score from stu left join grade on stu.id=grade.id;

179, select stu.id,stu.name,stu.age,stu.sex,subject,score from stu right join grade on stu.id=grade.id;

    //子查询

180, select* from stu where id in(select*from grade);

181, select* from grade where id in(select* from stu);

182, create table scholarship(level int,money int,score int);

183, insert into scholarship values(1,10000,90),(2,5000,85),(3,3000,80);

184, select id,name,score from grade where score>=(select score from scholarship where level=1);

185, select id,name,score from grade where score>=(select min(score) from scholarship);

    //exists子查询

186, select* from stu where exists(select name from grade where id=1);

187, select* from grade where score>any(select score from scholarship);

188, select* from grade where score>all(select score from scholarship);

//合并查询结果

189, select name from stu union select name from grade;

190, select name from stu union all select name from grade;

//别名

191, select* from scholarship s where s.level=1;

192, select* from scholarship s where s.money=5000;

193, select s.name from stu s,grade g where s.name=g.name;

194, select subjectas su,scoreas sc from grade;

195, select* from stu where name regexp"^w";

196, select* from stu where name regexp"g$";

197, select* from stu where name regexp"^w.g$";

198, select* from stu where name regexp"^w..g$";

199, select* from stu where name regexp"[aeo]";

200, select* from stu where name regexp"[a-g]";

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,928评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,192评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,468评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,186评论 1 286
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,295评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,374评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,403评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,186评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,610评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,906评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,075评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,755评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,393评论 3 320
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,079评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,313评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,934评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,963评论 2 351

推荐阅读更多精彩内容

  • 观其大纲 page 01 基础知识 1 MySQL数据库概要 2 简单MySQL环境 3 数据的存储和获取 4 M...
    周少言阅读 3,156评论 0 33
  • MYSQL 基础知识 1 MySQL数据库概要 2 简单MySQL环境 3 数据的存储和获取 4 MySQL基本操...
    Kingtester阅读 7,792评论 5 116
  • 1.1、常用数据库包括:Oracle、MySQL、SQLServer、DB2、SyBase等 1.2、Navica...
    NOX_5d2b阅读 3,349评论 0 0
  • 一、数据库操作: 1.1 创建数据库: create database student; 1.2 删除数据库: ...
    __71db阅读 782评论 0 0
  • 我想,我真的是一个特别恋旧的人。 然而,不是所有的东西都会一直陪着你,所以我常常怀念。在我身边陪我最久的是小学二年...
    意中晚归了阅读 288评论 2 1