Mysql命令快速上手

一、MysqlServer安装

ubuntu安装mysql

sudo apt-get install mysql-server

登陆mysql

mysql -h 主机名 -u 用户名 -p 密码

安装完mysql【Port:3306】之后使用root登陆

mysql -u root -p

密码

二、基础命令

2.1、创建用户

grant all on sample_db.* to 自定义用户名@local identified by "自定义密码";

用户以密码在任意主机上登陆数据库sample_db,并对sample_db进行所有操作

2.2、选择数据库

登陆时指定选择操作的数据库

mysql -D 数据库名 -h 主机名 -u 用户名 -p

登陆后使用use选择数据库

use 数据库名;

2.3、数据库删除方式

delete:可以删除表或者行,是删除数据,在日志中保存可回滚

turncate table:删除表中所有数据并不存日志,只能table,删除数据

drop:删除表结构及数据

2.4、查看数据库

show databases;

2.5、查看表

show tables;

2.6、删除数据库

drop databases 数据库名;

2.7、创建数据库

create database 数据库名称;

2.8、关键字释义

UNSIGNED属性就是将数字类型无符号化

tinyint 1字节 (-128,127) (0,255) 大整数值

int     4字节 (-2 147 283 648,2 147 483 647) (0,4 294 967 295) 大整数值

primary key 主键

foreign key(学号) references 表(学号)  外键

nut null

null

check(条件)

auto_increment  自增

2.9、创建数据库表

create table 表名称(列声明);

create table students(

学号 int primary key,

姓名 varchar(10) not null,

年龄 int

);

create table course(

课程名称 varchar(10) primary key,

学分 int

);

create table selectcourse(

学号 int,

课程名称 varchar(10),

成绩 int not null check(成绩>0 and 成绩<=100),

foreign key(学号) references students(学号),

foreign key(课程名称) references course(课程名称),

primary key(学号,课程名称)

); 

2.10、创建视图

create view sc(姓名,成绩)

as(

select a.姓名,b.成绩 from student a,selectcourse b

where a.学号 = b.学号

);

2.11、操作数据库

2.11.1、增

Insert into 表名 [(列名1,列名2,列名3,...)] values(值1,值2,值3,....);

包含所有值

insert into students values(NULL, "wang", "男", 25,"12800000");

包含部分值

insert into students (name, sex, age) values("zhang","男",24);

问题:

linux插入中文失败,mysql> insert into student(学号,姓名) values(2,"海");

ERROR 1366 (HY000): Incorrect string value: '\xE6\xB5\xB7' for column '姓名' at row 1

解决:

2.11.2、删

delete from 表名称 where 删除条件;

delete from students where id=2;

删除表中所有数据

delete from students;

删除表

drop table students;

drop view sc;

给表增加一列

alter table students add 性别 varchar(5);

删除表得一列

alter table students drop column 性别;

2.11.3、改

更新数据

update 表名称 set 列名称=新值 where 更新条件;

update students set tel=default where id=5;

update students set 年龄=14 where 学号=1;

2.11.4、查

select 列名称 from 表名称[查询条件];

无条件查询

select name,age from students;

select * from students;

条件查询

select 列名称 from 表名称 where 条件;

select tel from students where name="wang" and age="16";

根据年龄倒序排序,desc降序,asc升序

select * from students order by 年龄 desc;

取年龄前10,limit 起始位置,记录数,起始值即偏移量,可不写。

select * from students order by 年龄 desc limit 0,10;

统计表中记录数

select count(*) from students;

统计学生平均年龄

select avg(年龄) from students where 学号="1";

统计不同名字的学生数(根据名字去重)

select count(distinct 姓名) from students;

select count(distinct 学号) from selectcourse;

对结果进行分组

select 学号,count(*) from selectcourse GROUP BY 学号;

使用having对分组结果进行筛选

select 学号,count(*) from selectcourse GROUP BY 学号 having 学号>2;

多表连接查询

卡氏积连接,量表元组交叉乘积

select * from students,course;

自然连接,俩表中相同属性进行等值连接

select * from students,selectcourse where students.学号=selectcourse.学号;

自身连接

select a.姓名,a.员工号 from emp a,emp b where a.员工号=b.上司员工号;

复合条件连接

where字句中有多个条件得连接操作,称为复合条件查询

select * from students,selectcourse where students.学号=selectcourse.学号 and selectcourse.成绩>70;

嵌套查询,带有in谓词得子查询

单层嵌套

select * from students where 学号 in(select 学号 from selectcourse);

多层嵌套

select * from students where 学号 in(select distinct 学号 from selectcourse where 课程名称 in(select 课程名称 from course));

多层嵌套查询+复合条件查询

select * from students where 学号 in(select distinct 学号 from selectcourse where 课程名称 in(select 课程名称 from course)) and 年龄>22;

带exists谓词得子查询

exists代表存在量词,只产生true或false

select * from students where exists(select * from selectcourse where 学号=students.学号 and 年龄=20);

集合查询,关键字UNION,参加UNION操作各数据项数目必须相同,对应项数据类型也相同

select 姓名,年龄 from students UNION select * from course;

左连接,并且去重

select students.*,stu.* from students left join stu on students.学号=stu.学号 GROUP BY students.学号

右连接

select students.*,stu.* from students right  join on students.学号=stu.学号;

内连接:相等连接和自然连接

select students.*,stu.* from students inner join stu on students.学号=stu.学号;

查询姓名以xiao开头的信息

select * from students where 姓名 like "xiao%";

select * from selectcourse where 成绩 BETWEEN 50 AND 70;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小烂云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值