当前位置: 首页 > news >正文

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 = "西游记";

相关文章:

  • 关于笔记本电脑突然没有wifi图标解决方案
  • 数据采集+AI
  • 3. 轴指令(omron 机器自动化控制器)——>MC_GearInPos
  • d9-326
  • 第11章:优化I/O_《C++性能优化指南》_notes
  • MyBatis 语法不支持 having 节点
  • Linux实用操作及命令
  • 计算机二级WPS Office第十套WPS演示
  • CF254C Anagram
  • 初阶8 list
  • pycharm2024.1.1版本_jihuo
  • ①、环境准备-主流技术(IPS/FW/主备-主主快速切换)
  • Python+requests+ThreadPoolExecutor接口多线程抓取数据
  • 计算机二级WPS Office第八套WPS演示
  • STL之vector
  • K8s故障排查手册:从Pod崩溃到网络不通
  • 7.1 分治-快排专题:LeetCode 75. 颜色分类
  • 使用JAVA-使用GUI进行界面设计-进行维吉尼亚密码的解密与加密
  • 力扣hot100二刷——动态规划
  • 落地长沙市某三甲医院!麒麟信安云桌面再添建设标杆
  • 即日起,“应急使命·2025”演习公开征集新质救援能力
  • 武汉大学新闻与传播学院已由“80后”副院长吴世文主持工作
  • 居委业委居民群策群力,7位一级演员来到上海一小区唱戏
  • 美乌矿产协议签署被曝“临门一脚”时生变,美方提附加条件
  • 美乌矿产协议预计最早于今日签署
  • 中国武术协会原主席张耀庭逝世,曾促成电影《少林寺》拍摄