当前位置:网站首页>【MySQL】数据库基本操作
【MySQL】数据库基本操作
2022-06-12 14:08:00 【语音不识别】
1 数据库 增删改查
/*******************************************************/
# 增: 创建数据库
create database if not exists sun character set utf8;
# 删除数据库
drop database sun_db;
# 改: 使用数据库
use sun;
# 查: 查询正在使用的数据库 查询所有数据库
show databases ;
select database();
/*******************************************************/
2 数据库表 增删改查
/*******************************************************/
# 增: 增加表 create table 表名 (字段 数据类型 约束)
create table cs(
uid int primary key,
name varchar(100) not null ,
age int
);
# 删: 删除数据库表
drop table cs;
# 查: 查询 数据库表
show tables ;
desc cs1;
# 改: 修改数据库表名
rename table cs to cs1;
/*******************************************************/
3 数据表字段:增删改查
/*******************************************************/
# 增 : 增加字段
alter table cs add `desc` varchar(20);
# 删 : 删除字段
alter table cs drop age;
# 改: 修改字段名称
alter table cs change age ages int;
alter table cs change ages age int not null unique ;
alter table cs change uid uid int auto_increment;
# 查: 查询表中的字段
desc cs;
/*******************************************************/
4 数据表数据:增删改查
/*******************************************************/
# 增: 增加数据
insert into cs (uid, name, `desc`) values (null,'小子','帅气');
insert into cs values (null,'小子1','牛逼');
insert into cs values (null,'小子2','8888'),(null,'小子3','8888'),(null,'小子4','8888');
insert into cs values (null,'小子5','8888'),(null,'小子6','8888'),(null,'小子7','8888');
# 删: 删除数据
delete from cs where uid=3;
delete from cs; # 注意: 全部删除 不重置主键自增
truncate cs; # 注意: 全部删除 重置主键自增
# 改: 修改数据
update cs set `desc`='9999' where uid = 5;
update cs set `desc`='9999'; # 注意: 不加条件 全部修改
# 查询
select * from product;
select p.pname as "商品",p.price as "价格" from product as p ;
select pname as "商品",price as "价格" from product;
# 条件查询 where
select * from product where price > 300;
select * from product where price >= 300;
select * from product where price < 300;
select * from product where price <= 300;
select * from product where price != 300;
select * from product where price <> 300;
# 逻辑查询 and or not
select * from product where price >200 and price<3000;
select * from product where price <200 or price>3000;
select * from product where not (price>200 and price<3000);
# 空判断
select * from product where category_id is null;
select * from product where category_id is not null;
# 排序查询
select * from product order by price;
select * from product order by price,category_id;
select * from product order by price desc ,category_id desc ;
select * from product order by price asc,category_id asc;
# 模糊查询 between and ,like % _ ,in
select * from product where price between 200 and 3000;
select * from product where price in (200,600);
select * from product where pname like "__斯";
select * from product where pname like "%斯%";
select * from product where pname like "%霸%";
select * from product where pname like "___";
# 聚合查询 count max min sum avg
select count(*) from product;
select count(*) from product where price>300;
select max(price) from product;
select min(price) from product;
select sum(price) from product;
select avg(price) from product;
select round(avg(price),5) from product;
# 分组查询 group by having 条件
select category_id from product group by category_id;
select category_id,sum(price) from product group by category_id;
select category_id,sum(price) from product group by category_id having sum(price)>300;
# 分页查询 limit m,n
select price,pname from product where price>300 order by price desc ;
select price,pname from product where price>300 order by price desc limit 0,3;
/*******************************************************/
边栏推荐
- Create a small root heap and judge the node relationship (also.C\u str() substr(),atoi(),string. Use of find())
- Leetcode 2176. 统计数组中相等且可以被整除的数对
- 高考回忆录
- Comparator summary
- Details of bypassing safeseh mechanism by using modules that do not enable safeseh
- If you want to build brand awareness, what bidding strategy can you choose?
- Leetcode 2185. 统计包含给定前缀的字符串
- Brush one question every day /537 Complex multiplication
- TestEngine with ID ‘junit-vintage‘ failed to discover tests
- chapter19 Allocation
猜你喜欢

Crack WinRAR to ad pop-up window

对于跨境电商,更侧重收入的出价策略 —Google SEM

Socket model of punctual atom stm32f429 core board

NotePad 常用设置

Alibaba cloud development board haas510 connects to the Internet of things platform -- Haas essay solicitation

Postgresql14 installation and use tutorial

Pay attention to click and pursue more users to enter the website. What bidding strategy can you choose?

Player practice 17 xvideowidget

Player actual combat 25 unpacking module add close

简述CGI与FASTCGI区别
随机推荐
[semidrive source code analysis] [x9 chip startup process] 25 - Introduction to mailbox inter core communication mechanism (code analysis) rpmsg-ipcc RTOS & QNX
What is the default gateway
Is MySQL query limit 1000,10 as fast as limit 10? How to crack deep paging
Redis data deletion policy in 2022
Mémoire de l'examen d'entrée à l'université
SystemC common errors
【mysql进阶】索引分类及索引优化方案(五)
Player practice 26 adding slider and window maximization
Bridging and net
公司运营中更注重转化的出价策略,如何实现? —Google sem
Briefly describe the difference between CGI and fastcgi
阿里云开发板HaaS510解析串口JSON数据并发送属性
Player actual combat 21 audio and video synchronization
Tlm/systemc: TLM socket binding problem
Summary of virtual box usage problems
3. Hidden processes under the ring
Redis核心配置和高级数据类型
阿里云开发板HaaS510将串口获取数据发送到物联网平台
Redis core configuration and advanced data types
Alibaba cloud development board haas510 connects to the Internet of things platform -- Haas essay solicitation