MySQL数据库入门练习100句
2016-12-14 21:23
274 查看
目的:
学习总是需要记忆遗忘再记忆反反复复才能印入脑海。IT技术尤其如此。最好解决遗忘的方式,就是多多反复,然而捧起一本大部头从新看起显然效率低下。对于数据库入门级别的知识,看过一两遍,也就能理解主要机理了,欠缺在于代码练习。因此最近在重刷MySQL入门的同时,总结起来练习所使用的代码。用于以后的温习。某一天发现自己生疏了,花一点时间敲上一通,记忆就回来了。说明:
1 大部分的语句都非常简单,进行了少量的注释;2 代码稍有一些冗余,是为了熟练操作;
3 有意设置了一些错误,注意数据库的返回的信息;
4 所有测试在MariaDB上进行,MariaDB和MySQL几乎就是一样,但如果有错误,系手误或理解错误,望担待;
5 本文共100句代码,覆盖从数据库创建删除,表创建删除修改,键创建删除,索引创建删除的内容,后续会继续增加;
6 附《MySQL入门很简单》一书电子版,可用于配合练习,网盘地址,密码:77p4
——2016/12/14
代码
//登录和退出数据库 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\G 65: 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\G 68: 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\G 71: create table tt4(id int,age int,info varchar(50),index index4(info(10))); 72: show create table tt4\G 73: create table tt5(id int,name varchar(20),info varchar(50),index index5 (id,name)); 74: show create table tt5\G 75: explain select * from tt5 where name="nnn"\G 76: 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 //完成第一阶段练习 撒花
2016/12/24练习,排错,修订
2016/12/27练习,修订
相关文章推荐
- MySQL数据库入门练习100句
- MySQL数据库入门练习100句
- MySQL数据库入门练习200句
- MySQL数据库入门练习200句
- 英文100句打字练习更新到1.2
- 英文100句打字练习更新到1.31
- MySQL数据库入门练习200句
- 平衡树的练习
- 模数 C语言练习
- OCP 11g 第二章练习
- win32命名管道/完成端口用法练习
- java基础练习之猜数字
- 基础练习 数列特征
- oracle12c如何创建练习用户scott
- 基础练习 数列特征
- java例程练习(关于线程同步的补充)
- fileinput模块获取文件的总行数 分类: python 小练习 python Module 2013-12-04 18:19 304人阅读 评论(0) 收藏
- codevs 3119 高精度练习之大整数开根
- 算法导论第十章练习10.2-2,10.2-3,10.2-5,10.2-6,10.2-7
- C primer plus 第八章 例题练习(递归)