码迷,mamicode.com
首页 > 数据库 > 详细

mysql 数据库的操作

时间:2019-12-31 10:57:52      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:comm   arc   scribe   student   过滤   权限   order   唯一索引   外链   

–数据库的创建
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
————————————————

mysql 数据库的操作

标签:comm   arc   scribe   student   过滤   权限   order   唯一索引   外链   

原文地址:https://www.cnblogs.com/liuxiaofu/p/12122746.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!