数据库的复习总结

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值