数据库基础知识
1、数据库概念
2、数据库知识大纲
2、1创建、查看、删除数据库
2、2创建表、查看表
表的列于列之间用逗号隔开
注意:‘%stu&’ stu前边是任意内容,后边也是任意内容,查看表名字中包含stu的
2、3删除表
#删除表
drop table student;
show tables;#查看所有的表
drop table student1,student2,student3;#可以删除多个表
2、4表的数据类型
Set:集合类型,只能选其中一个
enum:枚举类型,可以选里边一个或多个
有中文无法实现时可以数据格式化,加入字符集:charset=’utf-8’(解决数据库乱码的问题)
numeric(a,b):总位数a位,有b位小数。
Char:分配的空间固定,缺点浪费空间,有点节省时间
Varchar:先分配固定空间,用完剩下的再回收回去,优点节省空间,缺点浪费时间。
Date,time用集合类型或者枚举类型
集合类型(‘A,B’)A和B只能选一个
枚举类型(‘A,B,C’)可以选一个或多个。
2、5Mysql中的约束
非空约束:not null 不许为空;
唯一约束:Unique key:不允许重复;
唯一并且非空:unique key(空格) not null(可以写多个约束中间用空格隔开即可)
可以用primary key主键约束:默认唯一且非空
外键约束:foreign key,多张表中于主键配和
默认值约束:default ‘值’,当不能为空时默认填0
例:age int not null default ‘0’; insert into student(sname,age) value (‘张三’,null);
输出结果中age的值为0
auto_increment:自增,并且每一次都从最大序号的增加。
2、6修改表
1、修改表的字段,增加字段,修改字段,删除字段
# 修改表的字段,增加字段,修改字段,删除字段
alter table student;#修改表
#增加字段sex,并且使用集合类型,默认值为f column/`kɒləm/ n.柱;栏目;纵队;圆形石柱
网络列;专栏;行
add column sex set('f','m') default'f';
select * from student;#查看表
alter table student;#每回修改表之前都要加这一句
add column birth date after sname;#见表一
alter table student;
#在第一列增加sno,int类型,主键约束,自增,直接写在要怎加的字段之后
add column sno int primary key auto_increment first;#见表二
#可以增加多个字段,用逗号隔开,可以指定顺序
alter table student;
add column snop int first,add column sdirth date after sname;
#修改字段sex
alter table student;
modify column sex set('f','m','n')default 'n';#此时看不出变化
insert into student (sname) values('admin');#见表四
alter table student ;
modify column sex not null#是错误的,要把字段所有的内容都写全
#删除字段
alter table student;
drop column snop;
2、7插入数据
select * from student;
#插入数据标准语句
#sno、sex、age有默认值,插入数据可以不用写
insert into student(sno,sname,sex,age)values(3,'zhangfei','男',18);
insert into student(sanme)values('guanyu');
#插入数据可以不写字段名称,插入的数据要于字段名完全对应
insert into student(5,'liubei','男',23);
#插入默认值default
Insert into student(sname,sex)values('kongming','男');
#插入空值不知道哪个可以是空值可以查询:desc student
insert into student(sanme,sex)values('孔融',null)
#插入数据非标准语句,不要用这些操作只能在Mysql可以用
insert student(sanme,sex)values('lisi',default);#缺少into
insert into student(sanme,sex)values('lisi',default),('wusi','男');#并列插入多个数据
2、8更新数据
#更新数据 update 表名称 set 字段名=新值 where 字段名=值
#标准语句,支持一次修改多个数据
update student set sex='男'where sno=1;
update student set age=1 where sex='男';#将性别为男的年龄都改为1
#没有where操作,导致全表更新
update student set age=10;
#使用表达式更改
#使用null 当在set里时用=,当在where里时用is null
update student set sex=null where sex='男';
update student set age=10 where sex=null;#结果没有数据更新 是错误的
update student set age=10 where sex is null;#是正确的
update student set age=default where sno=10;
#运算表达式
update student set age=age+2 where sno=12;
#可以一次更新多个值
update student set='男',age=19 where sno=1;
2、9数据的删除
delete from student where sno=16;
#可以一次删除多个数据
delete from student where sex is null;
#删除全表
delete from student
#truncate table 只能删全表语句 不可以加where语句,删除后再插入数据序号从1开始
#delete把全表数据删除后,再插入数据接着之前的数据序号增加,可以加where语句
truncate table student
2.10 查询
1、单表查询(不带查询条件)
1.1全表查询
#查询学生表中有哪些数据
select * from student;
# select * :查询所有
1.2查询部分字段
#查看一部分 用户名,密码
select username,password from student;
select password ,username from student;#显示出来的表中字段顺序与查询时顺序一致
1.3查询带表达式
#查询哪些学生是单号和双号
select sno,sno%2,sname from student;
select sno if(sno%2=0,'双号','单号'),sname from student;
#查询序号加1000的
select sno,sno+1000,sname from student;
1.4查询设别名称
1.5查询排序
#排序 ORDER BY 对哪个字段进行排序,排序方式(asc升序。desc降序)
#select * from 表名 ORDER BY 字段名 排序方式(asc,desc)
#不写排序方式,默认为升序排序
#查询学生年龄,按点击次数升序排序
select * from student1 order by age asc;
#有相同的结果,要加第二排序项,就 ,字段名 排序方式
#select * from 表名 ORDER BY 字段名 排序方式,第二排序项字段名 排序方式
1.6查询部分数据(limit关键字)
#只查询一小部分数据
SELECT * from student1 limit 5;#查询前5行数据
SELECT * from student1 limit 5,3#从第6行开始,查询3行
#对显示的结果去重复
select distinct age from student1;
总结:
2、单表查询(带查询条件)
2.1等值查询
#等值查询
可以查询列和列相等,不加单引号,使两个字段名相等
update student1 set sno=18 where sno=1;
select * from student1 where sno=age;
2.2不等值查询
#不等值查询 > >= < <= != <>不等于
#查询学生sno>10的;
select * from student1 where sno>10;
#查询性别不等于男的
select * from student1 where sex !='男';
select * from student1 where sex <>'男';
2.3模糊查询
#模糊查询
#一定要like关键字,要求%关键字,表示任意内容
SELECT * from student1 where sname like 'zhang%';
#一定要like关键字,要求_关键字,一个_表示一个字符,而且只能匹配一个字符
SELECT * from student1 where sname like 'zhang___';
#多个模糊查询
SELECT * from student1
SELECT * from student1 where sno like '1%';
2.4逻辑语句(not),and or多条件查询
注意 and的优先级高
#查询zhang姓,女生的学生信息;
#两个条件都满足才会显示
select * from student1 where sname like 'zhang%'and sex='女';
#只要满足一个条件就可以显示
select * from student1 where sname like 'zhang%'or sex='女';
#满足两个里边选一个,and的优先级高
select * from student1 where (sname like 'zhang%'and sex='女' ) or (sname like 'wang%' and sex='女');
注意: 括号可以改变优先级
select * from student1 where sex='男' or sname like 'wang%' and sex='女';
select * from student1 where (sex='男' or sname like 'wang%') and sex='女';
#not的使用,第一种常用
select * from student1 where sname not like 'zhang%';
select * from student1 where not sname like 'zhang%';
2.5空值处理(is null ,is not null)
2.6区间查询(between and)
#查询学生年龄再18和21之间的,包括18和21
select * from student1 where age between 18 and 21;
select * from student1 where age >= 18 and age <=21;
#查询年龄在18~21之外的学生
select * from student1 where age not between 18 and 21;
2.7集合查询(in)
#查询学生年龄是18,19,20,21这几个数 where age =18 or age=20 or age=21;
select * from student1 where age in(18 ,20,21);
select * from student1 where age not in(18 ,20,21);
#查询学生sname包括zhang,age23 sname=wang,age=20;见下图
select * from student1 where (sname ,age)in(('zhangseng',23),('wangfang',20));
3、分组查询
3.1Mysql函数
注: ceil取大于的最小整数,floor取小于的最大整数,truncate(dv,1)取小数后一位,round取四舍五入
注:查询sname字段字符有两个的
注:lower将大写全部变成小写,不区分大小写(AdMin->admin)
注:计算年龄