mysql简单入门笔记

MYSQL的SQL语句

​ SQL: Structrued Query Langauage 结构化的查询语言

    - DDL: 数据定义语言 : 定义数据库或者表结构. alter(修改) create(创建) drop(删除) truncate(截断)

    - DML: 数据操纵语言: 用来操作表中数据:  insert(插入)  update(更新)  delete(删除)

    - DQL: 数据查询语言: 用来查询数据  select(选择)  from(从哪里查)  where(哪一条,判断条件)

    - DCL: 数据控制语言: 用来控制用户权限 grant(授权) revoke(取消授权)

cmd窗口登陆数据库:

    cmd窗口登录数据:  mysql  -uroot -proot

    -u  username 用户名

    -p  password密码

一.数据库基本语句

1.创建数据库

    create database 数据库的名字 ---用的非常多的方式

    create database day08_1;

    create database 数据库的名字 character set 字符集;

    create database day08_2 character set gbk;

    create database 数据库的名字 character set 字符集 collate 校对规则;

    -- 校对规则: 字符的比较规则 a=100  A=110

    -- 默认的校对的规则utf8_general_ci    : 不区分大小写 : a=A  条件成立

    -- utf8_bin 比较的是字符的编码, a=A 不能成立

    -- ISO-8859-1  不支持中文

    -- gbk  中文字符集

    -- utf8  国际标准字符集, 支持中文

    create database day08_3 character set utf8 collate utf8_bin;

2.查看数据库

    -- 查看数据库定义的语句: 查询的是数据库创建的语句

    show create database 数据库的名字

    --查看所有数据库

    show databases;

3.修改数据库的操作

--修改数据的字符集

alter database 数据库的名字  character set 字符集

alter database data08_2 character set utf8;

4.删除数据库

drop database 数据库名字

5.其他数据库操作命令

--切换数据库(选中数据库)

use 数据库名字

--当前选中是哪个数据库,当前正在使用哪个数据库

select database();

二.表的CRUD操作

1.创建表

--语法:

create table 表名(

列名 列的类型 [列的约束],

列名 列的类型 [列的约束]

);

--[] 代表的可有可无

create table 表名(

列名1 列的类型 [列的约束],

列名2 列的类型 [列的约束]

);

-- 列的类型

java                             mysql

int                              int

String                           char(长度)/varchar(长度)
  
                                 char(10): 固定长度字符hello 没有填满用空格补齐10个   -- 都不能超过最大长度
    
                                 varchar(10):可变长度字符hello  只占5个字符   -- 可以存放10个汉字 ,10个字符

boolean                           boolean

Date date : YYYY-MM-DD            datetime: YYYY-MM-DD hh:mm:ss  默认值是null

time : hh:mm:ss                   timestamp:YYYY-MM-DD hh:mm:ss  默认值是当前的时间, 最大取值是2038年

                                  text: 大文本类型

                                   blob: 二进制大对象类型  大电影 java-存放和读取

-- 创建员工(employee)表: 员工编号empno,员工名称ename,工资sal,部门编号deptno

create table emp(

empno int,

ename varchar(20),

sal  double,

deptno int

);

--列的约束

主键约束: primary key不能重复,不能为空  通常用在编号ID

唯一约束: unique 唯一约束: 不能重复,但是可以为空

非空约束: not null 不能为空

create table emp1(

empno int primary key,

ename varchar(20) unique,

sal double not null,

deptno int

);

--auto_increment 自动增长属性,通常是加载编号上的

create table emp1(

empno int primary key auto_increment,

ename varchar(20) unique,

sal double not null,

deptno int

);

2.查看表

--  查看所有的表

show tables;

--  查看表的创建过程,建表语句

show create table 表名

--  查看表结构

desc 表名

3.修改表

添加列(add),修改列(modify),修改列名(change),删除列(drop),修改表名(rename),修改表的字符集

-- 添加列(add)  alter table 表名 add 列名 列名类型

-- 添加性别

alter table emp1 add gender varchar(2);

-- 修改列(modify) alter table 表名 modify 列名 列的类型

-- 重定义列的类型

alter table emp1 modify gender int; 0 1 2

-- 修改列名(change)  alter table 表名 change 旧的列名 新列名 列的类型

--  将gender 改成 sex

alter table emp1 change gender sex int;

-- 删除列(drop) alter table 表名 drop 列名;

alter table emp1 drop sex;

-- 修改表名(rename) rename table 旧表名 to 新表名

rename table emp1 to emp11;

-- 修改表的字符集 alter table 表名 character set 字符集

alter table emp11 character set gbk;

4.删除表

--drop table 表名

drop table emp11;

三.表中数据的CRUD的操作

1.插入数据

--创建一张表

-- 学生表 : 学号, 姓名,性别

create table stu(

stuno int primary key auto_increment,

sname varchar(20),

gender varchar(2)

);

-- 指定列名插入:insert into 表名(列名1,列名2) values(值1,值2)

-- 注意: 列名的数量和值的数量要一一对应

insert into stu(stuno,sname) values(1,'zs');

insert into stu(stuno,sname) values(null,'lisi');

-- 不指定列名插入:必须按照顺序,把表中所有的列的数据都填上,如果不填必须填null

insert into stu values(null,'wangwu','nv');

insert into stu values(null,'wangwu','female');  --数据太长了 varchar(2)

insert into stu values(null,'zhaoliu','男');

insert into stu values(null,'zhaoliu','人妖');

insert into stu values(null,'zhaoliu','人妖王');  --错误的

insert into stu values(null,'zhaoliu','nan');    --错误的

2.删除记录(面试)

--delete from 表名 [ where 条件 ]

--删除 stuno = 1的学生信息

delete from stu where stuno=1;

--全部删除  , 逐条删除数据

delete from stu;

--面试问题: 请说一下 delete 删除表中所有数据 和 truncate 删除表中所有数据有什么差别

detele: DML(操作数据)    逐条删除数据

truncate : DDL(操作结构)  先删除表,再创建表

-- delete 和 truncate 哪一个高效 ? truncate高效

看表中数据量的大小

数据量小: delete 高效

数据量大: truncate 高效

3.更新表记录

update 表名 set 列名=列的值,列名2=列的值2  [where 条件]

update stu set gender='男';

update stu set gender='女' where stuno=1;

update stu set gender='女' where sname='wangwu';

4.语句详情

--查询语句的基本结构:  select ... from ....

select 要显示的列名 from 从哪张表里面去查询数据 [where 条件:查询的是哪条记录]

select [distinct] [列名,列名] [*] from 表名 [where 条件]

--只是查询出结果,不会改变数据库里面原有的内容

--简单查询:

---查询所有商品信息:

select * from product;

---查询商品名称和商品价格:

select pname,price from product;

---别名查询. as 的关键字 , as 关键字是可以省略  外号

--表别名: 多表查询里面

select p.pname,p.price from product p;

select p.* from product p;

--列别名: as 可省略

select pname as 商品名称,price 商品价格 from product;

去重distinct

--- 去掉重复的值 distinct

--查询所有商品的名称

select pname from product;

--去除重复的商品名称

select distinct pname from product;

-- 多列去除重复的, 必须是每一列都一样才算重复

select distinct pdesc,cno from product;

-- select运算查询 : 仅仅在查询结果上做了运算 + - * /

select pname,price*0.75 折后价 from product;

select 1+1; -- mysql里面可以这样写, 在Oracle中不可以这样

-- 条件查询 [where关键字] : 查询表中符合条件的数据

where 列名 条件

关系比较


--关系/比较运算符: > >= = < <= != <>

!= : 非标准SQL

<> : 标准SQL语法

--逻辑运算: and , or , not

--其它运算符:

is null    判断是否是空

is not null 判断是否不为空

in (集合)   在某个范围内

between..and..  在某个区间内 ,包含两端  闭区间 [10,100]  (10,100)

-- 注意: null 代表的是未知的,不可预知的内容

--查询商品价格>60元的所有商品信息

select * from product where price > 60;

-- where 后的条件写法

--查询商品价格不等于99的所有商品

select * from product where price !=99;

select * from product where price <>99;

--查询出商品价格不为null的所有商品

select * from product where price != null; -- 错误的演示

select * from product where price is not null;

-- 查询商品价格在10 到 100之间 price >=10 and price <=100;

select * from product where  price >=10 and price <=100;

select * from product where price between 10 and 100;

select * from product where price between 100 and 10; --错误的演示 [100,10]

--查询出商品价格 小于35 或者商品价格 大于900

select * from product where price <35 or price >900;

模糊查询

--like: 模糊查询

%  匹配多个字符

_  匹配单个字符

-- 查询出名字中带有辣的所有商品:

select * from product where pname like '%辣%';

-- 查询出名字第三个字是辣字的所有商品信息

select * from product where pname like '__辣%';

-- in 判断值是否在某个集合/范围

--查询出商品分类ID在 1,3,4里面的所有商品

select * from product where cno in(1,3,4);

--排序查询: order by 关键字/列名

升序: asc  ascend  默认是升序

降序: desc  descend

--0. 查询所有商品,按照价格进行排序

select * from product order by price;

select * from product order by price asc;

delete from product where pid = 9;

--1.查询所有的商品,按价格进行降序排序 (asc-升序  desc-降序);

select * from product order by price desc;

--2.查询名称有 辣 的商品,按价格降序排序

---1. 查询出所有 辣的商品

select * from product where pname like '%辣%';

---2 排序

select * from product where pname like '%辣%' order by price desc;

--模糊查询如果出现了特殊字符

create table test2(

sname varchar(20)

);

insert into test2 values('华%安');

select * from test2 where sname like '%\%%';  --使用转义符号

select * from test2 where sname like '%#%%' escape '#';

-- escape 相当于是告诉mysql 我这里面的这个#代表的是转义字符

校对规则(了解的内容): 字符的在查询语句中的比较规则

-- 创建数据库的时候, 使用默认的校对规则 utf8_general_ci 不区分大小写

create database day08_4;

use day08_4;

create table test1(

sname varchar(10)

);

insert into test1 values('abc');

select * from test1 where sname = 'ABC';

select * from test1 where sname like 'ABC';  ---能查询出结果

-- 创建数据库的时候执行字符集和校对规则  utf8_bin  比较字符的编码

create database day08_5 character set utf8 collate utf8_bin;

use day08_5;

create table test2(

sname varchar(10)

);

insert into test2 values('abc');

select * from test2 where sname = 'ABC';

select * from test2 where sname like 'ABC';  ---- 查询不出来结果

聚合函数

--聚合函数:

sum() 求和 avg()求平均值 count()统计个数 max()最大值 min()最小值

--1.获得所有商品价格的总和:

select sum(price) from product;

select sum(price) 商品总价 from product;

--2.获得所有商品的平均价格:

select avg(price) 平均价格 from product;

--3.获得所有商品的个数:

select count(pid) from product;

select count(*) from product;

select 1 from product;

select count(22) from product;  --使用常量能够提高效率

--注意: where 条件后面不能接聚合函数 having 后面可以加

select * from product where  price > avg(price);--  错误

分组 group by

--分组: group by

select 分组的条件,分组之后的操作 from 表名  group by 分组条件 having 条件过滤;

--select后控制的是要显示的内容, 只写分组的条件和分组之后的操作

--1.根据cno字段分组,分组后统计商品的个数

select cno,count(1) from product group by cno ;

select cno,pname,count(1) from product group by cno ;  --错误的演示

--分组之后的条件过滤: having

--2.根据cno分组,分组统计每组商品的平均价格 并且商品平均价格 > 60

select cno,avg(price) from product group by cno ;

select cno,avg(price) from product group by cno having avg(price)>60;

-- SQL编写顺序

--  S..F..W..G..H..O

select ... From .. where .. group by ... having ....order by

-- SQL执行顺序

F..W..G..H..S..O

from ... where .. group by ... having ...select ...order by

5.乱码解决方法:

- 终身解决方案:

1. 先关闭掉MYSQL服务器

2. 找到安装路径C:\Program Files (x86)\MySQL\MySQL Server 5.5\my.ini

注意:  可能需要修改权限

修改第57行的编码为gbk

3. 打开mysql服务
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容