MySQL入门命令(1)

本文详细介绍了MySQL的基本操作,包括查看数据库、创建与删除数据库、切换与查看当前数据库、建表与修改表结构、数据的增删改查以及查询条件的使用。涵盖了如SELECT、INSERT、UPDATE、DELETE等核心命令,是MySQL初学者的实用教程。
摘要由CSDN通过智能技术生成

基础命令

查看服务器下的所有数据库

show database;

创建数据库

create database 数据库名

删除数据库

Drop database

查看当前使用的数据库

select database();

进入指定的数据库

use 数据库名

创建表

create table 表名(表的所有列的定义和约束);
> 例子:
> create table books(
> 	bid int primary key,
> 	title varchar(80) unique not null,
> 	author varchar(20),
> 	price float not null,
> 	publisher varchar(100) default '人民教育出版社',
> 	stocks int not null,
> 	publishdate date
> );

查看表的详细结构

alter table 表名 修改操作;
例子:
alter table 表名 add column 表名 varchar(1000);	#修改表:增加一列
alter table 表名 drop column 表名;	#修改表:删除一列
alter table 表名  column 表名 varchar(50) not null;	修改表:修改列、重命名

查看表的详细结构

desc 表名;

查看当前数据库下的所有表

Show tables;

删除表结构(如果有主外键关系,只能先删除外键表)

drop table 表名;
drop table 表名;

增、删、改、查(*)

增:insert

语法——insert into 表名 values();

-- 向books表中插入一条图书信息的数据
insert into books (bid, title, bauthor, price, publisher, stocks, publishdate) 
	values (10001, '三国演义', '罗贯中', 288.5, '清华大学出版社', 7, '2020-9-30' );

-- 向books表中插入一条图书信息的数据 
insert into books values (10002, '西游记', '吴承恩', 68.5, '吉林机械出版社', 27, null);

-- 向books表中插入多条图书信息的数据 
insert into books values 
	(10003, '苏菲的世界', '乔斯坦贾德', 89.5, default, 13, null),
	(10004, '钢铁是怎样炼成的', '施耐庵', 237.5, '四川出版社', 13, null),
	(10005, '跟老贾学SQL', '贾老师', 10.5, null, 13, null);

select * from books --查看boos表

删:delete

语法——delete from 表名[where条件]

例子:
删除列名中xxx的信息
delete from 表名 where 列名1 = 'XXX' 
删除列名中大于等于200的信息
delete from 表名 where 列名2 >= 200 
删除表
delete from 表名;

改:update

语法——updata 表名 set 列名 = 值,列名2 = 值 where 条件

例子:
修改列名1为xxx列名2的数值,在原来的数值基础之上加10
update 表名 set 列名2 = 列名2 + 10 where 列名1 = 'xxx'

查:select

语法——select 列名列表 from 表名 【where 条件】

列子:
select * from 表名; -- 查看所有列 
select 列名, 列名, 列名 from 表名; -- 查看指定的列 
或者可以在from 表名后跟where 子句,来完成行的筛选
关系运算符:=!=>>=<<= 
例子:
select * from 表名 where 列名 = 'xxx';  --等于
select * from 表名 where 列名 != 'xxx'; 	--不等于
select * from 表名 where 列名 > 150; 	--小于
select * from 表名 where 列名 >= 150; 	--小于或等于
select * from 表名 where 列名 < 150; 	--大于
select * from 表名 where 列名 <= 150; 	--大于或等于

关系运算符:in(一组值)、not in(一组值)
例子:
select * from 表名 where 列名 in ('XXX', 'XXX', 'XXX'); #查看“xxx”、“xxx”、“xxx”这个三个
select * from 表名 where 列名 not in ('XXX', 'XXX', 'XXX'); #查看不在“xxx”、“xxx”、“xxx”这个三个的数据

关系运算符:is null(为空)、is not null(不为空)
列子:
select * from 表名 where publisher is null; -- 查看出版社为空的图书信息 
select * from 表名 where publisher is not null; -- 查看出版社不为空的图书信息

关系运算符:between 低值 and 高值(用于筛选在一个范围之间的数据)
例子:
select * from 表名 where 列名 between 150 and 300; --查看值在150到300之间的数据
select * from 表名 where 列名 between '2020-1-1' and '2020-12-31'; --查看在2020/1/1到2020/12/31日期之间的数据

通配符:使用【like ‘通配符的字符’】来进行模糊匹配
_ 匹配任意的1个字符 (使用\_取消通配的含义,只代表字符本身) 
% 匹配任意的N个字符(0到任意多个)(使用\%取消通配的含义,只代表字符本 身)
例子:
select * from 表名 where 列名 like 'xx%'; 
select * from 表名 where 列名 like 'xx_'; 
select * from 表名 where 列名 like 'xx__'; 
select * from 表名 where 列名 like '%xx%'; 
select * from 表名 where 列名 like '%xx%';
select * from 表名 where 列名 like 'xx\%%'; 

逻辑运算符:and(并且)、or(或者)、not(非)
例子:
查看xxx的,不到100的信息
select * from 表名 where 列名 = 'xxx' and price < 100; 
select * from 表名 where (列名 = 'xxx') and (price < 100);

查找xxx的,或者不到100的信息
select * from 表名 where 列名 = 'xxx' or price < 100; 

查找列名1xxx,或者列名2低于100且列名3高于10本的信息(and的优先级高于orselect * from 表名 where 列名1 = 'xxx' or 列名2 < 100 and 列名3 >= 10; 
select * from 表名 where 列名1 = 'xxx' or (列名2 < 100 and 列名3 >= 10);
 
查找列名2高于10的, 列名1xxx的或者列名1低于100的(and的优先级高于orselect * from 表名 where 列名2 >= 10 and (列名1 = 'xxx' or 列名3 < 100);

查找不是XXX的数据
select * from 表名 列名 not bauthor = 'xxx'; 
select * from 表名 列名 bauthor != 'xxx';
分组统计(聚合)函数:对查询结构中,某列的数据,进行某种聚合统计
——计数(count)、求和(sum)、求平均值(avg)、求最大值(max)、求最小值(min)
例子:
查看列名总数
select count(*) from 表名;
查看列名的总数、列名1的总和、列名2的平均值、列名3的最大值、列名4的最小值
select count(*),sum(列名1),avg(列名2),max(列名3),min(列名4) from 表名;
不考虑列表5为空的列名,将其他的列表信息进行统计
select count(*),sum(列名1),avg(列名2),max(列名3),min(列名4) from 表名 where 列名5 is not null;

分组子句【在条件where子句之后(如果有),使用 group by 分组的列,按指定列来分组,分组后,select后,只跟分组的列名,或者分组函数
例子:
按ID来分组,统计ID下数据的数量
select 列表1 ID,count(*) 数量 from 表名 group by 列名1
不考虑XXX下列表的数据,其他的数按ID来分组,统计ID下数据的数量
select 列表1 ID,count(*) 数量 from 表名 where 列名1 !='xxx' group by 列名1
排序:asc(升序)、desc(降序)、limit(限制最后显示出来查询结果的行数)、limit m, n(查看从第m条【从0开始计算的】开始,最多查看n条数据)
例子:
select * from 表名 order by 列名 asc; -- 升序 
select * from 表名 order by 列名; -- 升序 
select * from 表名 order by 列名 desc; -- 降序
先以列表1为主降序排列,列表1相同的信息,再按列名2降序
select * from 表名 order by 列名1 desc, 列名2 desc;
去除列表1中xxx的数据,查其他的信息,按列名2降序
select * from 表名 where 列名1 != '贾老师' order by 列名2 desc;
查看表的前5select * from 表名 limit 5; 
查看列表15的信息
select * from 表名 order by 列名1 desc limit 5; 
查看列表148的这5组信息
select * from 表名 order by 列名1 desc limit 3, 5;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值