MySQL命令简介

1、对库的操作

 
mysql -u root -p #登录
show databases; #显示数据库
create database <数据库名称>; #创建数据库
drop database <数据库名称>; #删除数据库
select database(); #查看当前所在的数据库
show tables from mysql; #查看数据的表


2、对表的操作


表操作


describe <数据表名称>;
create table course (couid char(4), couname char(10), teachername char(3) ); 表新增
alter table stu1 rename stu2;#修改表
delete from stu1 #删除表
truncate stu1;#清空表的数据
注释 truncate和delete from+表名清空表的区别
delete from+表名清空表数据只是把表中的数据全部删除,还会保留主键自增的位置,比如当前表中最后一条数据的id为20,清空表之后,再添加1条数据,那么新增的那条数据的id为21.而如果用truncate清空表,新增数据的id会从1开始。
create table stu(id int primary key auto_increment,name char(10) not null default '',sex enum('男','女') not null default '男',cid tinyint unsigned not null default 1)
解析
①、最好每一张表都设主键,根据表可能要存储的数据量来选择尽可能小的数据类型。主键自增。其他字段能用数值类型的不要用字符串类型。int后如果不限定长度,不限定非负,默认为int(11);tinyint默认为tinyint(3)
②、性别字段用enum类型,enum后面的括号内是字符串,要加引号,但是MySQL处理enum是按数值类型储存的,所以比直接用字符串要快。
③、尽量不要有NULL值,text类型(文字内容字段的类型比如 BLOB, TEXT, GEOMETRY or JSON)不能有默认值
drop table dep;#表删除
alter table t1 rename t2;#表修改


行操作


create table course (couid char(4), couname char(10), teachername char(3) );

INSERT INTO `course` VALUES ('C003', 'XML', '殷惠'); #行新增
insert into stu (name,sex,cid) value ('小刚','男',1),('Lucy','女',3),('李雷','男',1),('Lily','女',2); #多行插
delete from course where teachername="N"; #行删除
delete from stu2 where id in (1,2,18);#多行删除
update course set teachername="lili",couname="xin" where couid="A001"; #行更改
select * from course order by primary_key#默认升序查询
select * from course order by primary_key desc;#降序查询
select * from course order by primary_key asc;#升序查询
select * from course where  teachername='nul'; #where不区分大小写
select * from course where  teachername='nul'; #where 区分大小写
select * from course where primary_key=1 and primary_key=3;
select * from course where primary_key=1 or primary_key=3; #and or的用法

列操作


alter table course add column new1 varchar(20) default null; #列新增
alter table course change couid coid integer; #列重命名
alter table course change coid couid int(7) not null; #列修改
alter table course modify new1 varchar(30); #列修改
alter table user drop column new2;  #列删除
alter table user change new1 new4 int; #列名称修改
alter table course add primary_key int(5) unsigned  not null auto_increment,add primary key (primary_key); #主键添加
alter table 表名 drop primary key;#主键删除
注意:删除主键之前必须先要删除主键的自增,否则会报错。
alter table course add index index_name (couid,new4); #索引添加
alter table course drop index index_name; #索引删除
show index from information_student.course; ##查看索引
explain select * from course where primary_key=1; #查看索引

3、复制操作


create table stu1 like stu;#复制表结构
insert into stu1 select * from stu;#复制表数据
create table stu2 select * from stu;#一起复制但会丢失主键属性
所以需要加上
alter table stu2 add primary key(id)
alter table stu2 modify id int unsigned auto_increment;

4、扩展

select * from scores where score>70 group by term having avg(score)>80 order by score desc;

2dbc9d81ec1d43bc8153f729505d7eba.png

 

解析:首先筛选成绩大于70-按term分组-求score平局值大于80-降序排列


select cid,sum(if(score>=60,1,0)) 及格,sum(if(score<60,1,0)) 不及格 from student group by cid; #sum统计

合并查询

涉及的命令:select book_id,title,pub_year, concat  (author_first," ",author_last) as author from books join authors using (author_id) where author_id=1;

bec1fb9ee9934cffa1f1750f7be28480.png

以这两张表为例:

8ce2648b43404cc78c3c86be558ccee2.png

 

内连接 涉及命令:select * from books join authors on books.author_id=authors.author_id;

4f5f9eb5ee7f4cd3bc64a4bbd41cc71d.png

左连接 涉及命令:select * from man left join woman on man.hid=woman.hid;

 

22e670d6fa504deb81e90f15e98b0fbc.png

右连接 涉及命令

28ca430837ba4a0e900c6aa8a812b950.png

 

数据拼接 涉及命令:select * from books union all select * from authors;

082e030028fa4c4aa555146738f7b2a5.png

数据有序拼接:select books.title,authors.country as title_country from books,authors;

43d7e9eb24de4624aa6ec56158ae7139.png 

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值