java 数据库 常用指令

 更详细可跳转大佬: MySQL基本用法(IDEA中操作MySQL)_idea中怎么用mysql?-CSDN博客

-- 注释
-- ------------------------------------DDL语句操作数据库--------------------------
-- 创建数据库: create database 数据库名 [character set 字符编码][collate 校对规则];-- []表示可选
-- 需求: 创建名称为day14_1的数据库,默认编码为utf8
create database day14_1;
-- 需求: 创建名称为day14_2的数据库,指定编码为gbk
create database day14_2 character set gbk;

-- 查看所有数据库: show databases;
-- 查看数据库结构: show create database 数据库名;
-- 需求:查询所有的数据库
show databases ;
-- 需求: 查看day14_1数据库的定义结构
show create database day14_1;
-- 需求: 查看day14_2数据库的定义结构
show create database day14_2;

# 修改数据库: alter database 数据库名 character set 字符编码;
# 	  注意:1.数据库名不能修改 2.只能修改数据库的编码,是utf8,不是utf-8
-- 需求:把day14_2数据库的编码修改为utf8
alter database day14_2 character set utf8;
show create database day14_2;

# 删除数据库: drop database 数据库名;
# 需求: 删除day14_2数据库
drop database day14_2;

# 其他操作:
#   切换数据库:  use 数据库名;
# 	查看正在使用的数据库: select database();
use test;
select database();
use day14_1;
select database();

# --------------------------------DDL语句操作表----------------------------
# 创建表的语法:
#   create table 表名(字段名 字段类型 字段约束,....);
#   子类类型: int,bigint,boolean,double/char(长度)/varchar(长度),date,datetime
# 创建一张用户表表(含有id字段,用户名字段,密码字段. id为主键自动增长)
create table users(
    id int primary key auto_increment,
    username varchar(40) not null,
    password varchar(40)
);


# 查看所有的表: show tables;
# 查看表的定义结构: desc  表名;
-- 练习:查看day14_1数据库中所有的表
show tables ;
-- 练习:查看day14_1数据库中users表的结构
desc users;

# 修改表:
# #### 语法
# - 增加一列:  `alter table 表名 add 字段名 字段类型 [字段约束];`
# - 修改列的类型约束:`alter table 表名 modify 字段名 字段类型 字段约束;`
# - 修改列的名称:`alter table 表名 change 旧列名  新列名 字段类型 字段约束;`
# - 删除一列: `alter table 表名 drop 字段名;`
# - 修改表名: `rename table 旧表名 to 新表名;`
# #### 练习
create table student(
    id int primary key auto_increment,
    name varchar(40) not null ,
    sex varchar(30)
);
# - 给学生表增加一个grade字段
alter table student add grade varchar(30);
desc student;

# - 给学生表的sex字段改成字符串类型
alter table student modify sex char(2);

# - 给学生表的grade字段修改成class字段
alter table student change grade class varchar(30);
desc student;

# - 把class字段删除
alter table student drop class;


# - 把学生表修改成老师表(了解)
rename table student to teacher;


# 语法:  `drop table 表名;`
-- 练习: 删除teacher表
drop table teacher;


# ------------------------------DML操作数据-------------------------------

# 准备数据:
create table product(
    pid int primary key auto_increment,  -- 只有设置了auto_increment id列才可以赋值为null
    pname varchar(40) not null,
    price double,
    num int
);


# 新增记录:

# 插入指定列: insert into 表名(列名,列名,...)  values(值,值,...);

#  练习: 指定pname,price列插入记录
insert into product(pname,price) values('Mac',9000);

# -- 注意:

# --  字段名与值的类型、个数、顺序要一一对应。

# --  值不要超出列定义的长度。

# --  插入的日期和字符串,使用引号括起来(单双引号)。

# --  插入特定的列:没有赋值的列,系统自动赋为null(前提是当前列没有设置not null 约束,否则会报错)

# 插入所有列: insert into 表名 values(值,值,...);

#  注意:默认所有列插入,values里面必须给表中每一个字段赋值,一般主键给一个null
-- 练习: 指定所有列插入记录
insert into product values(null,'iPhone',6000,2);
insert into product value(null,'iPhone',6000,2);

# 批量插入记录
insert into product values(null,'苹果电脑',18000.0,10);
insert into product values(null,'华为5G手机',30000,20);
insert into product values(null,'小米手机',1800,30);
insert into product values(null,'iPhonex',8000,10);
insert into product values(null,'苹果电脑',8000,100);
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);

insert into product values(null,'苹果电脑',18000.0,10),
(null,'华为5G手机',30000,20),
(null,'小米手机',1800,30),
(null,'iPhonex',8000,10),
(null,'苹果电脑',8000,100),
(null,'iPhone7',6000,200),
(null,'iPhone6s',4000,1000),
(null,'iPhone6',3500,100),
(null,'iPhone5s',3000,100),
(null,'方便面',4.5,1000),
(null,'咖啡',11,200),
(null,'矿泉水',3,500);

insert into product values(null,'苹果电脑',8000,3);

# - 语法:  `update  表名 set 字段名=值,字段名=值,... where 条件; `

# - 练习

#   - 将所有商品的价格修改为5000元
update product set price = 6000;

#   - 将商品名是Mac的价格修改为18000元
update product set price = 18000 where pname = 'Mac';

#   - 将商品名是Mac的价格修改为17000,数量修改为5
update product set price = 17000,num=5 where pname = 'Mac';

#   - 将商品名是方便面的商品的价格在原有基础上增加2元
update product set price = price + 2 where pname = '方便面';

# 语法
#   方式一:  `delete from 表名 where 条件;`
#   方式二:  `truncate table 表名;`
# - 练习
#   - 删除表中名称为’Mac’的记录
delete from product where pname='Mac';
#   - 删除价格小于6001的商品记录
delete from product where price < 6001;
#   - 删除表中的所有记录
delete from product;
truncate table product;

# ------------------------------DQL语句操作记录------------------------------
# 查询所有: select * from 表名;
-- 练习:查询product表中所有的信息
select * from product;

# 指定字段查询: select 字段名,字段名,... from 表名
-- 练习:查询product表中pname,price字段的值
select pname,price from product;

# 去重查询:select distinct 字段名 from 表名
-- 练习:去重查询pname字段的值
select distinct pname from product;
-- 注意:去重查询distinct前面不能有其他字段名
# select price,distinct pname from product;-- 报错

# 取别名查询:select 字段名 as 别名,字段名 as 别名 from 表名 as 别名
-- 练习:对pname,price取别名查询
select pname as 商品名称,price as 商品价格 from product as p;
select pname  商品名称,price  商品价格 from product  p;

# 列运算查询(+,-,*,/等):  select 列运算 from 表名;
-- 练习: 查询每件商品的总金额
select price*num from product;

# - 基本条件查询: select  ...  from 表名 where  条件;

# 条件:  比较运算符: `>   >=   <    <=    =  <>`
-- 练习: 查询price大于4000的商品信息
select * from product where price > 4000;

# 条件:   between...and...  范围
-- 练习: 查询price在4000到8000之间的商品信息
select * from product where price between 4000 and 8000;

# 条件:  in(值,值,...)   范围
-- 练习: 查询pid为1,3,5,7,9,11,13的商品信息
select * from product where pid in(1,3,5,7,9,11,13);

#   like模糊
#        _  :  匹配一个字符
#    	 %:   匹配0个到多个字符(大于等于0个)
-- 练习: 查询商品名称为iPh开头的所有商品信息
select * from product where pname like 'iPh%';

-- 练习: 查询商品名称含有手机的所有商品信息
select * from product where pname like '%手机%';

-- 练习: 查询商品名称为iPh开头,然后iPh后面有4位的所有商品信息
select * from product where pname like 'iPh____';

# 条件:  逻辑运算符: and,or,not
-- 练习: 查询price在4000到8000之间的商品信息
select * from product where price >= 4000 and price <= 8000;

-- 练习: 查询price大于4000或者小于1000之间的商品信息
select * from product where price > 4000 or price < 1000;

-- 练习: 查询pid不为1,3,5,7,9,11,13的商品信息
select * from product where pid not in(1,3,5,7,9,11,13);

# # 创建学生表(有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 表名 order by 字段名 [asc|desc];
# 方式二: select ... from 表名 order by 字段名 [asc|desc],字段名 [asc|desc];
# 注意:asc:升序,desc:降序,不指定默认是升序
# 练习
# 1. 练习: 以分数降序查询所有的学生
select * from student order by score desc ;

# 2. 练习: 以分数降序查询所有的学生, 如果分数一致,再以age降序
select * from student order by score desc,age desc ;


# 语法: SELECT 聚合函数(列名) FROM 表名;
# 注意:  聚合函数会忽略空值NULL

-- 练习:求出学生表里面的最高分数
select max(score) from student;
-- 练习:求出学生表里面的最低分数
select min(score) from student;
-- 练习:求出学生表里面的分数的总和
select sum(score) from student;-- 726
-- 练习:求出学生表里面的平均分
select avg(score) from student;-- 80.66666666666667
-- 练习:统计学生的总人数
select count(score) from student;-- 9

-- 修改: 把sname为wb的score修改为null
update student set score = null where sname = 'wb';

-- 练习:统计学生的总人数
select count(score) from student;-- 8
select count(*) from student;-- 9


-- 练习:求出学生表里面的分数的总和
select sum(score) from student;-- 627.5
-- 练习:求出学生表里面的平均分
select avg(score) from student;-- 78.4375-----错了,真正的平均分应该是69.7222222222
# ifnull(参数1,参数2)
# 如果不想忽略空值null,就使用ifnull(参数1,参数2)函数,进行判断
# 如果参数1为null,就取参数2的值,如果参数1不为null,就取参数1的值
select avg(ifnull(score,0)) from student;-- 69.72222222222223



# 语法: select ... from 表名 [where 条件] [group by 分组字段] [having 条件]
#     练习:
#     1. 练习:根据性别分组,统计男生的总人数和女生的总人数
select * from student group by sex;
# 注意事项
# **单独分组 没有意义,因为 返回每一组的第一条记录**
# **分组的目的一般为了做统计使用, 所以经常和聚合函数一起使用**
select count(*) from student group by sex;
# **分组查询如果不查询出分组字段的值,就无法得知结果属于那组**
select sex,count(*) from student group by sex;

#     2. 练习根据性别分组, 统计每一组学生的总人数> 5的(分组后筛选)
select sex,count(*) from student group by sex having count(*) > 5;

select sex,count(*) from student where sid in(1,2,3,4,5,6,7) group by sex having count(*) >= 5;

# select ... from 表名 limit a,b;
# a:从哪里开始查询, 从0开始计数 ,省略a不写,默认就是从0开始
# b:查询的数量【固定的,自定义的】
# 练习:
-- 练习: 查询sid为1到4--->第1页
select * from student limit 0,4;

-- 练习: 查询sid为5到8--->第2页
select * from student limit 4,4;

-- 练习: 查询sid为9到12--->第3页
select * from student limit 8,4;
# 规律:
# select * from student limit (页码-1)*每页显示的条数,每页显示的条数;

create database 数据名;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值