六盘水北京网站建设,个人博客网页设计代码,有实力营销型网站建设,wordpress如何登录后台一、进入数据库操作界面
1、mysql -u root -p 敲回车 #xff0c;输入密码 #xff0c;进入数据库操作界面
2、show databases 查看所有的数据#xff08;如果没有数据库#xff1a;创建数据库 create database 库名称#xff09;
3、use 数据库名 使…一、进入数据库操作界面
1、mysql -u root -p 敲回车 输入密码 进入数据库操作界面
2、show databases 查看所有的数据如果没有数据库创建数据库 create database 库名称
3、use 数据库名 使用数据库
4、show tables 显示库中的所有表
5、建表语句
格式 create table 表名(字段名1 字符类型字符长度),字段名2 字符类型字符长度)); 案例create table aa(id int(10),name varchar(20)); 6、查看表结构
desc 表名
案例 7、在navicat 中点击库名点击查询新建查询在新建查询中输入sql语句 8、插入数据
1插入方式一
格式INSERT INTO 表名 VALUES(值1,值2);
案例INSERT INTO aa VALUES(1,aa); 2插入方式二插入部分字段
格式INSERT into 表名(字段名) VALUES(字段值)
案例INSERT into aa(id) VALUES(4) 3插入的中文字符变成号
解决方案
在建表时的语句后面添加
DEFAULT charsetutf8;
案例create table cc(cid int(5),cname char(20))DEFAULT charsetutf8; 9、删除表格
drop table 表名
案例drop table yy ; 二、数据类型
1、数值类型
int 存储类型
float 浮点数 2、字符类型
char
varchar 3、时间类型
date
time
datetime
year 注意字符的长度
int20
varchar20 约束
约束用于对表中字段进行限制保证表中数据的正确性和唯一性
1、primary key 主键约束
非空唯一用于唯一标识的记录类似身份证。
一个表中只用一个主键 2、not null 非空约束 3、 unique 唯一索引
保证字段值具有唯一性并且能为空一个表中可以有多个唯一索引 4、default 默认值约束
定义默认给字段指定默认值 5、auto_increment 自增长约束一般都是和主键同时使用
作用在整数类型字段默认值从1开始自增
1一般和主键约束一起使用主要针对id
2每插入一条数据就是在字段上自动1 1、新建表 表结构的操作
2、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 idname 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 id1;
案例2 大于
select id ,name from hz where id1;
案例3小于
select id ,name from hz where id2;
案例4小于等于
select id ,name from hz where id2;
5
案例5大于等于
select id ,name from hz where id2;
6不等于
案例6select id ,name from hz where id ! 2;
7不等于
select id ,name from hz where id 2; 8and 同时满足条件
案例8; and 是同时满足多个条件
select id ,name,math from hz where id 2 and math90;
9or 只要满足其中一个条件 就显示
select id ,name,math from hz where id 6 or math90;
10between 。。。and 在什么范围之间
案例select * from hz where id BETWEEN 3 and 6 ;
备注包含了本身
11in 在一组数据中选择在数据汇总匹配
案例select * from hz where id in (1,3,8)
12not in 不在一组数据中选
案例select * from hz where id NOT in (1,3,8)
13is null 为空的数据
select * from hz where class is null;
14is not nu 不为空的数据
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行 select * from hz limit 4,3 ;# 表示从索引4开始取值第五行开始取三行 sql 聚合函数
max 最大数
案例1select max(math) from hz ;
min最小数
案例2select min(math) from hz ;
avg 平均值
案例3
select avg(math) from hz ;
sum 求和
案例4
select sum(math) from hz ;
count 统计
案例5select 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
1select sum(math) s,class from hz GROUP BY class having s200 ;
2select sum(math) s,class from hz GROUP BY class having sum(math)200 ;
注意having 一般接在group by 后面 改
update ......set......
格式update 表名 set 字段名新值 where条件
案例update hz set id1 where id9; 删除
(1)delete
格式DELETE from 表名 where 条件;
DELETE from hz where id1;
(2) truncate 快速删除数据 格式
truncate 表名 ;
案例
truncate ff ;
3drop 删除
格式drop table 表名
案例drop table emp drop truncate delete 单行注释ctrl /
取消注释shiftctrl/
多行注释选中多行 ctrl /
取消注释选中多行 shiftctrl/ 备份
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 分类: 教学笔记