创建数据库
1、创建数据库代码如下
create database ’ 数据库的名字 `
//数据库的名字要用反引号引起来以防关键字
2、creats database if not exists shujv
// 如果数据库shujv不存在的话就创建shujv数据库,如果存在的话就不用创建了
3、show create database shujv
//查看 shujv 数据库的信息
4、show database
//查询MySQL服务器中已经存在的数据库
5、use shujv
//选择 shujv 数据库进行操作
6、drop database shujv
//删除 shujv 这个数据库
7、drop database if exists shujv
如果shujv 数据库存在的话 就删除这个数据库,不存在就不删除
表的基本操作
概念:数据表在创建时,需为每个字段选择数据类型,而数据类型的选择则决定着数据的存储格式,有效范围和相应的限制。
1、MySQL 提供了多种数据类型,主要分为 3 类:
//数值类型、字符串类型、日期与时间类型
(1)、MySQL提供了很多数值类型,大体可以分为整数类型和浮点类型。
// 整数类型根据取值范围分为 INT、SMALLINT等
// 浮点类型又分为 FLOAT、DECIMAL等。
int 整数
float 4个字节 单精度
double 8个字节 双精度
decimal(m,d) m+2个字节
//decimal(18,5)意思是 有18位数,小数有五位
(2)、字符串类型
// char :固定字符串的长度
//varchar:可变字符串的长度
//text : 大文本数据 ,不区分大小写
//blob : 区分大小写,表示二进制
(3)、日期与时间类型
//date:存储日期
//time:存储时间
//time stamp:时间戳
//date time:存储日期和时间
2、存储引擎
// InnoDB:存储引擎
//myisam:存储引擎
//MEMORY 存储引擎
//ARCHIVE 存储引擎
主要是InnoDB存储引擎和myisam存储引擎
创建数据表
1、create table语句是创建数据表的
//crease table if not exists `biao’(
// 这里是…
//);
如果biao不存在的话就创建这个表,存在就不创建,表的名称也用反引号
2、创建一个学生表
在这里插入代码片create table `student_gai`(
`studentid` int not null auto_increment comment '学号',
`stu_name` varchar(16) not null comment '姓名',
`sex` char(1)default '男'comment '性别',
`age` int comment '年龄',
`birthdy` datetime comment '生日',
primary key (`studentid`)
)default charset 'utf8';
3、查看数据表的3种方法
show create table、describe、show columns
// show columns from biao
查看biao表的结构
4、修改表
//alter table biao
add desc
char(100);
为 biao这个 数据表添加描述字段 desc,要求数据类型为 char(100)。
//alter table biao
change desc
description
varchar(100)
将 biao这个 数据表中的描述字段 desc 的名称修改为 description,数据类型修改为 VARCHAR(100)。
5、 RENAME TABLE 原数据表名 TO 新数据表名;
//修改数据名称
6、RENAME TABLE new_user
TO user
;
//将数据表“new_user”重命名为“user”
7、drop table if exists biao
//删除biao 数据表(if exists用于在删除一个不存在的数据表时,防止产生错误。)
8、五种约束
// not null:非空约束
//unique :唯一约束
//primary key :主键约束
//default:默认约束
//外键约束
9、创建数据表
在这里插入代码片
create table `student`( #创建一个名为student的数据表
#列表名为studentid 为整形,不能是空值 设置自动增长 注释为学号
`studentid` int not null auto_increment comment '学号',
#列表名是stu——name 是可变长度为16
`stu_name` varchar(16) not null comment '姓名',
列表为sex 固定长度是一个默认为男 注释为性别
`sex` char(1)default '男'comment '性别',
`age` int comment '年龄',
`birthdy` datetime comment '生日',
#将studentid转化为主键约束
primary key (`studentid`)
)default charset 'utf8';
数据的管理
1、insert into 表名(字段名1、字段名2…)values(值1、值二)//增加数据
//向表中添加数据 列如:
1)带字段添加数据
//insert into ‘表名’(‘id’, ‘name’,‘sex’,‘age’)values values (1,‘张三’,‘男’,24);//字段名和表名都要加反引号
2)不带字段添加数据
//insert into ‘表名’ values (1,'张三','男',24);
3)带字段添加多条数据
// insert into `表名'('id', 'name','sex','age')values values
(1,'张三','男',24),(2,'李四','男',34),(3,'李婷','女',18);
4)不带字段添加多条数据
//insert into ‘表名’ values (1,'张三','男',24),(2,'李四','男',34),(3,'李婷','女',18);
5)指定添加数据
// insert into ‘表名’ (‘name’,sex) values (‘赵六’,‘男’);
2、update ‘表名’ set 字段名1 = 值1 where 条件表达式//修改数据
//修改表中的数据例如
1)update ‘student’ set ‘name’=‘李四’ where id=2;//条件修改
//将id为二的那一条数据中的 name 改为李四 //where是查询条件
2)update ‘student’ set name=‘蔡徐坤’;
//将student表中的name 全部改为蔡徐坤。
3)update ‘student’ set sex,age where name='李四';//修改多个
//修改名为李四的 sex和age 的信息在student表中
3、delete from 表名 where //删除数据表中的数据
删除数据表中的数据
1)delete from student where id=1;
//删除数据表中id=1的这一条数据
2)delete from ‘student ’;
//删除student表中的全部数据
3)truncate ‘student’;
//清空student表中的全部数据
4)delete from ‘student ’;和truncate ‘student’;的区别
第一个全部删除后,在增加一条数据后会接着上一个的id值继续往下排,
(前提是id加上了自动增长)
而第二个是清空去全部数据,在加上一条数据后id直接从1开始往下排。
查询数据
以上就是 SELECT 语句的基本语法结构
1、select * from 表名//
*表示表中的所有字段
2、select 字段1、字段2、…from student //查询student表中字段1、字段2的数据
select name,sex from ‘student’;
//查询student表中 name 和 sex 字段
3、select sex as ‘性别’,age as ‘年龄’ from student;
//查询student表时给sex和age一个别名用as
一、比较运算符
1、select * from student where name=‘李四’;
//查询student 表中 name字段值为李四的分类
2、select * student from where age=10 and name=‘张三’;
//查询 student 表中 age值为10并且 名字为张三的记录
3、 select distinct name from student;
//查询student 表中所有的name重复的记录
4、select * from student where id in(1,3,5,6);//in在…里
//查询id 在1,3,5,6里的所有数据
5、select id
,name
,sex
from student where age between 50 and 100;//
between… and …: 查询多少到多少之间的
//查询年纪在50到100的id,name ,sex相关信息
6、模糊查询 % 和_
select * from student where name
like ‘白_’;
//查询 student表中名字 白字在前的记录(_表示后面只能有一个字)
select *from student where name
like ‘%承%’;
//查询 student表中名字 包含白字的记录
7、and 关键字可以用于连接两个或者多个查询条件,只有满足所有条件的记录才会被返回
1)select * from student where id in(1,3,5,6) and ‘name’ like ‘白%’;
// like 关键字 可以判断两个字符串是否相匹配。
//查询id是1,3,5,6这里的并且名字是以白开头的记录
8、or 关键字也可以连接多个查询条件,但与 and 不同的不同之处在于,使用 or 关键字时,只要记录满足任意一个条件就会被查询出来。
1)select * from student where id in(1,3,5,6) or name
like ‘白%’;
// like 关键字 可以判断两个字符串是否相匹配。
//查询id是1,3,5,6这里的或名字是以白开头的记录
排序
1、select * from student order by age asc;
//将student表里的age进行升序
2、select * from student order by age desc;
//将student表里的age进行降序
LIMIT限量查询
1、select * from student limit 1,2;
//查询student表里从第二条记录开始,显示两条记录。
2、select * from student where age between 10 and 20 and sex=‘男’ order by id desc limit 3;
//查询student表中年龄在10到20岁并且性别是男,id进行降序只显示前三条的记录
聚合函数
例如:
1、查询age最大的
//select max(age) from zuo;
2、查询age最小的
//select min(zuoid) from zuo;
3、select count(age) from student;
查询age有多少个
GROUP BY分组查询
1、select sex from student group by sex;
//将sex进行分组查询,男的有多少个,女的有多少个;
2、select sex,age from student group by sex,age;
//将sex,age进行分组查询。
多表查询
1、合并查询
2、连接查询
1)交叉连接
2)内连接
3)外连接
外连接又包括:左外连接、右外连接、全体连接
1、合并查询
1)union all
select `cid`,`name` from article where `cid`=1
union all
select `cid`,`cname` from `category` where `cid`=2;
2)union
select `cid`,`name` from article where `cid`=1
union
select `cid`,`cname` from `category` where `cid`=2;
union all 和union的区别?
1、union all 能去除重复的部分,union显示重复的部分
2、union必修按照顺序、字段个数还有类型都一一对应上才行
2、连接查询
1)交叉连接
1、select * from category as c cross join article as a;
//join关键字 *********category表的记录和 article表的记录连接
2、select * from category as c cross join article as a where c.cid=a.cid;
// 查询category表中的cid与 article中的cid相等的,相连接
2)内连接
1、 select * from article a inner join category c on a.cid = c.cid;
//-- inner join …on… 内连接(内连接只匹配行,所以内连接会丢失数据);
3)外连接
(都是以article表为基础的)
1、select * from article a left join category c on a.cid = c.cid;//左外连接
2、 select * from category c right join article a on a.cid = c.cid; 右外连接
因为mysql不支持全连接所以可以用其他的代替来实现
select * from article a left join category c on c.cid = a.cid
union
select * from category c right join article a on c.cid = a.cid;
子查询
in、exists、any、all的使用
1、select * from `article` where 'cid' in (select `cid` from `category` where `cname` in('科技','生活'));
//查询artice表中的cid在 (查询名字在(科技','生活')里的cid 在category)里的是
2、update `article` set `name`='修改标题' where `cid`=999 and exists
(select 1 from `category` where `cid`=999);
//在article表中 修改name的内容为修改标题 查询cid是999并且(查询category表中如果有cid=999这条数据的话就显示1;)
3、select * from `article` where `cid` =any(select `cid` from `category`)
// 如果文章表中有一些文章的 cid 在栏目表中不存在相应记录,则不会被查询出来。
4、ALL 关键字在使用时,只有满足内层查询语句返回的所有结果,才可以执行外层查询语句。