mysql 数据库的操作

Wesley13
• 阅读 614

–数据库的创建
create database goods1;//创建数据库goods1;
create database goods2 if not exists goods1;//创建数据库goods1,如果不存在就创建
create database goods3 default character set utf8 collate utf8_general_ci;//设置数据库的语言
create database goods4 default character set gbk collate gbk_chinese_ci;

–数据库的显示
show databases;

–数据库的删除
drop database goods1;

–指定当前的数据库
use goods;

–创建数据库表
create table user1(
id int(4) not null comment ‘编号’ auto_increment primary key,
user_name varchar(32) not null comment ‘账号’,
password varchar(64) not null comment ‘密码’
)comment ‘用户’;

–显示数据库中的表
show tables;

–查看表的结构
desc user1;
describe user1;

–删除数据库的表
drop table user1;
drop table if exists user1;

–修改表的名字
alter table user1 rename user2;

–表中添加一个字段
alter table user2 add sex1 varchar(2) not null;

–该表表中字段
alter table user2 change sex1 sex varchar(1) not null;

–删除表中字段
alter table user2 drop sex;

–设置编码格式
set names gbk;//gbk:指当前命令行的编码格式

–添加数据
insert into user(user_name,password,sex,true_name) value(‘user1’,‘123’,‘女’,‘张三1’);
insert into user(user_name,password,sex,true_name) values
(‘user2’,‘123’,‘女’,‘张三2’),
(‘user3’,‘123’,‘女’,‘张三3’),
(‘user4’,‘123’,‘女’,‘张三4’);

insert into user value(10000,‘user10000’,‘123’,‘男’,‘张三-10000’);

–更新数据
update user
set sex=‘男’,true_name =‘张刚’,password=‘123456’
where id =10000;

–删除数据
delete from user where id = 10000;

–创建账号
use mysql;
insert into user(user,host,password) values(‘yuanqingjian’,’%’,password(‘1234’));

–分派权限
grant all on . to yuanqingjian@’%’ identified by ‘1234’;

–立即生效
flush privileges;

– 设置主键约束
alter table user2 add primary key(id);//针对id建主键
alter table user2 drop primary key;//针对id删除主键

–设置外键约束
alter table user2 add primary key(id);
alter table score add
constraint fk_user_id foreign key(user_id) references user2 (id);
//针对user2表的id字段
alter table score drop foreign key fk_user_id;//删除外键
alter table score drop index fk_user_id;//删除索引

–添加唯一约束
alter table user2 add unique index_user_name
(user_name);//针对user_name添加唯一索引

alter table user2 drop index index_user_name;//删除索引

–查询语句
select id,user_name,true_name,age,birthday from user2 where
user_name like ‘zs’

from:指定数据集,可以是多个表或视图; where:过滤条件;可以有多个匹配单元;确定结果数据的行数; select:展示模式;

–展示全部字段
select * from user2 where user_name like ‘zs’

–展示部分字段
select user_name,true_name from user2 where user_name like ‘zs’

–运算
select user_name,true_name,age+5 from user2 where user_name like ‘zs’

–指定别名
select u2.* from user2 as u2 where u2.user_name like ‘zs’ select u2.*
from user2 u2 where u2.user_name like ‘zs’

and 逻辑与
or 逻辑或

常见的运算符
like 只能用于字符串 和模糊查询

=
<
<=
is null
is not null
between v1 and v2 一般用于时间
in ()括号里面的值可以是多种数据类型

–前导模糊查询
select user_name,true_name from user2 where user_name like ‘张%’

- -后导模糊查询
select user_name,true_name from user2 where user_name like ‘%三’

–完全模糊查询
select user_name,true_name from user2 where user_name like ‘%晓%’

select user_name,true_name from user2 where age between 25 snd 18

–排除重复
select distinct user_name,true_name,age,birthday from user2

–数据排序
select * from user2 order by age//默认升序
select * from user2 order by age asc//默认升序
select * from user2 order by age desc//降序

select id,user_name,true_name,age,age-1 as real_age,birthday from
user2 order by real_age//对real_age进行排序 select
id,user_name,true_name,age,age-1 real_age,birthday from user2 order by
age//对age进行排序

//order by 在展示之后排序

–多重排序
select id,user_name,true_name,age,age-1 as real_age,birthday from
user2 order by real_age,id desc//先对real_age进行排序,如果相同,在降序

补充:
Navicat 命令行使用

–删除数据
delete from user where id =1000; truncate table user;//截断表 把数据区和结构区分离

–改变表中字段
alter table user2 change sex1 sex varchar(1) not null;//改变表名,字段名
alter table user2 modify sex1 varchar(2) not null;137187487091;

–分页查询 //指定页面尺寸 显示页面行数
select * from user2 where id<10000 limit 2,2//限制 起始点从0开始 到 显示多少行

pageSize = 2 //页面尺寸 pageIndex = 2 //页面序号从0开始

beginIndex = (pageIndex-1)*pageSize pageSize = pageSize

pageSize = 4 //页面尺寸 pageIndex = 2 //页面序号从0开始

beginIndex = (pageIndex-1)*pageSize=(2-1)*4 =4

select * from user2 where id<10000 limit 4,2

select * from user2 where id<10000 limit 4

第一个参数:起始点序号 第二个参数:页面尺寸

只写一个参数:页面尺寸,起始点是0

–删除数据
delete from user2 where user_name like ‘zs’;
delete from user2 where user_name like ‘zs’ and id >5;
delete from user2 where id in(1,4,8);

–别名用法
select u2.* from user2 as u2 where u2.id <10000; select u2.* from
user2 u2 where u2.id <10000;

select true_name,(age - 1) as real_age from user2 where id<1000;
select true_name,(age - 1) as 年龄 from user2 where id<1000;//as 可有可无
select true_name,(age - 1) as “年 龄” from user2 where
id<1000;//中文别名用双引号 select true_name,(age - 1) as ‘年 龄’ from user2
where id<1000;//中文别名用单引号

–多表查询
//查询test_id = 1 的所有学生成绩:
select t1.id, t1.true_name, t2.grade
from student t1,score t2 where t1.id = t2.student_id and
t2.test_id = 1

–内连查询
select t1.id, t1.true_name, t2.grade from student t1
inner join score t2 on t2.student_id = t1.id where t2.test_id=1

–左外链接
select t1.id, t1.true_name, t2.grade from student t1 left
outer join score t2 on t2.student_id = t1.id

–右外连接
select t1.id, t1.true_name, t2.grade from student t1 right
outer join score t2 on t2.student_id = t1.id

–子查询
a1: select avg(grade) from score where test_id = 1

select t1.true_name, t2.grade, t2.test_id from student t1 inner
join score t2 on t2.student_id = t1.id where t2.test_id = 1 and
t2.grade >a1

select t1.true_name, t2.grade, t2.test_id from student t1 inner
join score t2 on t2.student_id = t1.id where t2.test_id = 1 and
t2.grade >( select avg(grade) from score where test_id =1 )

from子句中的子查询:
第一次考试的成绩: select t1.true_name, t2.grade from student t1 left join
score t2 on t2.student_id = t1.id and t2.test_id = 1;

第二次考试的成绩: select t1.true_name, t2.grade from student t1 left join
score t2 on t2.student_id = t1.id and t2.test_id = 2; select
t1.true_name,g1.grade grade1,g2.grade grade2 from student t1 left
join ( select t1.id, t1.true_name, t2.grade from
student t1 left join score t2 on t2.student_id = t1.id and
t2.test_id = 1 ) g1 on g1.id = t1.id left join ( select t1.id,
t1.true_name, t2.grade from student t1 left join score
t2 on t2.student_id = t1.id and t2.test_id = 2 ) g2 on g2.id = t1.id
————————————————

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
Python3:sqlalchemy对mysql数据库操作,非sql语句
Python3:sqlalchemy对mysql数据库操作,非sql语句python3authorlizmdatetime2018020110:00:00coding:utf8'''
Stella981 Stella981
3年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
为什么mysql不推荐使用雪花ID作为主键
作者:毛辰飞背景在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采用uuid,使用uuid究
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这