#外键用于表达两个表格之间存在某种关系,用于将两个表格连接在一起。
drop table if exists logs;
create table logs
(
id int primary key auto_increment,
login_time datetime,
u_id int,
foreign key (u_id) references users (id)
);
# foreign key (列名1) references 表名(列名2)
# 列名1一定是当前表格中被选择作为外键的列名,表名一定是另外一张表格的名称,列名2一定是另一个表格中主键
# foreign key表示外键,括号里就是被选中作为外键的列名,references表示引用,后面放另一个表格的名字和主键的列名
#复合主键,当一列不足以区分某一个行的时候,可以考虑用过多个列组成复合主键
drop table if exists users1;
create table users1
(
name varchar(20),
age int,
primary key (name, age)
);
#过滤重复行 distinct关键字经常在面试中考到
select distinct name
from users;
#排序
select *
from users
order by id desc; #asc是升序排序(是默认的,不写asc就是升序,从小到达),desc降序排序
select *
from users
order by
外键用于表格
最新推荐文章于 2022-11-20 11:34:24 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)