## 在虚礼机中
### 进入数据库操作界面
- 1、mysql -u root -p 敲回车 ,输入暗码 ,进入数据库操作界面
- 2、show databases 查看全部的数据(如果没有数据库:创建数据库 create database 库名称)
- 3、use 数据库名 利用数据库
- 4、show tables 表现库中的全部表
### 建表语句
- 格式: create table 表名(字段名1 字符范例(字符长度),字段名2 字符范例(字符长度));
案例:create table aa(id int(10),name varchar(20));
### 查看表结构:
- desc 表名
## 在navicat for MySQL中
### 查看表数据
- select * from 表名
### 建表时的语句
- create table cc(cid int(5),cname char(20))DEFAULT charset=utf8;
- DEFAULT charset=utf8;(防止插入中文数据无法表现)
### 插入数据:
- 插入方式一:
- 格式:INSERT INTO 表名 VALUES(值1,值2);
案例:INSERT INTO aa VALUES(1,"aa");
- 插入方式二:(插入部分字段)
- 格式:INSERT into 表名(字段名) VALUES(字段值)
案例:INSERT into aa(id) VALUES("4")
### 数据范例
- 1、数值范例
- int 存储范例
- float 浮点数
- 2、字符范例
- char
- varchar
- 3、时间范例
- date
- time
- datetime
- year
### 约束:
- 约束用于对表中字段进行限定,保证表中数据的精确性和唯一性
- 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);
### drop 删除字段
- 格式:alter table 表名 drop 字段名 ;
- 案例:alter table student2 drop hzdcs ;
### rename 修改表名
- 格式:alter table 旧表名 rename 新表名 ;
- 案例:alter table student2 rename hzdcs ;
### modify after 字段的变更(移动)
- 格式:alter table 表格 modify 变更的字段 字段范例(字段长度) after 指定字段 ;
案例:alter table hz modify math int(10) after id ;
### first 添加字段到第一位
- 格式:alter table 表名 add 表字段 字符范例(字符长度) first ;
案例:alter table hz add no int(20) first
### change 修改字段
- 格式:ALTER TABLE 表名 change 旧字段名 新字段名 字符范例(字符长度);
案例:ALTER table student2 change dcs hzdcs int(19);
## 数据库汇中:增、删、改、查
### 一、查询语句:
- 查询一个表中的全部数据
- 格式: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:比力运算:>,<,=,!=,<>,>=,<=
- !=不即是 案例6:select id ,name from hz where id != 2;
- <>不即是 select id ,name from hz where id <> 2;
- 条件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:>=大于即是 select id ,name from hz where id>=2;
- and 同时满足条件
- 案例8:select id ,name,math from hz where id > 2 and math>90;
- or 只要满足此中一个条件就表现
- select id ,name,math from hz where id > 6 or math>90;
- between 。。。and 在什么范围之间
- 案例:select * from hz where id BETWEEN 3 and 6 ; 备注:包含了本身,
- in 在一组数据中选择(在数据汇总匹配)
- 案例:select * from hz where id in (1,3,8)
- not in 不在一组数据中选
- 案例:select * from hz where id NOT in (1,3,8)
- is null 为空的数据
- select * from hz where class is null;
- is not null 不为空的数据
- select * from hz where class is not null
## 排序
### order by 排序(降序)
- (1)降序 (大到小) order by desc
- 案例:select * from hz order by id desc ;
### 升序(小到大)
- asc 或不写
- 案例:select * from hz order by id asc ;
- select * from hz order by id ;
### 二次排序
- 案例: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行
- select * from hz limit 4,3 ;# 体现从索引4开始取值,第五行开始,取三行,
## sql 聚合函数
### 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+/
### #+文字
## mysql备份:
### (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
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |