MySQL数据库表的约束,关联及查询

一.表的约束

1.设置t_hero的主键为id
复制代码
mysql> alter table t_hero add primary key(id);
2.设置t_hero id属性非空
复制代码
mysql> alter table t_hero modify id int not null;
3.设置name属性为非空非重复
复制代码
mysql> alter table t_hero modify name varchar(255) not null;
mysql> alter table t_hero add unique (name);
4.设置检查约束
复制代码
mysql> alter table t_hero add constraint chk_gender check(gender in ('男', '女'));
mysql> desc t_hero;

二.查询语句练习

1.基本查询语句
复制代码
-- 查询所有数据
select * from t_hero;
-- 查询需要的字段信息
select id, name, password from t_hero;
-- 查询一个字段,一个等值条件
select name from t_hero where id = 1;
-- select 字段列表  from 表名称  where 条件

2.聚合函数使用
复制代码
mysql> select count(*) from t_hero;
+----------+
| count(*) |
+----------+
|        4 |
+----------+
1 row in set (0.00 sec)
3.查询时的别称使用
复制代码
mysql> select count(*) 总数 from t_hero;
+------+
| 总数 |
+------+
|    4 |
+------+
1 row in set (0.00 sec)
4.常见条件查询
复制代码
mysql> select id,name,address from t_hero where id in (2,4);
+----+----------+---------+
| id | name     | address |
+----+----------+---------+
|  2 | 林黛玉   | 贾府    |
|  4 | 紫霞仙子 | 盘丝洞  |
+----+----------+---------+
2 rows in set (0.00 sec)
5.模糊查询
复制代码
mysql> select * from t_hero where name like "林%";
+----+--------+----------+------+--------+---------+------------+-------+
| id | name   | nickname | age  | gender | address | weapon     | types |
+----+--------+----------+------+--------+---------+------------+-------+
|  2 | 林黛玉 | 林妹妹   |   18 | 女     | 贾府    | 倒拔垂杨柳 | 人类  |
+----+--------+----------+------+--------+---------+------------+-------+
1 row in set (0.00 sec)
6.逻辑运算符and
复制代码
mysql> select id,name,address from t_hero where name like "林%" and address = "贾府";
+----+--------+---------+
| id | name   | address |
+----+--------+---------+
|  2 | 林黛玉 | 贾府    |
+----+--------+---------+
1 row in set (0.00 sec)
7. 分组查询
复制代码
mysql> select nickname from t_hero group by nickname;
+----------+
| nickname |
+----------+
| 齐天大圣 |
| 林妹妹   |
| 黑旋风   |
| 盘丝大仙 |
+----------+
4 rows in set (0.00 sec)
8.结果排序
复制代码
mysql> select * from t_hero where id <= 3 order by id;
+----+--------+----------+------+--------+---------+------------+-------+
| id | name   | nickname | age  | gender | address | weapon     | types |
+----+--------+----------+------+--------+---------+------------+-------+
|  1 | 孙悟空 | 齐天大圣 | 1000 | 男     | 花果山  | 如意金箍棒 | 猴子  |
|  2 | 林黛玉 | 林妹妹   |   18 | 女     | 贾府    | 倒拔垂杨柳 | 人类  |
|  3 | 李逵   | 黑旋风   |   30 | 男     | 沂州    | 两柄板斧   | 人类  |
+----+--------+----------+------+--------+---------+------------+-------+
3 rows in set (0.00 sec)

三.外键与多表关联

1.创建用于外键关联的表
复制代码
mysql> select * from typesName;
+----+----------+
| id | name     |
+----+----------+
|  1 | 猴子   |
|  2 | 人类   |
|  3 | 神仙   |
+----+----------+
2.创建外键相关字段
复制代码
mysql> alter table t_hero add bookNum int;
3.多表关联
复制代码
mysql> alter table t_hero
    -> ADD CONSTRAINT fk_hero_book
    -> foreign key(bookNum) references bookName(id);
 
mysql> UPDATE t_hero
    -> SET heroNum = 1
    -> WHERE book = "西游记";
 
mysql> UPDATE t_hero
    -> SET heroNum = 2
    -> WHERE book = "红楼梦";
 
mysql> UPDATE t_hero
    -> SET heroNum = 3
    -> WHERE book = "水浒传";
 
mysql> UPDATE t_hero
    -> SET heroNum = 4
    -> WHERE book = "西游记";
相关推荐
啦啦啦_99998 分钟前
Redis-5-doFormatAsync()方法
数据库·redis·c#
生产队队长16 分钟前
Redis:Windows环境安装Redis,并将 Redis 进程注册为服务
数据库·redis·缓存
老邓计算机毕设17 分钟前
SSM找学互助系统52568(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·ssm 框架·javaweb 毕业设计
痴儿哈哈20 分钟前
自动化机器学习(AutoML)库TPOT使用指南
jvm·数据库·python
洛豳枭薰43 分钟前
Innodb一次更新动作
mysql
Σίσυφος19001 小时前
PCL法向量估计 之 方向约束法向量(Orientation Guided Normal)
数据库
老毛肚1 小时前
手写mybatis
java·数据库·mybatis
海山数据库1 小时前
移动云大云海山数据库(He3DB)postgresql_anonymizer插件原理介绍与安装
数据库·he3db·大云海山数据库·移动云数据库
l1t1 小时前
DeepSeek总结的PostgreSQL的GPT推理SQL移植到DuckDB的性能优化方法
sql·gpt·postgresql
云飞云共享云桌面1 小时前
高性能图形工作站的资源如何共享给10个SolidWorks研发设计用
linux·运维·服务器·前端·网络·数据库·人工智能