当前位置:网站首页>【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;
/*******************************************************/
边栏推荐
- For cross-border e-commerce, the bidding strategy focusing more on revenue - Google SEM
- Alibaba Cloud Development Board haas510 submission Device Properties
- 拆改广告机---业余解压
- Running phase of SystemC
- The original Xiaoyuan personal blog project that has been around for a month is open source (the blog has basic functions, including background management)
- Dial up and Ethernet
- 对于跨境电商,更侧重收入的出价策略 —Google SEM
- Alibaba cloud development board haas510 parses serial port JSON data and sends attributes
- How to use Android studio to create an Alibaba cloud Internet of things app
- 什么是自动出价?它的优势是什么?
猜你喜欢

Player practice 18 xresample

Alibaba cloud development board haas510 sends the serial port data to the Internet of things platform

Mémoire de l'examen d'entrée à l'université

Mold and remainder

Player actual combat 14 display YUV

SystemC common errors

Llvm pass-- virtual function protection

What is automatic bidding? What are its advantages?

阿里云开发板HaaS510报送设备属性

如何使用android studio制作一个阿里云物联网APP
随机推荐
Create a small root heap and judge the node relationship (also.C\u str() substr(),atoi(),string. Use of find())
Greed issues - Egypt scores
atomic and exclusive operation
Player actual combat 16 xdecode class
Create a slice slice pit using the make method
What is automatic bidding? What are its advantages?
Tcp/ip network communication knowledge record
969. pancake sorting
Player practice 15 xdemux and avcodecparameters
Now you must know the pointer
Alibaba cloud development board haas510 connects to the Internet of things platform -- Haas essay solicitation
阿里云开发板HaaS510报送设备属性
How to package QT program learning records with inno setup
3. Process concealment under the ring ----- continuous concealment and new opening prevention
Player actual combat 13 create qtopengl project to promote window control and reload qoopenglwedge
Is Shell Scripting really a big technology?
SystemC simulation scheduling mechanism
Acwing: topology sequence
Backtracking: Prime Rings
Binary tree traversal