当前位置:网站首页>【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;
/*******************************************************/
边栏推荐
- 1414. minimum number of Fibonacci numbers with sum K
- My resume.
- Implementation of Ackermann function with simulated recursion
- 【视频课】android studio物联网APP设计制作全套教程--国庆期间全掌握
- 工具笔记 —— 常用自定义工具类(正则,随机数等)
- 如何使用android studio制作一个阿里云物联网APP
- 测试工程师如何转型测开
- 【mysql进阶】索引分类及索引优化方案(五)
- QA of some high frequency problems in oauth2 learning
- The original Xiaoyuan personal blog project that has been around for a month is open source (the blog has basic functions, including background management)
猜你喜欢

Create a small root heap and judge the node relationship (also.C\u str() substr(),atoi(),string. Use of find())

Alibaba cloud development board haas510 submission device attributes

Design of PLC intelligent slave station based on PROFIBUS DP protocol

Is MySQL query limit 1000,10 as fast as limit 10? How to crack deep paging

Player actual combat 22 to solve the problems of flower screen and Caton

Implementation and debug of process hiding under x64

Leetcode 2176. 统计数组中相等且可以被整除的数对

如果要打造品牌知名度,可以选择什么出价策略?

程序分析与优化 - 6 循环优化

Leetcode 2185. Counts the string containing the given prefix
随机推荐
Binary tree traversal
Cmake basic tutorial - 02 b-hello-cmake
拆改广告机---业余解压
Remote code injection
Fourteen week assignment
阿里云开发板HaaS510解析串口JSON数据并发送属性
Player practice 15 xdemux and avcodecparameters
Alibaba cloud development board haas510 sends the serial port data to the Internet of things platform
简述CGI与FASTCGI区别
Mold and remainder
SystemC common errors
Codeforces Round #798 (Div. 2)(A~D)
Implementation and debug of process hiding under x64
Player actual combat 23 decoding thread
TestEngine with ID ‘junit-vintage‘ failed to discover tests
OAuth2学习中的一些高频问题的QA
通信流量分析
Leetcode 2176. 统计数组中相等且可以被整除的数对
Use of pytorch (to be supplemented)
Postgresql14 installation and use tutorial