mysql—常用命令

学习网站: https://houdunren.gitee.io/note/

学习记录如下

连接服务器

连接服务器

mysql -u root -p -P3306 -h 127.0.0.1 

u表示:用户
root表示:用户名
p表示:密码
-P3306表示:默认端口3306
-h 127.0.0.1表示:连接本地主机

连接本地数据库时可以使用默认值

mysql -u root -p 

使用-e 执行SQL语句

mysql -u root -p  mydatabasename -e"show databases;"

语句执行后,会让你输入密码,如果密码正确,就会显示show databases的结果
root 账号,mydatabasename 数据库名字,-e"show databases"显示数据库

退出连接

exit

注意 每条SQL指令以 “;” 结束,按回车键后执行该条语句。在SQL后使用 \c表示取消本条SQL,后面不需要写 ;。

数据库操作

数据库列表
可以得到当前服务器中的所有数据库。

show databases;

创建新库
下面是创建数据库 mydatabase 并设置字符集为 utf8。

create database mydatabase charset utf8;

查看数据库

show create database mydatabase ;

删除数据库

drop database mydatabase ;

为了防止删除不存在的数据库报错

drop database if exists mydatabase ;

选择数据库

use mydatabase ;

外部导入

mysql -u root -p < test.sql

连接后导入

mysql -u root -p

输入密码

source E:/test.sql

数据表操作

创建数据表

create table class(
id int primary key auto_increment,
cname varchar(30) not null,
description varchar(100) default null)
charset utf8;

创建一张class表:
字段 id 为主键自增
字段 cname 为字符串类型varchar 并不允许为 null
字段 description 为可为null 字符串
字符集为 utf8 ,如果不设置将继承数据库字符集

添加数据

insert into class (cname,description) values('kcy','a cool girl');
insert into class (cname) values('scy');

二个记录没有设置值时使用默认的null值

根据已经存在的表结构创建新表

create table class2 like class;

这种方法只复制结构,不复制数据。

复制其他表的数据

insert into class2  select * from class;

只复制批定字段

insert into class2(cname)  select cname from class;

复制表时同时复制数据

create table class2 select * from class;

下面是只复制指定字段

create table class3 (id int primary key auto_increment ,name varchar(30)) select id,cname as name from class;

临时表
临时表是用于储存临时数据表表,会在数据库连接中断时自动删除。 可以与普通表同名,优先级高于普通表 。
创建临时表,展示临时表的内容。

**create temporary table temtable SELECT * from class;
select * from temtable;**

删除临时表

**drop temporary table if exists temtable;**

当然开发中我们更喜欢将临时数据放在缓存或会话中,以上只是介绍这个mysql特性。

创建表的例子

create table stu(id int primary key auto_increment, sname char(30),class_id int default null ,age smallint default null );
insert into stu(sname,class_id,age) value('小孙',1,21),('小李',1,22),('小冰',2,33),('小红',4,12); 

增加语句

添加一条记录

insert into stu set sname = '小孔',age = 18 ,class_id = 3;

添加多条记录

insert into stu(sname,class_id,age) value('老孙',6,20),('老李',6,24),('老冰',9,31); 

删除语句

删除所有年龄小于20的同学

delete from stu where age < 20; 

删除所有年龄小在30并没有班级的同学

DELETE FROM stu WHERE class_id IS NULL;  

修改语句

将班级为2的学生改为班级3

update stu set class_id = 3 where class_id = 2;

2班年龄小于20岁的同学年龄设置为NULL

update stu set age = null where class_id = 2 and age < 20;

将年龄小于20岁的同学年龄加10岁

update stu set age = age + 10 where age<20;

查询语句

查询所有字估数据

select * from class;

查询指定字段数据

select description,cname from class;

根据条件查询(cname = “scy”)

select * from class where cname = "scy";

查询包含关键词(英语)的数据

select * from class where description like '%英语%';

合并列返回查询结果

select concat(id,cname) as 'id_cname' from class;

指定多条件查询

select * from class where id >1 and cname = "scy";

查找一班或姓张的同学

select * from stu where class_id =1 or sname like '%张%'

介绍中不包含“计算机”的班级

select* from class where description not like '%计算机%';

查询学生所在班级编号,并去除重复值

select distinct class_id from stu ;

查询年龄在20~35岁的同学

select * from stu where age between 20 and 35;

查找不在30~35岁间的同学

select * from stu where age not between 30 and 35;

查找2、3班的所有同学

select * from stu where class_id in(2,3);

查找除了1、3班的同学

select * from stu where class_id not in (1,3);

查询没有分配班级的学生姓名

select sname from stu where class_id is null;

查询已经分配班级的学生信息

select * from stu where class_id is not null;

查询结果中对没分配班级的学生显示未分配

select sname,if(class_id is null,'未分配',class_id) as class_id from stu; 
select sname,ifnull (class_id ,'未分配') as class_id from stu;

按学生年龄从大到小排序

SELECT * FROM stu order by age desc;

班级从大到小排序,相同班级的同学年龄从小到大排序

select * from stu order by class_id desc, age asc;

随机获取一名同学

select * from stu order by rand() limit 1;

最后报名的同名

select * from stu order by id desc limit 1

每二和第三报名的同学 ( Limit 是从零开始的)

select * from stu order by id asc limit 1,2;

查找2班年龄最小的同学

select * from stu where class_id =2 and age is not null order by age asc limit 1; 
以上代码结果不准确,因为可能有同年龄的同学,所以可以使用子查询操作。 
select * from stu where age = (select age from stu where class_id =2 and age is not null order by age asc limit 1)  
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值