MySQL基础语法

docx复制过来已无格式

SQL的分类
	DDL:数据库定义与数据库/表结构:create drop alter
	DML:数据操纵语言,操作表数据:insert update delete
	DCL:数据控制语言,设置用户访问权限 安全
	DQL:数据查询语言:select from where
登陆数据库
mysql -u账号名 -p密码
mysql -uroot -proot
中间不要加空格,末尾也不要加;会被误认为账号名Huon密码的一部分
数据库的创建
单纯创建数据库
create database 数据库名字;
create database a;
创建数据库并指定字符集
create database 数据库名字character set 字符集;
create database b character set utf8;
创建数据库,指定字符集并指定校对规则
create database 数据库名字character set 字符集collate 校对规则;
create database c character set utf8 collate utf8_bin;

数据库的查看
查看所有的数据库
show databases;
注意: information_schema 
	 performance_schema
	 mysql 
这3个相关信息不要动,如果删除的话可能导致自己的用户信息也删除,关闭cmd窗口后就再也登不上去了
	 test这是一个默认的数据库,一开始里面没有任何东西,删除也没关系
查看数据库定义的语句
show create database 数据库名字;
show create database c;
查看当前正在使用的数据库
select database();
数据库的修改
修改数据库的字符集
alter database 数据库名字 character set 字符集;
alter database c character set gbk;
数据库的删除
drop database 数据库名字;
drop database c;

切换数据库
use 数据库名字;
use a;
创建表格
create table 表名(
	列名1  列的类型(长度)  约束,
列名2  列的类型(长度)  约束,
);
create table student(
	sid int primary key,
	sname varchar(31),
age int
);

约束类型	作用
primary key	主键约束
unique	唯一约束
not null	非空约束
数据类型	Java	mysql
整形	int	int
字符	char/string	Char/varchar
布尔	boolean	boolean
浮点	float/double	float/double
时间	date	date/time/datetime/timestamp
存放文本		text
存放二进制		blob

类型	格式	默认值
date	YY-MM-DD	
time	hh : mm : ss	
datetime	YY-MM-DD  hh : mm : ss	null
timestamp	YY-MM-DD  hh : mm : ss	当前时间

特别注意:指定大小值得是字符个数,而非字节个数
		cha(3)如果存入一个a,这实际存储的是  a空格空格
		varchar(3)如果存入一个a,这里实际存储的是 a
查看表
	查看所有的表(show)
		show tables;
查看表的创建过程(show create)
		show create table 表的名字;
show create table student;
查看表的结构(desc)
		desc 表的名字;
		desc student;
表中列的操作(alter)
	添加列(add)
		alter table 表名 add 列名 列的类型 类的约束;
		alter table student add schengji int not null;
修改列(modify)
		alter table 表名modify 列名 列的类型(长度) 约束;
		alter table student modify sid varchar(2) not null;
修改列名(change)
alter table 表名change 旧列名 新列名 列的类型(长度) 约束;
		alter table student change sid snon varchar(2) not null;
删除列(drop)
		alter table student drop 列名;
		alter table student drop snon;
修改表名(rename)
		rename table 旧表名to 新表名;
		rename table student to xuesheng;
修改的表的字符集(character set)
		alter table 表名character set 字符集;
		alter table xuesheng character set gbk;
删除表(drop)
		drop table 表名;
		drop table xuesheng; 
表中数据的操作
	插入数据(insert)
		insert into 表名 (列名1,列名2,列名3) values(值1,值2,值3);
insert into student(sid,sname,age) values(001,'aaa',0);
一一对应所在表格的列, 其他没有时为空,不允许为空时为默认值
insert into 表名 values(值1,值2,值3);
insert into student values(002,'bbbb',1);
按表格列的顺序插入数据,不能缺任何一个,缺少任何一个每填都会报错
批量插入数据
insert into 表名 values(值1,值2,值3), (值1,值2,值3), (值1,值2,值3);
insert into student values(006,'bbbb',1),(007,'bbbbb',1),(008,'bbbbbbb',1);
查看表格数据(select)
		select * from 表名;
	插入数据乱码问题
在插入数据时,如果我们插入中文的话我们会发现乱码报错,这是因为我们在cmd的操作面板上用的是gbk,数据库服务器用的也是gbk,但是传输到服务起的过程中用的确实utf-8,所以导致乱码
			解决方法:修改mu.ini配置(在安装位置里),
1.	暂停MySQL服务
2.	在安装路径哪里找到my.in的配置文件
把57行的utf8改成gbk
删除表数据(delete)
			delete from表名
delete from student;
			一条条的删除表中的所有数据
			delete from 表名 where条件
delete from student where age=0;
			一条条删除满足条件的数据

truncated table 表名;
truncated table student;
注意:delete删除数据于truncate删除数据的区别:
	delete:一条条删除表的数据
	truncated:先删除表在重建表
	如果数据较少,delete更高效
	如果数据较多,truncated更高效
修改数据(update)
			update 表名 set 列名1=值1,列名2=值2
			update student set age=2;
			修改所有的指定列数据
			update 表名 set 列名1=值1,列名2=值2 where 条件
			update student set age=50 where sname='aaa';
			修改所有的满足条件的列数据
注意:如果为时间或字符串则需要单引号


数据查询(select)
			select [distinct] [*] [列名1,列名2] 
from 表名 
[where 条件] 
[group by 根据哪个列分组]
[having 条件过滤] 
[order by 排序];
		   	        :二选一,列出所有还是列出部分  
distinct:去除重复的数据
数据条件查询(where)
			select * from 表名where 条件;
			select * from student where sname='张三';
关系可以是大于,小于,等于和不等于,空还是非空
			不等于:<>(标准语法)和!=(非标准语法)两种
			空:is null
select * from student where sname is null;
非空:is not null
select * from student where sname is not null;
			关键字还有and,or和not三种
			select * from student where age between 10 and 100;
还有一种写法是between...and...
			select * from student where age>10 and age<100;
select运算查询
			select * ,运算 from 表名;
select * ,price*0.9 from shop;
加个别名,只是展示用,数据库的数据并没有使用
			select * ,price*0.9 as 打九折 from shop;
select的模糊查询(like)
			select * from student where 列名 like '%字符%';
select * from student where 列名 like '_字符%';
select * from student where 列名 like '_字符_';
select * from student where 列名 like '%字符_';
可以有多个字符
			关键字:  _:代表一个字符
				    %:代表多个字符
查询名字带有b
			select * from student where sname like '%b%';
			查询第二个字符是b
select * from student where sname like '_b%';
select的范围查询(in)
			select * from 表名 where 列名in (值1,值2,值3);
			找到student表中sid为1,6,8的行
			select * from student where sid in (1,6,8);
	排序查询(order by)
		asc : ascend升序(默认排序)
		desc : descend降序
		默认select * from 表名order by 列名;
			select * from shop order by price;
		降序select * from 表名order by price 列名;
select * from shop order by price desc;
		升序select * from 表名order by price 列名; 
select * from shop order by price asc;
分组查询(group by)
		select 要显示的列,聚合函数(*) from 表名 group by分组的列名;
		select 要显示的列,要显示的东西 from 那个表 group by哪个列分组

		从product表中将cno相同的数据为一组,统计个数
		select cno,count(*) from product group by cno;

		从product表中将cno分组,分组统计商品的平均价格,且商品平均价格>60
select cno,avg(price) from product group by cno having avg(price) > 60;
分析: 要显示select cno,avg(price) 
从哪张表from product group by cno
条件筛选 having avg(price) > 60;

别名
	表别名
		只是展示用,数据库的数据并没有使用
用于多列表查询
		select 别名1.列名1 别名2.列名2 from 表名 as 别名;
		select p.pname,p.price from shop as p;
		也可以中文
select 商品.pname,商品.price from shop as 商品;
关键字as是可以省略的
select 商品.pname,商品.price from shop商品;
	列别名
为了给别人看时候更好的理解
		select 类名1 as 别名1,列名2 as 别名2 from shop;
		select pname as 商品名称,price as 商品价格 from shop;
关键字as是可以省略的
select pname 名称,price 价格 from shop;
效果是一样的
聚合函数(列方面的)
sum():求和
select sum(要求总和的列名) from 表名;
select sum(price) from product;
avg():平均值
select avg(要求平均值的列名) from 表名;
select avg(price) from product;
count():统计数量
select count(要统计数量的列名) from 表名;
select count(price) from product;
max():最大值
select max(要求最大值的列名) from 表名;
select max(price) from product;
min():最小值
select min(要求最小值的列名) from 表名;
select min(price) from product;
综合使用
		select * from 表名 where 语句1 语句2;
		找到cno为2的商品,且加个按升序排列
		select * from shop where cno=2 order by price desc;
注意点
注意where后面不能接聚合函数,出现在分组前
			如果一定要接聚合函数,按以下例子
			找到表product中price大于平均值的数据
			错误:select * from product where price >avg(price);
正确:select * from product where price > (select avg(price) from product);
	select * from 表名 where 列名> (聚合函数的一整条语句);
注意having后面可以加聚合函数,出现在分组之后
			从product表中将cno分组,分组统计商品的平均价格,且商品平均价格>60
select cno,avg(price) from product group by cno having avg(price) > 60;
	编写顺序
			S------F-----W-----G---------H------O
			select—from—where--group by--having—order by
执行顺序
			F-----W------G---------H------S------O
			from--where-- group by--having--select--order by


简单示例

商品分类表
创建商品分类表格:id,名字,介绍
create table shopfl(
sid int primary key auto_increment,
sname varchar(10),
sintroduce varchar(20)
);
auto_increment:若为空则从1开始自动增加
插入数据
insert into shopfl values(null,'零食','好吃的');
insert into shopfl values(null,'饮料','好喝的');
insert into shopfl values(null,'游戏','好玩的');
查询数据
查询表的所有数据
select * from shopfl;
查询列名为sanme和sintroduce的数据
select sname, sintroduce from shopfl;
商品
商品与商品分类:所属关系
create table product(
	pid int primary key auto_increment,
	pname varchar(10),
	price double,
	pdate timestamp,
cno int
);
cno为商品在分类表的哪个类,用分类表的id
商品数据
insert into product values(null,'绿豆饼',10.0,null,1);
insert into product values(null,'红豆饼',10.00,null,1);
insert into product values(null,'酸奶',67.00,null,1);
insert into product values(null,'可乐',67.00,null,2);
insert into product values(null,'雪碧',67.00,null,2);
insert into product values(null,'k宝矿力',99.00,null,2);
insert into product values(null,'仙剑',99.00,null,3);
insert into product values(null,'三国无双',99.00,null,3);
insert into product values(null,'x天道',99.00,null,3);
查询商品
查询表的所有数据
select * from product;
查询表中列名为pname和price的数据
select pname,price from product;
查询表中列名为price的数据且去除重复
select distinct price from product;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值