mysql数据库基础知识整合复习总结
(上为样表)
day01
创建学生表
create table student(
id int not null,
name varchar(20) not null,
sex varchar(4),
age int
);
插入两条记录
insert into student values(1,'张三','男',21),
(2,'李四','男',20);
插入指定列值
insert into student (id,name,sex) values(3,'王五','男');
去重
select distinct sex from student;
并列多重条件选择
select * from student where age=21 or id=3 or name='李四';
根据age排序默认升序(由小到大),升序:asc,降序:desc, null:默认为最小
select * from student order by age desc;
更新属性值:年龄
update student set age=19 where id=3;
删除一条记录
delete from student where id = 6;
筛选出表中前n条记录:limit n
select * from student limit 3;
选出名字带小的学生
select * from student where name like '%小%';
选出姓张的两个字的学生
select * from student where name like '张_';
选出年龄为19,20的学生
select * from student where age in(19,20);
区间选择
select * from student where id between 3 and 7;
指定别名,可不写as,指定后,不可使用原表名:如student.name,只能使用别名
select stu.name from student as stu where age=21;
inner join(内连接) == join(连接) left join(左连接) == left outer join(左外连接)
right join(右连接) == right outer join(右外连接) full join(全连接) == full outer join(全外连接)
UNION 操作符用于合并两个或多个 SELECT 语句的结果集。union 操作符选取不同的值。如果允许重复的值,请使用 union all。
select id,name from student where age=19 union select id,name from student where age=21;
创建备份学生表
create table student_log(
id int not null,
name varchar(20) not null,
sex varchar(4),
age int
);
mysql不支持select ... into 新表 from 旧表 只能备份如下:
insert into student_log select * from student where student.age =21;
create database 数据库名; create table 表名();
not null:只能在建表时,放于句末。
UNIQUE 约束唯一标识数据库表中的每条记录。请注意,每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束。
建表时: (放于所有属性列后)
唯一性约束:unique (属性1)/constraint 约束名 unique(属性1,属性2...)
主键约束:primary key (属性1)/constraint 约束名 primary key(属性1,属性2...)
’从表‘中添加外键约束:foreign key (从表属性1) references 主表(主表主键)/constraint 约束名 foreign key (从表属性1) references 主表(主表主键)
check约束:check (语句)/constraint 约束名 check (语句)
(特殊)default约束:default 默认值; 同not null,放于属性的 数据类型后边。
(特殊)自增约束:auto_increment;同not null,只能放于属性的 数据类型后边。开始值是 1,每条新记录递增 1。要让其以其他值开始,这么设:alter table 表名 auto_increment=数字值;插入记录时,无需插入该列数据,默认会自动增加该列数据。
建表后:
添加:
唯一性约束:alter table 表名 add unique(属性1)/alter table 表名 add constraint 约束名 unique (属性1,属性2)
主键约束:alter table 表名 add primary key(属性1)/alter table 表名 add constraint 约束名 primary key (属性1,属性2)
外键约束:alter table 表名 add foreign key (从表属性1) references 主表 (主表主键)/alter table 表名 add constraint 约束名 foreign key (从表属性1) references 主表(主表主键)
check约束:alter table 表名 add check (语句)/alter table 表名 add constraint 约束名 check (语句)
(特殊)default约束:alter table 表名 alter 属性列名1 set default 默认值;
撤销:
唯一性约束:alter table 表名 drop index 约束名/自动生成的约束名
主键约束:alter table 表名 drop primary key
外键约束:alter table 表名 drop index 约束名/列名(如果是未指定约束名)
check约束:alter table 表名 drop check
(特殊)default约束:alter table 表名 alter 属性列名1 drop default;
alter table student add unique (id);
insert into student values(3,'徐玉荣','女',22);
alter table student add unique (id,name);
alter table student drop index id_2;
alter table student add unique (name,sex);
alter table student drop index name;
alter table student add constraint id_sex unique (id,sex);
alter table student drop index id_sex;
alter table student add constraint id_name primary key (id,name);
alter table student drop primary key;
创建成绩单表
create table blank(
bid int not null,
math int,
english int,
sid int
);
-- alter table blank add primary key (bid);
-- 添加外键 alter table blank add foreign key (sid) references student (id);
insert into blank values(1,76,87,23);
Error Code : 1452
Cannot add or update a child row: a foreign key constraint fails (`classstudy`.`blank`, CONSTRAINT `blank_ibfk_1` FOREIGN KEY (`sid`) REFERENCES `student` (`id`))
-- alter table blank drop foreign key blank_ibfk_1;
-- alter table blank drop index sid ;
-- alter table blank add constraint temp foreign key (math) references student (id);
-- alter table blank drop foreign key temp;
-- alter table blank drop index temp;
-- insert into blank values(1,76,87,23);
-- alter table student add check(age>=18 and sex in ('男','女'));
-- insert into student values (10,'李超','男',15);-- 插入记录失败 Check constraint 'student_chk_1' is violated.
-- alter table student add constraint ages check(age!=100);
-- alter table drop check ages;-- student_chk_1;
-- alter table student alter age set default 24;
-- insert into student (id,name,sex) values (10,'李超','男');
-- alter table student alter age drop default;
-- insert into student (id,name,sex) values (11,'李君怀','男');
撤销索引:alter table 表名 drop index 索引名
删除表(表的结构、属性以及索引也会被删除):drop table 表名;
删除数据库:drop database 数据库名;
(仅仅删除表格中的数据)结构还在:truncate table 表名;
-- truncate table student_log;
ALTER TABLE 语句用于在已有的表中添加、修改或删除列。
alter table 表名 add 新增列名 数据类型;
alter table 表名 drop column 原列名1;
alter table 表名 modify 列名1 新数据类型;
alter table 表名 change 原列名1 新列名1 新列名1的数据类型();
-- alter table student_log add birthday date;
-- alter table student_log drop column birthday ;
-- alter table student_log change age birth varchar(20);
-- insert into student_log values(1,'张三','男','148');
-- alter table student_log modify birth smallint;
-- insert into student_log values(2,'网三','男','男');
day02
内建 SQL 函数的语法是:SELECT function(列) FROM 表
函数的基本类型是:
Aggregate 合计函数:函数的操作面向一系列的值,并返回一个单一的值。
Scalar 函数:函数的操作面向某个单一的值,并返回基于输入值的一个单一的值。
-- count(column_name) 函数返回指定列的值的数目(null 不计入)
-- 去重计数 select count(distinct sex) from student ;
-- avg 函数返回数值列的平均值。NULL 值不包括在计算中。
-- 选择年龄大于全体学生平均年龄的学生并按年龄升序排序 select * from student where age>(select avg(age) from student) order by age;
-- select first(name) as first_name from student; 报错,mysql不支持first()函数
-- select last(name) as last_name from student; 报错,mysql不支持last()函数
-- select max(age) as max_age from student; max() 函数返回一列中的最大值。NULL 值不包括在计算中。
-- select min(age) as min_age from student; min() 函数返回一列中的最小值。NULL 值不包括在计算中。
-- select sum(age) as 总年龄和 from student; sum() 函数返回数值列的总数(总额)。
-- select sum(age) as 总年龄和 from student;
-- 可以对一个或多个以上的列应用 GROUP BY 语句,合计函数(比如 SUM)常常需要添加 group by 语句。
-- select sex 性别,age 年龄,count(*) 合计人数 from student group by sex,age;
-- 在 SQL 中增加 HAVING 子句原因是,where 关键字无法与合计函数一起使用。分组后不是一张表,而是每组都相当于一个视图,只能用having筛选出符合要求的。
-- select sex 性别,age 年龄,count(*) 合计人数 from student group by sex,age having sex='男';
-- alter table student add hometown varchar(20);
-- select name 姓名,ucase(hometown) 家乡 from student; ucase()函数把字段的值转换为大写。
-- select name 姓名,lcase(hometown) 家乡 from student; lcase()函数把字段的值转换为小写。
-- mid()函数用于从文本字段中提取字符。 select mid(列名,start[,length]) from 表名;start:起始值是1,必须有,length可选。
-- select distinct mid(name,1,1) 姓氏 from student; 选取学生中共有多少姓氏。
-- select length(name) from student;
round()函数用于把数值字段四舍五入,舍入为指定的小数位数。 select round(列名,decimal) from 表名; decimal:小数位数。
-- alter table student add math double;
-- select id 编号,name 姓名,round(math,1) 数学成绩 from student;
select id 编号,name 姓名,now() 时间 from student;
-- format(列名,格式)函数用于对字段的显示进行格式化。
-- 将表student修改为id自增的
-- alter table student modify id int auto_increment;
-- insert into student(name,sex,age,hometown,math) values('张飞','女',21,'puyang',99);
-- 让自增列的值以其他值开始: alter table 表名 auto_increment=预期初始值;
-- alter table student auto_increment =20;
-- insert into student(name,sex,age,hometown,math) values('张羽凡','男',21,'puyang',59);
-- 视图是可视化的表。视图包含行和列,就像一个真实的表。视图中的字段就是来自一个或多个数据库中的真实的表中的字段。视图总是显示最近的数据。
-- 创建视图(在views文件夹下新增一项视图) create view 视图名 as select 语句; create view id_name_math as select id,name,math from student where sex='男';
-- 查询视图 select * from id_name_math;
-- 更新视图,视图名不变,更改select语句,alter view 视图名 as select 新语句;如: alter view id_name_math as select id,name from student where sex='男';
-- 重新查询视图 select * from id_name_math;
-- 撤销视图 drop view 视图名; drop view id_name_math;
如果您希望使查询简单且更易维护,那么请不要在日期中使用时间部分!
null 用作未知的或不适用的值的占位符。测试null值:
select * from student where age is null;
select * from student where age is not null;
注释:无法比较 NULL 和 0;它们是不等价的。
-- ifnull(列名,代替null的值)==coalesce(列名,代替null的值);效果一样。coalesce:合并,联合。
-- select age*coalesce(math,2) ,age*ifnull(math,0) from student;
-- 可以在表中创建索引,以便更加快速高效地查询数据。用户无法看到索引,它们只能被用来加速搜索/查询。更新一个包含索引的表需要比更新一个没有索引的表更多的时间,这是由于索引本身也需要更新。因此,理想的做法是仅仅在常常被搜索的列(以及表)上面创建索引。
-- 创建索引 create index 索引名 on 表名 (列名1[,列名2,...] [desc/asc])/create unique index 索引名 on 表名 (列名1[,列名2,...]) 在表上创建一个唯一的索引。唯一的索引意味着两个行不能拥有相同的索引值。
-- create index tm on student (id);
在 MySQL 中,有三种主要的数据类型:文本、数字和日期/时间类型。
IBM 公司发明了 RDBMS。
RDBMS 是 SQL 的基础,也是所有现代数据库系统的基础。DBMS - 数据库管理系统(Database Management System)数据库管理系统是一种可以访问数据库中数据的计算机程序。DBMS 使我们有能力在数据库中提取、修改或者存贮信息。现代的 SQL 服务器构建在 RDBMS 之上。
学了之后,如果不经常使用,过了不久便会忘记,学会总结,善于总结是一种习惯!以便于忘了之后,回来翻看便能迅速拾起知识!!!
多动手,多总结!
多动手,多总结!
多动手,多总结!