MYSQL从入门到入土

show databases;//查看所有数据库
use 数据库名字;//使用某个数据库
select * from tablename;//查看数据中某个表的数据
select * from tablename where ID=1;//显示ID为1的表中数据
create database test;//创建一个名字叫test的数据库
show tables;//查看数据库中存在的所有数据表

CREATE TABLE pet(
name varchar(20),
sex CHAR(1),
birth date);//创建数据表

describe pet;//查看pet这个数据表的结构

insert into pet
values(‘tom’, ‘n’, ‘2000-1-1’);//插入数据

mysql常用数据类型:

mysql大致支持三种类型,数值,日期,字符串

delete from len_pet where name = ‘a’;//删除数据
update len_pet set name=‘猫猫咪’;//修改数据

mysql建立表格的限制条件
主键约束:
它能够唯一的确定表中的一条记录,也就是我们通过给某个字段添加约束,就可以是得该字段不重复不为空
比如:
create table user(
id int primary key,
name varchar(20));
联合主键primary key(id, name)虽然有两个主键,但是只要两个主键加在一起不相同就可以,且联合主键都不能为空

自增约束:

create table user(
id int primary key auto_increment,
name varchar(20));

insert into user(name) values(‘aaaaa’);//这个意思是指定字段,就是向name中插入名字
发现虽然只向名字字段插入姓名,但是id变成了1,再次插入变成了2,也就是id的值可以自动增长

如果忘记添加主键约束了可以使用如下的语句
alter table user add primary key(id);//将id设置为主键值

删除主键值
alter table user drop primary key;

修改主键值
alter table user modify id int primary key;

唯一约束:
alter table user5 add unique (name);
也可以
create table user(
id int,
name varchar(20) unique
);

删除唯一约束
alter table user drop index name;

添加唯一约束
alter table user modify name varchar(20) unique;

非空约束:
create table user(
id int,
name varchar(20) not null
);

默认约束:
当我们插入字段,如果没有传值,就会使用默认值
create table user(
id int,
name varchar(20),
age int default 10
);

外键约束:
涉及到两个表:子表,父表
或者 主表 副表

比如
班级
create table class(
id int primary key,
name varchar(20)
);

学生
create table student(
id int primary key,
name varchar(20),
class_id int,
foreign key(class_id) references class(id)
);
子表引用了父表中的数据时,父表中的数据时不允许被删除的

三大范式2020.9.18的理解
1.一个表中的数据每个数据都是不可分割的原子项
2.表中的所有数据都必须约束于一个主键值
3.表中的所有数据都必须直接的约束于一个主键值
三个范式都是在满足前一个的情况下讨论的

查询数据
只想查询某几个字段
select sname ssex from student;

select distinct depart from teacher;//查找teacher数据表中的depart字段,且显示的数据不会重复
select * from score where degree between 60 and 80;//查询分数在60到80之间的数据

运算符比较
select * from score where degree between degree > 60 and degree <80;

想要查询指定成绩的数据
select * from score where degree in(85, 86, 87);//显示成绩为这几个的数据

还有
select * from student where class=‘xxxxx’ or ssex =‘girl’;

升序与降序
select * from student order by class desc;降序
asc就是升序

按照degree降序,cno升序排列
select * from score order by cno asc, degree desc;条件越靠前优先级越高,也就是说排出来之后cno是一定符合要求的但是desc可能会因为cno而有些错乱

查询人数
select count(*) from student where class=‘95021’;

查找最高分学生的学号和课程号
select sno,cno from where degree=(select max(degree) from score);

select sno,cno,degree from score order by degree desc limit 0,1;

查询每门课程的平均成绩
select avg(degree) from score where cno=‘3-111’;
或者
select cno,avg(degree) from score where group by cno;
或者
select cno,avg(degree) from score where group by cno having count
(cno)>=2 and cno like ‘3%’;
having之后跟条件 like模糊查询

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值