MySQL表操作 insert-delete-update-select

本文介绍了SQL的基本操作,包括insert、delete、update、select等,并详细解释了如何使用这些命令进行数据的增删改查。此外还提供了alter table的具体用法,如添加、修改和删除列,以及重命名和删除表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.insert
2.delete
自定义变量删除
set@potter=(select author_id from authors where author_last=’rowling’ and author_first=’jk’);
delete from books where author_id=@potter;

3.update alter
alter table tbl_name change column a a int(4) unsigned zerofill;

4.select

简单查询

集合函数和分组查询

连接查询
select book_id,title concat(author_first,”,author_last) as author from books join authors using(author_id) where title like
‘%traveler%’ and title like ‘%winter%’;

检索数据、过滤数据where + 操作符

select top 5 name from tbl_name; – 查询前五行数据

select name frome tbl_name limit 5 offset 5; – 查询从第五行开始的5行数据

select name frome tbl_name where email is null;

数据查询结果排序 order by

组合where子句 is not or and in having like

逻辑查询处理顺序

创建后表的修改
alter table 语句用于创建后对表的修改, 基础用法如下:
添加列
基本形式: alter table 表名 add 列名 列数据类型 [after 插入位置];
示例:
在表的最后追加列 address: alter table students add address char(60);
在名为 age 的列后插入列 birthday: alter table students add birthday date after age;
修改列
基本形式: alter table 表名 change 列名称 列新名称 新数据类型;
示例:
将表 tel 列改名为 telphone: alter table students change tel telphone char(13) default “-“;
将 name 列的数据类型改为 char(16): alter table students change name name char(16) not null;
删除列
基本形式: alter table 表名 drop 列名称;
示例:
删除 birthday 列: alter table students drop birthday;
重命名表
基本形式: alter table 表名 rename 新表名;
示例:
重命名 students 表为 workmates: alter table students rename workmates;
删除整张表
基本形式: drop table 表名;
示例: 删除 workmates 表: drop table workmates;
删除整个数据库
基本形式: drop database 数据库名;
示例: 删除 samp_db 数据库: drop database samp_db;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值