Day12
DDL-数据库定义语言
概念
database defination language 数据库定义语言,用于定义数据库,用于定义表结构
表中字段基本数据类型
大致可以分为三类:数值、日期/时间和字符串(字符)类型
UTF-8:一个汉字=3个字节
GBK:一个汉字=2个字节
varchar(n) 表示 n 个字符,无论汉字和英文,Mysql 都能存入 n 个字符,仅是实际字节长度有所区别
数据库语句操作
在登陆 MySQL 服务后,使用 create 命令创建数据库,语法如下:
CREATE DATABASE 数据库名; //创建第一个数据库 mydb1 create database mydb1 //删除创建的数据库 drop database mydb1; //选择数据库 use mydb1 所有的数据库名,表名,表字段都是区分大小写的。所以你在使用SQL命令时需要输入正确的名称。 查看数据库创建细节 show create database mydb1; 创建一个使用gbk字符集的数据库 create database mydb2 character set gbk;
表结构语句操作
选择数据库 use mydb1; 创建MySQL数据表需要表名、表字段名、定义每个表字段 //创建表 create table student(id int,name varchar(20),sex varchar(20),age int,salary float(6,2),birthday date) //删除表 drop table student; //查看所有表 show tables //查看表的创建细节 show create table student; //展示表结构 desc student // 在原有的学生基础上添加address列 alter table student add address varchar(20) //在原有的学生基础上删除address列 alter table student drop address
定义表的约束
create table student(id int primary key auto_increment,name varchar(20) unique not null,sex varchar(20),age int,salery float(6,2),birthday date);
-
如果你不想字段为 NULL 可以设置字段的属性为 NOT NULL, 在操作数据库时如果输入该字段的数据为NULL ,就会报错。
-
AUTO_INCREMENT定义列为自增的属性,一般用于主键,数值会自动加1。
-
PRIMARY KEY关键字用于定义列为主键。为了标识数据库记录唯一性,不允许记录重复,且键值不能为空,主键也是一个特殊索引。 您可以使用多列来定义主键,列间以逗号分隔。
-
UNIQUE KEY的用途:主要是用来防止数据插入的时候重复的
-
ENGINE:设置存储引擎
-
CHARSET: 设置编码
DML -数据库操作语言
DML:data manipulation language 数据库操作语言,用以操作数据库。
插入数据
//插入数据 insert into student values(1,’zhangsan’,’nan’,19,389.10,’1999-10-10’); //查询 select * from student
插入中文报错: //修改表编码 alter table teacher convert to character set utf8; insert into student(id,name,sex,age) values(3,’王五’,’男’,19); //或者在建库的使用直接使用utf8格式创建 create database mydb2 character set utf8;
解决插入的中文查询后显示??的问题
show variables like "char%"; //会显示所有系统相关的编码格式 set character_set_client=gbk; set character_set_results=gbk;
删除数据
//删除单条数据 delete from student where id=1; //删除所有数据,不删除结构,会放到日志中,事务提交后才生效 delete from student; //摧毁表,删除表中所有数据,不删除结构,立即生效 truncate table student;
delete from student; 与 truncate table student; 都能删除该表中所有数据,区别:前者
删除后自增主键还在,后者主键会从1开始。
修改数据
//设置所有人的年龄加10岁 update student set age=age+10 //修改zhangsan 为张三 update student set name=’张三’ where name=’zhangsan’ //修改王五的salery和出生日期 update student set salery=100.01,birthday=’1999-10-10’ where id=3;
DQL-数据库查询语言
概念及作用:Data Query Language 数据库查询语言
数据查询
/删除student drop table student //创建数据库表-学生成绩表 create table student(id int primary key auto_increment,name varchar(20) unique not null,chinese float,english float,math float); //添加几条数据 insert into student values(1,’张三’,90,80,80); insert into student values(2,’李四’,90,87,60); insert into student values(3,’王五’,70,60,69); insert into student values(4,’赵六’,99,90,87); //查询所有学生信息 select * from student; //查询id为1的学生信息 select * from student where id=1; //查询id为1的学生姓名 select name from student where id=1; //查询数学成绩大于80的同学成绩 select * from student where math>80 //查询所有学生成绩,并输出效果为 姓名 语文 英语 数学 效果,见下图: select name as 姓名,chinese as 语文,english as 英语,math as 数学 from student //查询所有成绩及数学分+10分 select *,(math+10)from student //统计每个学生的总分 select name,(math+english+chinese) as 总分 from student //查询总分大于230分的同学 select * from student where (math+english+chinese)>230 //查询数学成绩在80-90之间的同学 select * from student where math between 80 and 90 //查询数学语文英语都大于80的同学成绩 or或者 and和/并且 select * from student where math>80 and english>80 and chinese >80; //查询数学成绩在 80 60 90内的同学,即数学成绩有60、80、90的。 select * from student where math in(80,60,90); //模糊查询 //查询所有姓名中包含张的同学 select * from student where name like ‘%张%’
排序查询
MySQL中 升序为asc,降序为desc
升序:select * from 表名 order by 表中的字段 asc(MySQL中默认是升序排列,可不写) ; 降序:select * from 表名 order by 表中的字段 desc ;
若要进行同时一个升序一个降序 例如:
order by 升序字段 asc,降序字段 desc ;
//按照数学成绩从小到大查询 select * from student order by math; //按照数学成绩从大到小查询select * from student order by math desc;
分页查询
limit是mysql的分页查询语法:
select * from table limit m,n
其中m是指记录从m+1开始,,N代表取n条记录。
//取出第3条至第6条,4条记录 select * from student limit 2,4 //查询出数学成绩由高到低前两名 select * from student order by math desc limit 0,2;
分组查询
分组查询得到结果是第一次查到的某个组别。
//创建一个订单表 create table employee(id int,name varchar(20),sex varchar(20),age int); insert into employee values(1,'sunsan','男',18); insert into employee values(2,'lisi','男',18); insert into employee values(3,'wangwu','女',19); insert into employee values(4,'zhaoliu','男',15); //分组查询 select * from employee group by sex; //分组查询加条件 select * from employee group by sex having age>18;
(1) having 条件表达式:用来分组查询后指定一些条件来输出查询结果
(2) having作用和where一样,但having只能用于group by
报表查询
count 个数 sum 总数 avg 平均数 max 最大值 min 最小值
//统计班级里边有多少学生 select count(*)from student; //统计总成绩大于250分的人数 select count(*)from student where (math+english+chinese)>250; //统计班级里边各科总成绩 select sum(math),sum(english),sum(chinese) from student //统计所有科目的总成绩 select sum(math+english+chinese) from student; //统计一下语文平均成绩 select sum(chinese)/count(*) from student; //average select avg(chinese) from student; //统计一下班级语文最高分和最低分 select max(chinese) from student; select min(chinese) from student; //报表查询订单根据名称合并后,总价格>10000的商品 select * from orders group by product having sum(price) >7000
数据控制语言
数据控制语言:简称【DCL】(Data Control Language),用来定义数据库的访问权限和安全级别,及创
建用户;关键字:grant等