我的MySQL基础教程

MySQL安装


bin目录里cmd

初始化


mysqld –-initialize安装mysqld install ‘服务名’卸载sc delete 服务名

启动


net start mysql 命令启动MySQL服务net stop mysql 关闭

登录


mysql -h localhost -u root -p密码123456直接回车退出quit;

修改use mysql;

变更当前数据库

alter user 'root'@'localhost' identified with mysql_native_password by '你的新密码';

修改密码

DDL库表


DDL操作库:


展示所有库

show databases;

创建库

create database 库名(character set utf8);

(库名不能重复)

判断库存在

create database if not exist 库名;

删除库

drop database 库名;

使用进入数据库

use 库名;

DDL操作表:


使用进入数据库后:

创建表create

create table 表名(

字段名1 数据类型1,

xxxxxxx xxxxxxxx,

xxxxxxx xxxxxxxx #最后一行不加逗号

);

createtable student(

id int,#数字id

name varchar(10),

sex char(1),#男女

birthday date,#年月日

score double(5,2),#成绩,总长度5,小数后最多2

email varchar(64),

tel varchar(15),

statustinyint#用数字表示各种状态,上学,休学,辍学...

);

查询表retireve

查看这个库的所有表:

show 库名;

查看一个表的结构:

desc 表名;

describe 表名;

修改表update

1.修改表名:

alter table 表名 rename to 新表名;

2.添加一列:

alter table 表名 add 列名 数据类型;

3.修改数据类型:

alter table 表名 modify 列名 新的数据类型;

4.修改列名和数据类型:

alter table 表名 change 列名 新列名 新的数据类型;

alter table 表名 change 列名 新列名 新的数据类型 character set utf8;

5.删除列:

alter table 表名 drop 列名;

删除表delete

drop table 表名;

DML增删改数据


添加(insert into)


1.给指定列添加数据

insert into 表名(列名1,列名2,...) values(值1,值2,...);

insert into 表名 values(值1,值2,...);

INSERTINTO 表名(列名1,列名2,…)VALUES(值1,值2,…);

2.给全部列添加数据

INSERTINTO 表名 VALUES(值1,值2,…);

3.批量添加数据

INSERTINTO 表名(列名1,列名2,…)VALUES(值1,值2,…),(值1,值2,…),(值1,值2,…)…;

INSERTINTO 表名 VALUES(值1,值2,…),(值1,值2,…),(值1,值2,…)…;

-- 批量添加数据

INSERTINTO stu VALUES

(2,'李四','男','1999-11-11',88.88,'lisi@itcast.cn','13888888888',1),

(2,'李四','男','1999-11-11',88.88,'lisi@itcast.cn','13888888888',1),

(2,'李四','男','1999-11-11',88.88,'lisi@itcast.cn','13888888888',1);

修改(update set)


update stu set id=2 where name = '张三';

  • 修改表数据

UPDATE 表名 SET 列名1=值1,列名2=值2,… [WHERE 条件];

注意:
修改语句中如果不加条件,则将所有数据都修改!
像上面的语句中的中括号,表示在写sql语句中可以省略这部分
  • 练习

  • 将张三的性别改为女

update stu set sex ='女'where name ='张三';

  • 将张三的生日改为 1999-12-12 分数改为99.99

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

  • 注意:如果update语句没有加where条件,则会将表中所有数据全部修改!

update stu set sex ='女';

删除(delete from)


delete from stu where name = '张三';

  • 删除数据

DELETEFROM 表名 [WHERE 条件];

  • 练习

-- 删除张三记录

deletefrom stu where name ='张三';

-- 删除stu表中所有的数据

deletefrom stu;

DQL查询数据


SELECT字段列表

FROM表名列表

WHERE条件列表

GROUP BY分组字段

HAVING分组后条件

ORDER BY排序字段

LIMIT分页限定

基础查询:


  • 查询多个字段

SELECT 字段列表 FROM 表名;

select name,age from stu;

SELECT*FROM 表名;-- 查询所有数据

select*from stu;

  • 查询时去除重复记录

SELECTDISTINCT 字段列表 FROM 表名;

selectdistinct address from stu;

-- 去除了重复的地址记录

  • 给查询的字段起别名

AS:AS 也可以省略

select name,math as 数学成绩,english as 英文成绩 from stu;

select name,math 数学成绩,english 英文成绩 from stu

条件查询:


  • 查询年龄大于20岁的学员信息

select*from stu where age >20;

  • 查询年龄大于等于20岁的学员信息

select*from stu where age >=20;

  • 查询年龄大于等于20岁 并且 年龄 小于等于 30岁 的学员信息

select*from stu where age >=20&& age <=30;

select*from stu where age >=20and age <=30;

上面语句中 && 和 and 都表示并且的意思。建议使用 and 。
也可以使用 between ... and 来实现上面需求

select*from stu where age BETWEEN20and30;

  • 查询入学日期在'1998-09-01' 到 '1999-09-01' 之间的学员信息

select*from stu where hire_date BETWEEN'1998-09-01'and'1999-09-01';

  • 查询年龄等于18岁的学员信息

select*from stu where age =18;

  • 查询年龄不等于18岁的学员信息

select*from stu where age !=18;

select*from stu where age <>18;

  • 查询年龄等于18岁 或者 年龄等于20岁 或者 年龄等于22岁的学员信息

select*from stu where age =18or age =20or age =22;

select*from stu where age in(18,20,22);

  • 查询英语成绩为 null的学员信息

null值的比较不能使用 = 或者 != 。需要使用 is 或者 is not

select*from stu where english =null;-- 这个语句是不行的

select*from stu where english isnull;

select*from stu where english isnotnull;

模糊查询练习:
模糊查询使用like关键字,可以使用通配符进行占位:
(1)_ : 代表单个任意字符
(2)% : 代表任意个数字符
  • 查询姓'马'的学员信息

select*from stu where name like'马%';

  • 查询第二个字是'花'的学员信息

select*from stu where name like'_花%';

  • 查询名字中包含 '德' 的学员信息

select*from stu where name like'%德%';

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值