数据库命令练习

对于数据库命令的练习 -用来快速巩固自己学到的数据库知识

//登录和退出数据库
1:mysql -u root -p//输入密码
2: exit
3: mysql -u root -p

//数据库创建,删除,进入数据库
4:CREATE DATABASE `testdatabase`;5: CREATE DATABASE `testdatabase`;6:DROP DATABASE `testdatabase`;7: CREATE DATABASE `testdatabase`;8: USE testdatabase;
9: CREATE DATABASE `testdatabase2`;10: USE DATABASE testdatabase2;
11: USE `testdatabase2`;
12: USE `testdatabase`;
13: DROP DATABASE `testdatabase2`;//表的创建和删除,创建表同时设置属性
14: CREATE TABLE table1;15: CREATE TABLE table1(No INT);16: CREATE TABLE table2 (No INT,name VARCHAR(20),sex boolean);17: DROP TABLE table1,table2;18: CREATE TABLE tt1 (id INT(11) primary key);19: CREATE TABLE tt2 (id int primary key, name varchar(20) unique, age int not null, sex boolean default '1');
    //boolean 实际上是一个int,因此值为0或1
20:CREATE TABLE tt3 (id int primary key, age int not null auto_increment);21: CREATE TABLE tt3 (id int primary key auto_increment, age int not null);
    //一个表只能有一个auto_increment约束,且约束的属性必须是主键或主键的一部分
22:show tables;23:drop table tt1,tt2,tt3;show tables;//key键:
    //主键:
24:create table tt1 (id int primary key);25: create table tt2 (id int,name varchar(20),primary key (id));26: create table tt3 (id int,name varchar(20),age int,primary key(id,name));27: create table tt4(id int primary key,age int primary key);//只可以有一个主键
28: drop table tt1,tt2,tt3,tt4;

    //外键:
29: create table tt1(id int(4),uid int(4));30: create table tt2(id int(4),uid int(4),constraint nickname foreign key(uid) references tt1(uid));31: drop table tt1;
    create table tt1(id int,uid int,primary key(id,uid));32: create table tt2(id int,uid int,constraint nickname foreign key(uid) references tt1(id));//由上四条可知外键必须依赖于父表的主键上
33:create table tt2(id int(4),uid int(4),constraint nickname foreign key(uid) references tt1(id));
    //外键连接必须连接的是主表的主键,如果连接主表主键的一部分,必须连接主键的第一个字段
34: create table tt3(id int,uid int,constraint nick name foreign key(id) references tt1(id));35: drop table tt1,tt2,tt3;//此处会报错,因为有外键依赖要先删子表才能删父表,如果输入drop table tt3,tt2,tt1;则不会报错

36: create table tt1(Id int unsigned,uid int,primary key(id));37: create table tt2(id int,uid int,constraint nickname foreign key (uid) references tt1(id));38: create table tt2(id int,uid int unsigned,constraint foreign key (uid) references tt1(id));
    //外键关联的主从表字段数据类型必须完全一样,否则报错“foreign key constraint is incorrectly formed”


//查看表:
39:describe tt1;
    describe tt2;
40: desc tt1;
    desc tt2;
41: show create table tt1;
    show create table tt2;42: show create table tt1 \G 
    show create table tt2 \G

//修改表:
    //修改表名43:alter table tt1 rename tt2;44: alter table tt1 rename temp;45: desc tt1;
    desc temp;
46: alter table temp rename to tt1;47: drop table tt1,tt2;

    //修改表属性数据类型
48: create table tt1(id int,name varchar(20));49: alter table tt1 modify id varchar(20);50: insert into tt2(id,name) values(1,'string');//插入和删除操作还没有涉及,在这里只是为了加入一条数据已说明下面的问题
51:alter table tt1 modify name int;//有值的情况下转换字段类型失败,因为字符串类型无法自动转型为整数类型
52:delete from tt1;//删除表中元素53: alter table tt1 modify name int;
    desc tt1;


    //修改表属性
54:alter table tt1 change name newname int; 
    desc tt1;
55: alter table tt2 change newname name varchar(20);
    desc tt2;

    //增加字段
56: alter table tt1 add name tinytext after id;
    desc tt1;
57: alter table tt1 add age int not null after id;desc tt1;
58: alter table tt1 add no int unique first;desc tt1;

    //删除字段
59:alter table tt1 drop age;desc tt1;desc tt1;

    //修改属性排列顺序
60:alter table tt1 modify no int after name;desc tt1;
61: alter table tt1 modify no int first;desc tt1;
62: drop table tt1,tt2;//索引:
    //创建带索引的数据库表
63:create table tt1(id int,name varchar(20),age int,index index1(id));64: show create table tt1\G65: explain select * from tt1 where id=1;//explain语句用来查看索引详细情况,参考博文:http://blog.csdn.net/zhuxineli/article/details/14455029
66:create table tt2(id int,name varchar(20),age int,unique index index2(id));//唯一索引,唯一索引指的是被索引的字段值唯一
67: show create table tt2\G68: create table tt3(id int,name varchar(20),age int,fulltext index index3(id));//全文索引
69: create table tt3(id int,name varchar(20),age int,fulltext index index3(name));70: show create table tt3\G71: create table tt4(id int,age int,info varchar(50),index index4(info(10)));72: show create table tt4\G73: create table tt5(id int,name varchar(20),info varchar(50),index index5 (id,name));74: show create table tt5\G75: explain select * from tt5 where name="nnn"\G76: explain select * from tt5 where id='1'\G

    //在已存在表上添加索引
//create [unique|fulltext|spatial] index indexname on tablename (columnname [(length)] [ASC|DESC]);77: drop database testdatabase;
    create database testdatabase;
    use testdatabase;
78: create table tt1(id int,name varchar(20));79: create index index1 on tt1 (id);80: create index index1 on tt1 (name);//同一表的索引别名不能相同
81: create index index2 on tt1 (name);82: drop index index2 on tt1;83: create table tt2(id int,name varchar(20),age int,sex boolean,info varchar(50));84: create index index1 on tt2 (id);//不同表的索引别名可以相同
85:create unique index index2 on tt2 (id);//不同索引可以重复使用相同字段
86: create fulltext index index3 on tt2 (name);87: create index index4 on tt2 (sex,age);//多字段索引
88:show create table tt2;89: drop table tt1,tt2;90: show tables;

    //Alter语句添加索引
    //Alter table tablename add [unique|fulltext|spatial] index indexname (columename [(length)] [ASC|DESC]);91: create table tt1(id int);92: alter table tt1 add index index1 (id);93: create table tt2 (id int,age int,name varchar(20),sex boolean, info varchar(50));94:alter table tt2 add index index2 (id,name);95: alter table tt2 add unique index index3 (sex);96: alter table tt2 add index index4 (info(10));97: alter table tt2 add fulltext index index5 (info(10));
    show create table tt2;

    //删除索引
98: drop index index1 on tt2;
    show create table tt2;//观察删除索引后的表信息
99:drop table tt1,tt2;
100: exit
//创建数据库
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 between 2 and 5;130, select * from tt1 where id not between 2 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 limit 2;153, select * from tt1 limit 3;154, select * from tt1 limit 0,2;155, select * from tt1 limit 1,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.id and 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 subject as su,score as 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]";


猜你喜欢

转载自blog.51cto.com/meyangyang/2160143