当前位置:网站首页>数据库的复习总结
数据库的复习总结
2022-06-27 12:05:00 【渔夫阿布】
1.操作数据库
数据库的创建,数据库的选定,删除数据库,查看所有的数据库,查看正在使用的数据库
#创建数据库(默认字符集)
create database day01;
create database day01_0;
#选定数据库
use day01;
use day01_0;
#查看正在使用的数据库
select database();
#删除指定的数据库
drop database day01_0;
#查看所有的数据库
show databases;
2.操作表
表的创建
增加表中的字段
修改字段中的属性
修改字段的名称
删除字段
显示所有的表
显示表的结构
#创建一个学生表
create table t_stu(
stu_id int primary key auto_increment ,
stu_name varchar(20) unique,
stu_gerder char(1) not null
);
#为t_stu表增加一个字段
alter table t_stu add stu_grade varchar(20) not null;
#将t_stu表stu_grade字段类型修改为int
alter table t_stu modify stu_grade varchar(20);
#将t_stu表stu_grade字段名修改为class
alter table t_stu change stu_grade class varchar(20) not null;
#将t_stu表中的class字段删除
alter table t_stu drop class;
#显示所有的表
show tables;
#显示表的结构
desc t_stu;
#删除表
drop table t_stu;
3.操作表中的记录
准备工作
create table product(
pid int primary key auto_increment,
pname varchar(40) ,
price double ,
num int
);
3.1增删改
增加
insert into product values(null,'苹果电脑',18000.0,10);
insert into product values(null,'华为5G手机',3000,20);
insert into product values(null,'小米手机',1800,30);
insert into product values(null,'iPhonex',8000,10);
insert into product values(null,'iPhone7',6000,200);
insert into product values(null,'iPhone6s',4000,1000);
insert into product values(null,'iPhone6',3500,100);
insert into product values(null,'iPhone5s',3000,100);
insert into product values(null,'方便面',4.5,1000);
insert into product values(null,'咖啡',11,200);
insert into product values(null,'矿泉水',3,500);
修改
#修改操作
#将所用的价格设置为8000
update product set price=800 ;
#将苹果电脑的price设置为18000
update product set price=18000 where pname='苹果电脑';
#将小米手机的price设置为1800,num设置为10
update product set price=1800,num=10 where pname='小米手机';
#将苹果电脑的价格下降1000
update product set price=price-20 where pname='苹果电脑';
删除
#删除所用的记录
delete from product01;
delete from product01 where pname='香蕉';
3.2查询
简单查询
#简单查询
#查询所有的数据
select * from product;
#查询特定的列(字段)
select pid, pname, price from product;
#别名查询
select pname 产品名称 ,price 价格 from product ;
#去重查询distinct
select distinct price,num from product;
#运算查询
select price+180000 更改之后的价格,num from product;
条件查询
#条件查询where
select * from product where price>800 or num>200;
select * from product where price>=800 and num=200;
#模糊select * from product where pname like "iPh%";
#范围
select * from product where pid not in(1,2,6,7);
select * from product where pid not between 2 and 5;查询like
排序查询
#环境准备
# 创建学生表(有sid,学生姓名,学生性别,学生年龄,分数列,其中sid为主键自动增长)
CREATE TABLE student(
sid INT PRIMARY KEY auto_increment,
sname VARCHAR(40),
sex VARCHAR(10),
age INT,
score DOUBLE
);
INSERT INTO student VALUES(null,'zs','男',18,98.5);
INSERT INTO student VALUES(null,'ls','女',18,96.5);
INSERT INTO student VALUES(null,'ww','男',15,50.5);
INSERT INTO student VALUES(null,'zl','女',20,98.5);
INSERT INTO student VALUES(null,'tq','男',18,60.5);
INSERT INTO student VALUES(null,'wb','男',38,98.5);
INSERT INTO student VALUES(null,'小丽','男',18,100);
INSERT INTO student VALUES(null,'小红','女',28,28);
INSERT INTO student VALUES(null,'小强','男',21,95);
select * from student;
#排序查询
select * from student order by score asc;
聚合函数
#聚合函数
#求出score最小值
select min(score) from student;
#求出score最大值
select max(score) from student;
#求出score总和
select sum(score) from student;
#求出平均值
select avg(score) from student;
#计算记录的个数
select count(*) from student;
聚合函数不考虑null
边栏推荐
- Thinkphp6 interface limits user access frequency
- 【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(二)
- This privatized deployed enterprise knowledge base makes telecommuting a zero distance
- Dm8: Dameng database - lock timeout
- 私藏干货分享:关于企业架构中如何进行平台化
- Building crud applications in golang
- 一个有趣的网络掩码的实验
- Dynamic programming [III] (interval DP) stone merging
- 树莓派 3b+ 学习
- 想学好C语言,操作符也很重要
猜你喜欢
解除百度文库VIP、语雀、知乎付费限制,原来这么简单
一个有趣的网络掩码的实验
nmcli team bridge 基本配置
In 2021, the global carbon graphite brush revenue is about US $2366million, and it is expected to reach US $2701.8 million in 2028
Uniapp drop-down layer selection box effect demo (sorting)
Histrix工作原理
Tidb 6.0: making Tso more efficient tidb Book rush
ACL 2022 | 中科院提出TAMT:TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络
Neo4j:入门基础(一)之安装与使用
AI for Science:科研范式、开源平台和产业形态
随机推荐
Microservice splitting
盘点一些好用且小众的 Markdown 编辑器
解压 .log.gz 文件
最短编辑距离(线性dp写法)
【On nacos】快速上手 Nacos
MIT6.031 软件构造 Reading7阅读笔记Designing Specifications(设计规范)
Configuration of YML
动态规划【三】(区间dp)石子合并
Jianmu continuous integration platform v2.5.0 release
Research Report on the overall scale, major producers, major regions, products and application segments of swine vaccine in the global market in 2022
TiDB 6.0:让 TSO 更高效丨TiDB Book Rush
uni-app 使用escook/request-miniprogram插件发请求说明
如何修改 node_modules 裏的文件
Interviewer: with the for loop, why do you need foreach?
面试突击60:什么情况会导致 MySQL 索引失效?
JMeter connection DM8
对象序列化
怎么找相同台词的影视片段?这8个电影搜索神器,一句台词找到对应片段
Deep understanding of happens before principle
. Net6 access skywalking link tracking complete process