MySQL的基本使用
MySQL是一个关系型数据库管理系统,数据库就是一种特殊的文件,其中存储着需要的数据
MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化语言。
Mysql的特点:于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选择 MySQL 作为网站数据库。
一、命令行脚本
1.1.命令行连接
mysql -uroot -p 回车后输入密码
1.2.退出登录
quit 和 exit 或 ctrl+d
1.3.数据库操作
查询所有数据库
show databases;
使用数据库
use 数据库名字
查看当前使用的数据库
select database();
创建数据库
create database 数据库名 charset=utf8;
删除数据库
drop database 数据库名;
1.4.数据表操作
查询数据库中所有表
show tables;
查看表结构
desc 表名;
修改表–添加字段
alter table 表名 add 列名 类型;
修改表–修改字段:重命名版
alter table 表名 change 原名 新名 类型及约束;
修改表–修改字段:不重命名版
alter table 表名 modify 列名 类型及约束
修改表–删除字段
alter table 表名 drop 列名
删除表
drop table 表名;
查询表的创建语句
show create table 表名;
二、MySQL-查询
2.1.向表中插入数据
insert into 表名 values()
查询所有字段
select * from 表名;
查询指定字段
select name from students;
使用as起别名
select id as 序号,name as 名字 from students;
消除重复行
select distinct age from students;
2.2.条件
where后面支持多种运算符,进行条件的处理
比较运算符
逻辑运算符
模糊查询
范围查询
空判断
比较运算符
等于: =
大于: >
大于等于: >=
小于: <
小于等于: <=
不等于: != 或 <>
例如
select * from students where id > 3;
and,or,not
select * from students where id > 3 and gender=0;
select * from students where id < 4 or is_delete=0;
模糊查询
like
% 表示任意多个字符
_表示一个任意字符
例:查询姓黄的学生
select * from students where name like "黄%";
select * from students where name like "黄_";
范围查询
in 表示在一个非连续的范围内
例:查询编号是1或3或8的学生
select * from students where id in(1,3,8);
空判断
is null
例:查询没有填写身高的学生
select * from students where height is null;
判断非空 is not null
例:差询填写了身高的学生
select * from students where height is not null;
优先级
优先级由高到低的顺序为:小括号,not,比较运算符,逻辑运算符
and比or先运算,如果同时出现并希望先算or,需要结合()适应