随笔分类 -  sql

摘要:连接查询: 内连接:AB交集 隐式内连接 select t1.name, t1.age, t2.dname from emp t1, dept t2 where t1.dep_id=t2.did; --起别名简化书写 显式内连接 select * from emp join dept on emp. 阅读全文
posted @ 2023-02-28 15:30 大灰狼21 阅读(13) 评论(0) 推荐(0) 编辑
摘要:一对多: 在多的一方建立外键关联一的一方 多对多:(订单/商品) 建立第三张中间表,中间表至少包含两个外键,分别关联两个主键 中间表可以计数 一对一:(一张表拆成用户表和用户详情表) 在任意一方加入外键,关联另一方主键,设此外键unique 阅读全文
posted @ 2023-02-28 14:44 大灰狼21 阅读(35) 评论(0) 推荐(0) 编辑
摘要:drop table if exists emp; -- 建表emp create table emp ( id int primary key auto_increament; -- 主键且自增.数字且唯一 ename varchar(50) not null unique; -- 非空,唯一 j 阅读全文
posted @ 2023-02-28 10:57 大灰狼21 阅读(57) 评论(0) 推荐(0) 编辑
摘要:聚合函数 查班里一共多少学生: select count(id) from stu; -- count()统计的列名不能为null select count(*) from stu; -- 推荐写* 查班里数学最高/低分/平均分: select min(math) from stu; -- 聚合函数 阅读全文
posted @ 2023-02-28 09:40 大灰狼21 阅读(15) 评论(0) 推荐(0) 编辑
摘要:模糊查询 查姓马: select * from stu where name like '马%'; 查第二个字是花: select * from stu where name like '_花%'; -- 下划线占第一个位置 查名字里包含德:: select * from stu where nam 阅读全文
posted @ 2023-02-28 09:11 大灰狼21 阅读(15) 评论(0) 推荐(0) 编辑
摘要:基础查询: select name,age from stu; -- 查两列 select * from stu; -- 查全部,不推荐 select distinct address from stu; --查一列去除重复数据 select name,math as 数学成绩,english as 阅读全文
posted @ 2023-02-27 20:12 大灰狼21 阅读(16) 评论(0) 推荐(0) 编辑
摘要:给指定列添加数据: insert into stu(id,name) values(1,'张三'); insert into stu values(1,'张三'); -- 所有列时,列表可省略(不建议) insert into stu values(1,'张三'),(2,'李四'),(3,'王五') 阅读全文
posted @ 2023-02-27 19:39 大灰狼21 阅读(12) 评论(0) 推荐(0) 编辑
摘要:查询当前数据库下所有表名称: show tables; 查询表结构: desc user; 创建表: create table tb_user( id int, age int, score double(5,2), -- 小数点后两位,5-2=3,三位数100,0~100.00 birthday 阅读全文
posted @ 2023-02-27 19:16 大灰狼21 阅读(67) 评论(0) 推荐(0) 编辑
摘要:查询数据库: show databases; 创建数据库: create database db1; create database if not exist db1; 删除数据库: drop database db1; drop database if exists db1; 查看数据库: sel 阅读全文
posted @ 2023-02-27 18:12 大灰狼21 阅读(50) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示