您的位置:首页 > 其它

day1学习总结

2019-01-02 21:24 99 查看

RDBMS术语
数据库:一些关联表的集合。
数据表:表是数据的矩阵。
主键:主键是唯一的,一个数据表只能包含一个主键。
外键:外键用于关联两个表。
数据库的三大范式:
数据库的第一范式:每一列都是最小的原子单位,不可以进行再分裂;
数据库的第二范式:每一张表都是描述的一个事物,都必须与主键建立关系,不能够出现局部依赖
数据库的第三范式:遵循这两种规则。
创建表类
//创建班级表
create table classes(
cid int(4) primary key auto_increment,
cname varchar(20),
cnum int(20)
);
//创建学生表
create table students(
sid int(4) primary key auto_increment,
sgender char(4),
sname varchar(20),
sage int(20),
cid int(4)
);
//创建科目表类
create table projects(
pid int(4) primary key auto_increment,
pname varchar(20),
sid int(4)
);
//相关知识
select *from classs as c,stutends s where c.cid=s, cid and cid=1;
alter table student rename as xrs(需要修改的名字)

show tables;(查看当前数据下所有的表)

desc+表名 xrs(查看表结构)

alter table xrs add sphone varchar(20);(添加一个属性, 列)

修改列的名字与类型
alter table classs change snum cnum int(20);
alter table xrs change sphone spho varchar(30);
update xrs set sage=21 where condition;

alter table xrs drop spho;(删除指定一列)

插入数据
insert into xrs values(1,“xrs”,“1”,80,90);

insert into xrs values(2,“xdh”,“2”,81,80);

insert into xrs values(3,“czh”,“3”,81,82);

insert into xrs values(4,“zw”,“4”,81,99);

insert into xrs values(7,“ls”,“5”,81,92,2);

insert into xrs (sname,sage)value(“mm”,20);
insert into xrs values(7,“zz1”,“5”,81,92),(8,“zz2”,“5”,81,92),(9,“zz3”,“5”,81,92);
查看当前表中数据;
select * from xrs;

修改表中指定键ID的属性
update xrs set sname=“xdh”,sage=20 where sid=4;
update xrs set sname=“wxc”,sage=21,sgender=1 where sid=5;
alter table xrs add sgender char;

删除表中指定键ID的属性。
delete from xrs where sid=7;

查找表中数据
与and
select sname,sid,sgender from xrs where scores=80 and snum=81;
或or
select sname,sid from xrs where snum=81 or scores=92;

分数大于等于60;
select sname,sid from xrs where scores>=60;

alter table xrs add sproject varchar(20);

性别不为空;(不为空)
select *from xrs where sgender is not null;

update xrs set sproject=“java”;

update xrs set sproject=“web” where sid=4;

查询不同学科;
select distinct sproject from xrs;

模糊查询
使用关键字like。通配符_,%;
select *from xrs where sname like"%x%";

insert into xrs values(11,“add”,“21”,“1”,89,2,“ui”);

select *from xrs where sname like “w_”;

给表加别名
select *from xrs as s;

select s.sname as “aa”,s.project as “bb” from xrs as s;

ifnull()函数

这函数是判断第一个元素是否为空,如果null返回的是第二个元素,其他返回第一个元素;

select count(sname) from xrs;

select count(sname) as “总记录数” from xrs;

select max(scores) as “最高的分数” from xrs;
select min(scores) as “最低的分数” from xrs;
select avg(scores) as “平均分” from xrs;

分页查询
limit 第一个参数是当前的索引(从0开始),第二个是页量
select *from xrs limit 2,2;

limit 索引=((当前页-1))*页量。

select *from xrs limit 9,2;

升序:Asc
select sname from xrs order by sname asc;
降序:desc
select sname from xrs order by sname desc;
select sage from xrs order by sage desc;

select current_time();//时间

select current_date();//日期
select now();当前日期时间
select concat(“dd”,“ff”);//拼接

select concat(“d”,“f”,“3”);不同类型

select ucase(“Fj”);全大写
select lcase(“Fj”);全小写
select left(“fujiawei”,3);截取左边三个;
select right(“sdsaf”,4);截取右边四个;
select length(“1243445”);长度
select substring(“xierishi”,1,3);截取第一个字节长度三;
select trim(" ff ");去除空格
select abs(-8);绝对值
select format(“58.9898”);取整不进行四舍五入
select rand();随机数

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: