外联约束关系的取消mysql_关系型数据库MySQL(一)_增删改查

本文详细介绍了SQL数据库的基本操作,包括创建表、添加主键、数据类型、约束以及删除操作。此外,还涵盖了表的数据更新、查询、聚合函数、视图、事务处理和各种查询条件,如模糊查询、范围查询等,深入讲解了SQL在数据管理中的应用。
摘要由CSDN通过智能技术生成

1、创建表单

create table product

(product_id  char(4)  not null,

product_name  varchar(100)  not null,

sale_price  integer,

regist_date  date,

primary key(product_id));

2、创建表后添加主键

alter table product add primary key(product_id);

3、数据类型

integer 整型

char 定长字符串型

varchar 可变字符串型

date 日期型

4、约束

not null 非空约束

primary key 主键约束

5、表的删除

drop table product;   --删除表结构和数据,不可恢复

delete from product;   --删除表数据,留下表结构

6、表数据删除

delete from product where sale_price=4000;   --删除特定数据

7、表的更新

rename table product to new_product;   --表的重命名

alter table product add column product_type varchar(50);   --添加列

alter table product drop column product_type;   --删除列

alter table product modify column product_id char(8);   --修改列的类型

alter table product change column sale_price new_sale_price integer;   --修改列名

8、表数据更新

insert into product values('0001', '办公桌', null, 100);   --插入一条数据

insert into new_product select * from product;   --复制其他的表中的所有数据

update product set product_id='0001' where sale_price=500;

update product set sale_price=20,purchase_sale=50 where product_id='0002';   --多列更新

在workbench中运行上两条语句会报错,关闭安全模式即可: set sql_safe_updates=0;

9、列的查询

select product_id as id from product;   --列的别名

select distinct product_type from product;   --删除重复行

10、聚合查询

select count(sale_price) from product;    --计算null之外的行数

select count(distinct sale_price) from product;   --计算不同价格的行数

11、聚合函数

count(对行而言)

sum, avg, min, max(对列而言,会排除null)

12、对表进行分组

select product_type,count(*) from product group by product_type;   --按商品类型进行分组

select sale_price,count(*) from product where product_type='衣服' group by sale_price;

13、为聚合结果指定条件

select product_type from product group by product_type having count(*)=2;   --分组后为2行的组

14、排序查询

select * from product order by sale_price;   --按价格升序

select * from product order by sale_price desc;   --按价格降序

15、事务

start transaction;

DML语句1; DML语句2;DML语句3;...........;

commit/rollback;

定义:需要在同一个处理单元中执行的一系列更新处理的集合

commit: 一旦提交,就无法恢复到事务开始前的状态

rollback: 一旦回滚,数据库就会恢复到事务开始之前的状态

数据定义DDL: create, drop, alter

数据操纵DML: select, insert, update, delete

数据控制DCL: commit, rollback, grant, revoke

16、视图

create view productview(product_type,count_type)

as

select product_type,count(*) from product group by product_type;

定义: 已经保存好的select语句,'参照视图'='执行相应的select语句'

drop view productview(product_type,count_type);   --删除视图

17、谓词

like: 模糊查询(%: 0个以上的任意字符串, _: 任意1个字符串)

between: 范围查询

is null, is not null: 判断是否为null

in: or的简便用法—in(值1,值2,...)

exists: 判断是否存在满足某种条件的记录

18、其他

SQL的注释符:-- 一行,/* */ 多行

算术运算符:+, -, *, /, abs(数值), mod(被除数,除数), round(对象数值,保留小数位数)

逻辑运算符:not > and > or

比较运算符:>= 、 <= 、 <> 、 =

对null不能使用比较运算符,需用is null 或者 is not null

字符串函数:||拼接函数, length字符串长度函数, lower小写转换函数, upper大写转换函数

日期函数:current_time当前时间函数, current_date当前日期函数

19、表的加法(并集)

select product_name from product1

union

select product_name from product2;

union后面加个all,则可以保留重复行

表的减法(差集): except, 语法与union一样

表格公共部分(交集): intersect, 语法与union一样

20、内联结

以列为单位,选取同时存在于两张表中的数据

select sp.shop_id,sp.shop_name,p.product_id,p.product_name,

from shopproduct as sp inner join product as p

on sp.product_id=p.product_id;

把以上语句当成是一张新表,就可以在后面使用where,group by,having,order by语句

21、外联结

选取出主表中的全部信息,指定主表的关键字是left/right

select sp.shop_id,sp.shop_name,p.product_id,p.product_name,

from shopproduct as sp left outer join product as p

on sp.product_id=p.product_id;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值