MySql

Mysql

添加环境变量 - MYSQL_HOME

添加path %MYSQL_HOME%\bin

配置文件

[mysql]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
character-set-server=utf8
default-storage-engine=INNODB
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

存放在根目录下 名称为 my.ini

初始化

cmd - 管理员模式

mysqld --initialize-insecure

注册mysql服务

mysqld -install

启动mysql服务

net start mysql
# 卸载mysql
net stop mysql //停止mysql服务
mysqld -remove mysql //移除mysql
然后删除相关环境变量

修改默认账户密码

mysqladmin -u root password 123  //账户名root  密码 123

SQL 类型

DDL : 操作数据库,表

DML:对表中的数据进行增删改

DQL: 对表中的数据进行查询

DCL: 对数据库进行权限控制

数据库常用操作

单行注释  -- 内容 #内容
多行注释 /*   */

查询数据库名称

show databases;  //加分号

创建数据库

create database db1;

创建的时候判断数据库是否存在

create database if not exists db1;

删除数据库

drop database db2;

删除存在数据库

drop database if exists db2;

进入数据库

use db1;

查看当前使用的数据库

select database(); //查询当前使用的数据库

查询表

show tables;

查询表的字段

desc 表名;//结构信息

表的创建

creat table 表名(
	字段名1 数据类型1,
	字段名2 数据类型2
);

数据类型

MySQL 支持多种类型,可以分为三类:

  • 数值

    tinyint : 小整数型,占一个字节
    int	: 大整数类型,占四个字节
    	eg : age int
    double : 浮点类型
    	使用格式: 字段名 double(总长度,小数点后保留的位数)
    	eg : score double(5,2)   
    
  • 日期

    date : 日期值。只包含年月日
    	eg :birthday date : 
    datetime : 混合日期和时间值。包含年月日时分秒
    
  • 字符串

    char : 定长字符串。
    	优点:存储性能高
    	缺点:浪费空间
    	eg : name char(10)  如果存储的数据字符个数不足10个,也会占10个的空间
    varchar : 变长字符串。
    	优点:节约空间
    	缺点:存储性能底
    	eg : name varchar(10) 如果存储的数据字符个数不足10个,那就数据字符个数是几就占几个的空间	
    

案例-创建表

create table student(
	id int,
	name varchar(10),
	gender char(1),
	birthday date,
	score double(5,2),
	email varchar(64),
	tle varchar(15),
	status tinyint
);

修改表

alter table 表名 rename to 新表名; // 修改表名
alter table 表名 add 列名 数据类型; //增加列
alter table 表名 modify 列名 新的数据类型;//修改数据类型
alter table 表名 change 列名 新列名 新数据类型;//修改列
alter table 表名 drop 列名; // 删除列

DML主要是对数据进行增(insert)删(delete)改(update)操作。

添加数据

  • 给指定列添加数据
INSERT INTO 表名(列名1,列名2,…) VALUES(值1,值2,…);
  • 给全部列添加数据
INSERT INTO 表名 VALUES(值1,值2,…);
  • 批量添加数据
INSERT INTO 表名(列名1,列名2,…) VALUES(值1,值2,…),(值1,值2,…),(值1,值2,…)…;
INSERT INTO 表名 VALUES(值1,值2,…),(值1,值2,…),(值1,值2,…)…;

修改数据

update stu set sex = '女' where name = '张三';
update stu set birthday = '1999-12-12', score = 99.99 where name = '张三';

删除数据

delete from 表名 where 条件 //删除语句 
delete from stu where name = 张三

查询案例

-- 删除stu表
drop table if exists stu;


-- 创建stu表
CREATE TABLE stu (
 id int, -- 编号
 name varchar(20), -- 姓名
 age int, -- 年龄
 sex varchar(5), -- 性别
 address varchar(100), -- 地址
 math double(5,2), -- 数学成绩
 english double(5,2), -- 英语成绩
 hire_date date -- 入学时间
);

-- 添加数据
INSERT INTO stu(id,NAME,age,sex,address,math,english,hire_date) 
VALUES 
(1,'马运',55,'男','杭州',66,78,'1995-09-01'),
(2,'马花疼',45,'女','深圳',98,87,'1998-09-01'),
(3,'马斯克',55,'男','香港',56,77,'1999-09-02'),
(4,'柳白',20,'女','湖南',76,65,'1997-09-05'),
(5,'柳青',20,'男','湖南',86,NULL,'1998-09-01'),
(6,'刘德花',57,'男','香港',99,99,'1998-09-01'),
(7,'张学右',22,'女','香港',99,99,'1998-09-01'),
(8,'德玛西亚',18,'男','南京',56,65,'1994-09-02');

查询数据——条件查询

select 列名1,列名2 from 表名;
select DISTINCT 列 from 表; //去除重复数据
select name as 名称 from 表; //用as 来 重命名展示的列名
select * from stu where age>20;
selct * from stu where age>=20 and age<=30;
= select * from stu where age between 20 and 30;
select * from stu where hire_date between '1998-09-01' and '1999-09-01';
select * from stu where age != 18;  - '<>'也是不等于
或者  || = or 
in (18,20,22)
select * from stu where age in (18,20,22)
null值不能用等于来比较。需要用is
select * from stu where age is not null;
-----------模糊查询---------------
通配符 : _ 表示单个任意字符 % 表示任意个字符
select * from stu where name like '马%';
select * from stu where name like '_花%';

查询数据——排序查询

排序方式:
ASC 升序排列 默认值
DESC 降序排列
select * from stu order by age asc;
select * from stu order by math desc, englisg asc; //数学成绩一样,英语成绩按照升序排列

查询数据——分组查询

聚合函数
cout(列名)  统计数量
max()  min()  sum()  avg()
select 聚合函数() from 表;
select count(id) from stu;

SELECT 字段列表 FROM 表名 [WHERE 分组前条件限定] GROUP BY 分组字段名 [HAVING 分组后条件过滤];

SELECT sex, avg(math), count(*) from stu GROUP BY sex;

select  sex, avg(math), count(*) from stu where math > 70 group by sex;

select  sex, avg(math), count(*) from stu where math > 70 group by sex having count(*) > 2; -- 人数大于2才会被展示

查询数据——分页查询

SELECT 字段列表 FROM 表名 LIMIT  起始索引 , 查询条目数;
select * from stu limit 0,3;

原文地址:https://www.cnblogs.com/jy00/p/15605411.html