当前位置:网站首页>数据库的复习总结
数据库的复习总结
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
边栏推荐
猜你喜欢

剑指 Offer 04. 二维数组中的查找

数学知识——博弈论(巴什博奕、尼姆博奕、威佐夫博奕)思路及例题

How to participate in openharmony code contribution

Online bidding of Oracle project management system

1. Mx6ull startup mode

亚马逊测评掉评、留不上评是怎么回事呢?要如何应对?

Shell script learning notes

picocli-入门

nmcli team bridge 基本配置
![[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (II)](/img/ce/b58e436e739a96b3ba6d2d33cf8675.png)
[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (II)
随机推荐
ssh工作流程及原理
Nifi from introduction to practice (nanny level tutorial) - identity authentication
自定义多线程基类threading.Event
Raspberry pie 3b+ learning
dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article
A brief talk on cordola tree
微服务拆分
Jwas: a Bayesian based GWAS and GS software developed by Julia
居家办公被催之后才明白的时间管理
namespace ‘rlang’ 0.2.0 is being loaded, but >= 0.3.0 is required
This privatized deployed enterprise knowledge base makes telecommuting a zero distance
pull request
application. Configuration information of properties
Word text box page feed
[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (II)
Time management understood after being urged to work at home
私藏干货分享:关于企业架构中如何进行平台化
JMETER连接DM8
Thinkphp6 interface limits user access frequency
i. Construction of mx6ull C language environment