数据库MySQL指令

数据库之搭建

1、rpm -qa|grep 服务名称

案例:rpm -qa|grep mysql

2、将所有msyql的包删除干净

删除方法:

(1)yum remove mysql * 删除linux中的数据库

(2)yum erase 包名 ,删除linux中的数据库

(3)rpm -e --nodeps 包名 删除linux中的数据库

3、mysql的安装

a.安装客户端

yum install mysql

b.安装服务端

yum install mysql-server

安装好以后,查看有三个mysql安装包

rpm -qa|grep mysql

(4)启动mysql

service mysqld start 开启数据库(我们使用数据要保持数据库开启)

service mysqld status 查看数据库的状态

service mysqld stop 关闭数据库

service mysqld restart 重启数据库

(5)mysqladmin -u root password '123456' 设置数据库密码

(6)进入数据库操作界面

mysql -u root -p 敲回车 输入密码

(7)show databases 显示所有的数据库

(8)授权

grant all privileges on . to root@'%' identified by "123456";

grant 授

all privileges 所有的权限

on

第一个星表示所有库

第二型表示所有的表

to

root 用户

@

% 表示所有用户

identified by 设置密码

(9)刷新权限

flush privileges;

(10)create database 数据库名

案例:

create database hz017;

(11)show databases ;查看所有的数据库

(12)use 库名

案例:use hz017

(13)退出:

ctrl+z,或ctrl+c或qiut

数据库单表命令

建表语句(在建表时的语句后面添加:DEFAULT charset=utf8;,可在数据中输入中文字符)

格式: create table 表名(字段名1 字符类型(字符长度),字段名2 字符类型(字符长度));

案例:create table aa(id int(10),name varchar(20));

==============================

数据类型

1、数值类型

int 存储类型(整数)

float 浮点数(带小数点)

2、字符类型(输入数据时加入引号'')

char

varchar

3、时间类型

date

time

datetime

year

======================================

查询表内数据
select * from 表名
*代表所有

插入数据:

(1)插入方式一:

格式:INSERT INTO 表名 VALUES(值1,值2);

案例:INSERT INTO aa VALUES(1,"aa");

(2)插入方式二:(插入部分字段)

格式:INSERT into 表名(字段名) VALUES(字段值)

案例:INSERT into aa(id) VALUES("4")

(3)插入的中文字符变成?号

解决方案:

在建表时的语句后面添加:

DEFAULT charset=utf8;

案例:create table cc(cid int(5),cname char(20))DEFAULT charset=utf8;

9、删除表格

drop table 表名

案例:drop table yy ;

约束:

约束用于对表中字段进行限制,保证表中数据的正确性和唯一性

1、primary key 主键约束

非空,唯一,用于唯一标识的记录,类似身份证。

一个表中只用一个主键

2、not null 非空约束

3、 unique 唯一索引

保证字段值具有唯一性,并且能为空,一个表中可以有多个唯一索引

4、default 默认值约束

定义:默认给字段指定默认值

5、auto_increment 自增长约束(一般都是和主键同时使用)

作用:在整数类型,字段默认值从1开始自增

(1)一般和主键约束一起使用,主要针对id

(2)每插入一条数据,就是在字段上自动+1,

表结构的操作:

add 添加字段

格式:ALTER TABLE 表名 add 字段名 字符类型(字符长度);

案例:ALTER TABLE student2 add dcs int(20);

3、change 修改字段

格式:ALTER TABLE 表名 change 旧字段名 新字段名 字符类型(字符长度);

案例:ALTER table student2 change dcs hzdcs int(19);

4、 drop 删除字段

格式:ALTER table 表名 drop 字段名 ;

案例:ALTER table student2 drop hzdcs ;

5、rename 修改表名

6、modify after 字段的调换

格式:ALTER table 表格 MODIFY 变动的字段 字段类型(字段长度) after 指定字段 ;

案例:ALTER table hz MODIFY math int(10) after id ;

7、first 添加字段到第一位

格式:alter table 表名 add 表字段 字符类型(字符长度) first ;

案例:alter table hz add no int(20) first ;

数据库汇中:增、删、改、查

一、查询语句:

(1)查询一个表中的所有数据

格式:select * from 表名 ; * 表示所有的字段

案例:select * from hz ;

(2)查询部分字段(多个字段用,分割)

格式:select 字段1,字段2 from hz ;

案例:select id,name from hz ;

(3)查询字段可以通过as 取别名

格式:

案例1( as写,):

select id as " 编号",name as "姓名" from hz ;

案例2(可以省略 as不写):

select id " 编号",name "姓名" from hz ;

(4)指定条件查询内容:(数据为空时不能用等于)

where +条件

条件1:

比较运算:>,<,=,!=,<>,>=,<=

条件2:

and ,or ,between ....and ,in , is not null

案例1:= 等于

select id ,name from hz where id=1;

案例2:> 大于

select id ,name from hz where id>1;

案例3:<小于

select id ,name from hz where id<2;

案例4:<=小于等于

select id ,name from hz where id<=2;

(5)

案例5:>=大于等于

select id ,name from hz where id>=2;

(6)!=不等于

案例6:select id ,name from hz where id != 2;

(7)<>不等于

select id ,name from hz where id <> 2;

================================

(8)and 同时满足条件

案例8; and 是同时满足多个条件

select id ,name,math from hz where id > 2 and math>90;

(9)or 只要满足其中一个条件 就显示

select id ,name,math from hz where id > 6 or math>90;

(10)between 。。。and 在什么范围之间

案例:select * from hz where id BETWEEN 3 and 6 ;

备注:包含了本身

(11)in 在一组数据中选择(在数据汇总匹配)

案例:select * from hz where id in (1,3,8)

(12)not in 不在一组数据中选

案例:select * from hz where id NOT in (1,3,8)

(13)is null 为空的数据

select * from hz where class is null;

(14)is not null 不为空的数据

select * from hz where class is not null;

order by 排序

(1)降序 (大到小)

order by desc

案例:select * from hz order by id desc ;

(2)升序(小到大)

asc 或不写

案例:

select * from hz order by id asc ;
select * from hz order by id ;

(3)二次排序

案例:select * from hz order by math desc ,id desc;

like 模糊匹配查询

%:表示匹配1个字符或多个字符

_ : 下滑线表示一个字符

案例1:匹配xx开头的数据

select * from hz where math like "7%"; # 匹配7开头的数据

案例2:匹配xx结尾数据
select * from hz where math like "%7"; #匹配7结尾的数据

案例3:匹配含有xx数据
select * from hz where math like "%7%"; #匹配含有7的数据

案例4:匹配指定位数的数据
select * from hz where math like "7_"; #匹配具体位数的数据

limit (索引位,步长) 显示指定的数据,限制;

根据索引位置来取值,从0开始,一个表第一行的索引就是0,第二行就是1

select * from hz limit 2; #表示取两行数据, 2 表示步长

select * from hz limit 1,2#表示从索引1开始第二行,2表示步长2行(2、3行)
select * from hz limit 4,3 ;# 表示从索引4开始取值,第五行开始,取三行,(5、6、7行)

sql 聚合函数(条件group)

max 最大数

案例1:select max(math) from hz ;

min最小数

案例2:select min(math) from hz ;

avg 平均值

案例3:

select avg(math) from hz ;

sum 求和

案例4:

select sum(math) from hz ;

count 统计

案例5:select count(math) from hz ;

distinct 去重

案例6:

select DISTINCT(math) from hz ;

group by ....... having

group by 是分组,一般不会单独使用,通常和聚合函数组合使用

案例1:分组

select sum(math),class from hz GROUP BY class ;

案例2:分组 在条件 having

(1)select sum(math) s,class from hz GROUP BY class having s>200 ;

(2)select sum(math) s,class from hz GROUP BY class having sum(math)>200 ;

注意:having 一般接在group by 后面

改数据:

update ......set......

格式:update 表名 set 字段名=新值 where条件;

案例:update hz set id=1 where id=9;

删除:

(1)delete

格式:DELETE from 表名 where 条件;

DELETE from hz where id=1;

(2) truncate 快速删除数据
格式:

truncate 表名 ;

案例:

truncate ff ;

(3)drop 删除

格式:drop table 表名

案例:drop table emp ;

drop >truncate> delete

==================

单行注释:ctrl +/

取消注释:shift+ctrl+/

多行注释:选中多行 ,ctrl +/

取消注释:选中多行 shift+ctrl+/

备份:

(1)备份表结构:

格式:create table 新表名 like 旧表名;

create table emp_new like emp;

(2)备份表数据

格式:

INSERT into 新表结构 select * from 旧表有数据 ;

案例:

INSERT into emp_new select * from emp ;

(3)备份部分数据

格式:INSERT into 表名(字段1,字段2) select 字段1,字段2 from 旧表 ;

案例:INSERT into emp2(sid,name) select sid ,name from emp ;

(4)备份表结构和数据

格式:

create table 新表 as (select * from 原表);

案例:create table hh as (select * from emp);

在linux 中:备份:

格式:mysqldump -u root -p 原库>新sql脚本名

案例:mysqldump -u root -p hz017>/home/hz17.sql

还原:

还原:

格式:mysql -u root -p 新库<备份好的脚本

案例:mysql -u root -p new</home/hz17.sql

一、多表查询

1、什么是多表关联查询

从2个表或者更多的表中查询我们需要的数据

2、多表连接的关系?

(1)内连接

(2)左连接

(3)右连接

(4)左独有数据

(5)右独有数据

(6)全外连接

比如: a 表:1,2,3 b 表:1,2,4

内连接:显示左边12和右边12关联 12

左连接:显示左边1,2,3,右 边12 关联 123 4不显示

右连接: 显示右边1,2,4全部显示,左 边12关联 124, 3不显示

左独有数据:显示3

右独有数据:显示4

全外连接:显示1,2,3,4

三、多表连接

1、内连接(普通内连接,隐藏内连接两者结果相同,只是格式不同)

定义:查询两个表共有的关联的数据

(1)普通内连接:

格式:select * from 表1 inner join 表2 on 表1.关联字段1=表2.关联字段2 ;

案例:select * from dept inner join emp on dept.dept1=emp.dept2 ;

(2)隐藏内连接

格式:select * from 表1 , 表2 where 表1.关联字段1=表2.关联字段2 ;

案例:select * from dept ,emp where dept.dept1=emp.dept2

2、左连接(左边全部显示,右边只显示与左边相同部分)

格式:select * from 表1 left join 表2 on 表1.关联字段1=表2.关联字段2 ;

案例:select * from dept left join emp on dept.dept1=emp.dept2;

3.右连接(右边全显示,左边只显示与右边相同部分)

格式:select * from 表1 right join 表2 on 表1.关联字段1=表2.关联字段2 ;

案例:select * from dept right join emp on dept.dept1=emp.dept2;

4、左独有数据(只显示左边独有数据)

格式:select * from 表1 left join 表2 on 表1.关联字段1=表2.关联字段2 where 右表字段 is null ;

案例:select * from dept left join emp on dept.dept1=emp.dept2 wehre name is null;

五、右独有数据(只显示右边独有数据)

格式:select * from 表1 right join 表2 on 表1.关联字段1=表2.关联字段2 where 左表字段 is null ;

案例:select * from dept right join emp on dept.dept1=emp.dept2 wehre dept1 is null;

六、全外连接 (union)

1内连接+左独有+右独有
select * from dept inner join emp on dept.dept1=emp.dept2
UNION
select * from dept left join emp on dept.dept1=emp.dept2 where name is null
UNION
select * from dept right join emp on dept.dept1=emp.dept2 where dept1 is null;

2 左连接+右独有
select * from dept left join emp on dept.dept1=emp.dept2
UNION
select * from dept right join emp on dept.dept1=emp.dept2 where dept1 is null ;

3 右连接+左独有
select * from dept right JOIN emp on dept.dept1=emp.dept2
UNION
select * from dept left join emp on dept.dept1=emp.dept2 where name is null ;

多表总结:

普通内连接:select * from 表1 inner join 表2 on 表 1.关联字段=表2.关联字段
隐藏内连接:select * from 表1,表2 where 表 1.关联字段=表2.关联字段
左连接:select * from 表1 left join 表2 on 表 1.关联字段=表2.关联字段
右连接 :select * from 表1 right join 表2 on 表 1.关联字段=表2.关联字段
左独有数据:select * from 表1 left join 表2 on 表 1.关联字段=表2.关联字段 where 表2中的字段 is null
右独有数据:select * from 表1 right join 表2 on 表 1.关联字段=表2.关联字段 where 表1 中的字段 isnull
全外连接:union
(1)左独有 union 右独有 union 内连接
(2)左连接 union 右独有
(3)右连接 union 左独有

索引

一、索引的介绍

1、什么是索引?

(1)定义:索引是一种数据结构

一个索引在存储的表中的数据结构;

(2)索引是在表的字段上创建的

(3)索引包含了一列值,这个值保存在一个数据结构中

2、索引作用?

(1)保证数据记录的唯一性

(2)实现表与表之间的参照性

(3)减少排序和分组的时间(例如在使用order by ,group by 查询语句中进行数据检索)
(4)可以使用索引快速访问数据库中指定信息

3、索引的缺点?

(1)索引要占物理内存

(2)索引对表进行增删改查,索引要动态维护,降低数据的维护速度

4、索引的分类

(1)普通索引

index 简称 mul 最基本的索引,没有任何限制

(2)主键索引

primary key 简称 pri 是一种唯一索引,不能为空

(3)唯一索引

unique 简称 uni 是一种唯一索引,可为空,一个表中可以有多个唯一索引

以下作为了解下:

(4)全文索引

(5)组合索引

(6)单列索引

(7)聚焦索引

(8)非聚焦索引

索引的应用

1、索引的查询

方法一:

格式1:

格式:show INDEX from 表名;

案例:show INDEX from emp ;

方法二:

格式:show keys from 表名;

案例:show KEYS from student2 ;

(2)查看表结构,通过表结构查看索引

desc 表名

(3)创建普通索引

第一种情况:索引名和字段名不一致

格式:ALTER table 表名 add INDEX 索引名(字段名);

案例:ALTER table student2 add INDEX sym(sex);

简写:mul

第二种情况:索引名和字段名一致

格式:ALTER table 表名 add INDEX (字段名);

案例:ALTER table student2 add INDEX (age);

(4)唯一索引(唯一,为空,在一个表可以有多个唯一索引)

单词:unique

简写:uni

第一种情况:添加唯一索引名和字段名不一致

格式:

alter table 表名 add UNIQUE 索引名(字段名)

案例:

alter table student2 add UNIQUE aa(name)

第二种情况:添加唯一索引名和字段名一致

格式:

alter table 表名 add UNIQUE (字段名)

案例:

alter table student2 add UNIQUE (name)

(5) 添加主键索引 (唯一,不能为空,一个表中只有一个主键)

单词:primary key 主键

简写:pri

格式:

ALTER table 表名 add PRIMARY key (字段名) ;

案例:

ALTER table student2 add PRIMARY key (id) ;

(6)删除索引:

1、第一种情况:删除普通索引和唯一索引是通一种方法

格式:

alter table 表名 drop INDEX 索引名

案例:
alter table student2 drop INDEX aa;

2、第二种情况:删除主键索引

格式:

alter table 表名 drop primary key;

案例:

alter table student2 drop primary key ;

===============================================

二、创建方法二

格式:create INDEX 索引名 on 表名 (字段名)

案例:create INDEX aa on student2 (english)

===============================================

三、建表时创建索引

格式:

CREATE table 表名( 字段名 字段类型(字符长度) PRIMARY key ,字段名 字符类型(字符长度) UNIQUE )) ;

案例:

CREATE table wzx( id int(10) PRIMARY key ,name VARCHAR(20) UNIQUE ,age int(10)) ;

索引是对表的一列数据起到约束作用

===========================================

posted @ 2024-07-18 17:55  藕丝鲜芋  阅读(4)  评论(0编辑  收藏  举报