1.打开cmd,运行,mysql -u root -p
然后输入密码,点回车,进入数据库
2. 系统数据库
查看数据库命令:show databases;
自带 information_schema :存储数据库对象信息,如用户表信息列信息 根除 字符,分区,里面内容不能动
performance_schema :存储数据库服务器性能参数信息
mysql:自己创建的数据库
sys: 5.7 版本后是sys,可以快速了解系统元信息,这个库通过视图的形式把information_schema和performance)schema结合起来
添加数据库命令:createabase +数据库名+' ; ' 例如: createdabase my_kb;
删除数据库命令: drop dabase my_kb;
3. 使用数据库
步骤:
- 查看数据库:show dabases;
- 使用数据库 :use 数据库名;
- 显示数据库中的表:show tables;
图形化界面 Navicat
DDL:数据定义语言
- 创建表名:creat table 表名();create table emplyee(id int,name varchar(50));
- 添加一列:alter table 表名 add 列名 数据类型;
- 查看表的字段信息:desc 表名;
- 修改一个表的字段类型:alter table 表名 modify 字段名 数据类型;
- 删除一列: alter table 表名 drop 字段名 数据类型;
- 修改表名:rename table 原始表名 to 要修改的表名;rename table emplyee to employee;
- 查看表的创建细节: show create tables 表名;
- 修改表的字符集为gbk:alter table 表名 character set 字符集名称;alter table employee character set gbk;
- 修改表的列名:alter table 表名change 原始列名 新列名 数据类型; alter table employee change name newname varchar(20);
- 删除表:drop table 表名;
DML:数据操作语言(对表中的数据进行增、删、改的操作)
- 查询表中所有的数据:select * from 表名;
- 插入操作:insert into 表名(列名1,列名2...) values(列值1,列值2...);
- 批量插入:insert into 表名(列名1,列名2...) values(列值1,列值2...), 表名(列名1,列名2...) values(列值1,列值2...);
- 注意:列名要与列值的类型、个数、顺序一一对应。值不要超过列定义的长度。插入的日期和字符一样,都是用括号括起来。
- 更新操作:update 表名 set 列名1=列值1,列名2=列值2...where 列名=值。
- 例如:update employee set score=90;所有分数变成90。
- 加上条件的: update employee set name='zhangsan' where name='zs';改多个字段数据中间加上逗号隔开
- update employee set age=age+1 where name='zhangsan'; 在原来年龄基础上加一
- 修改数据库密码:退出当前用户:exit;
- use mysql;
- update mysql.user set authentication_string=password('123456') where user='root' and Host = 'localhost';
- flush privileges;刷新MySQL的系统权限相关表
- quit; 退出
- 还有一种方法:mysqladmin -u root -p password 12356 注释:password后面是新密码,会让输入原始密码,就会更改
- 删除操作:1. delete from 表名 where 列名=值; 例:delete from student where name='ww';
- delete from student;删除了所有的数据。删除后表的结构还在。
- 2. truncate table students; 删除名为students的表,并且创建新的一个表出来。删除的数据不能找回。执行速度比delete快
DQL:数据查询语言
查询虚拟结果集:查询出来的结果,是虚拟的,存放于内存中,下次进来就没了。
注释:选中内容 按住 ctrl+/ 注释掉内容。
- 查询所有的列:select * from 表名;
- 查询指定的列: select name,age from student;
- 查询性别为男,并且年龄为20的学生记录:SELECT * FROM student WHERE gender='男' AND age=20;
- 查询学号为1001或者名为zs的记录:SELECT * FROM student WHERE id=1001 or name='zs';
- 查询学号为1001,1002,1003的记录:SELECT *FROM student WHERE id=1001 or id=1002 or id=1003;
- SELECT * FROM student WHERE id in(1001,1002,1003);
- 查询年龄为空的记录:select * from employee where age is null;
- 不为空:select * from employee where age is not null;
- 查询性别为非男的学生记录:select * from employee where gender !='男';
- 年龄在18-20之间的学生:select * from employee where age>=18 and age <=20;还有一种写法:where age between 18 and 20;
模糊查询:
-- 查询姓名由5个字母构成的学生记录里面是5个下划线,--只想看名字,就把*换成name即可
下划线代表单个字符,不一定是字母,*代表查询所有字段。
- select * from employee where name like '_____' ;
- 由五个字母组成并且第五个是y :select * from employee where name like '____y' ;
- 查询姓名以m为开头的记录:select * from employee where name like 'm%' ; m后面加上% 表示后面任意字符
- 查询姓名第二字符为u的记录:select * from employee where name like '_u%';
- 查询姓名包含字符为s的记录:select * from employee where name like '%s%';
字段控制查询
- 去除重复查询:SELECT DISTINCT name from employee;
- 字段相加,运算必须是数据型:SELECT *,age+score FROM employee;
- SELECT *,IFNULL(age,0)+IFNULL(score,0) AS total FROM employee;
- IFNULL是个函数,如果第一个参数数值是null,则设置为0,进行运算。as total 命名新的数据列名为total
升序,排序:
- SELECT * FROM employee ORDER BY salary DESC,id ASC; salary 是降序,id是升序。
- 查询有绩效的人数,和有管理费的人数::SELECT COUNT(performance), COUNT(manage) FROM employee;
- 查询所有雇员月薪和:SELECT SUM(salary) FROM employee;
- 查询所有雇员月薪+绩效和:SELECT SUM(salary+IFNULL(performance,0)) FROM employee;
- 统计所有员工平均工资:SELECT AVG(salary) FROM employee;
- 查询最高工资和最低工资:SELECT MAX(salary), MIN(salary) FROM employee
分组查询: