sql讲解2
一、创建表:create table 表名 (字段名1 字符类型2(字符长度),字段名2 字符类型2(字符长度2)) DEFAULT charset=utf8;
案例:create table b3(sid int(10),sname varchar(20),sex int(10),rq date)DEFAULT charset=utf8;
select * from b3 ;
desc b3;
建表的字符类型:数值类型:
int 或intteger 大整数值 4个字节float 单精度,浮点数 4个字节字符类型:char 定长字符类型 0-255字节varchar 边长字符 0-65535字节
时间类型:date 年月日 3个字节time 时分秒 3个字节year 年 1个字节datatime 年月日、时分秒 8个字节数
(6)对表格中的字段进行操作
a、建表案例:create table b3(sid int(10),sname varchar(20),sex int(10),rq date)DEFAULT charset=utf8;
select * from b3 ;
desc b3;

b、add 添加表字段(默认添加在最后一位)
格式:ALTER table 表名 add 字段名 字符类型(字符长度);
案例:ALTER table b3 add sg int(10);

c、修改表字段格式:ALTER table 表名 change 原字段名 新字段名 新字符类型(新字符长度);
案例:ALTER table b3 change sg tz int(10);

d、drop
删除字段格式:ALTER table 表名 DROP 字段名;
案例:alter table b3 drop tz;

e、rename
修改表名格式:ALTER table 源表名 RENAME 新表名;
案例:alter table b3 rename b5;

f、modify
after 字段的调换格式:ALTER table 表名 modify 调换的字段名 字符类型(字符长度) AFTER 指定字段 ;
案例:ALTER table b5 MODIFY rq date AFTER sid ;

g、first
添加字段到第一位格式:ALTER table 表名 add 新字段名 字符类型(字符长度) FIRST ;
案例:alter table b5 add no int(20) first ;

二、数据库中的增删改查建表数据
select create table student2( id int primary key , name char(20), sex char(10), age int(3), mobile char(20), class char(10), english int(10), chinese int(10), math int(10) )engine=innodb default charset=utf8; insert into student2 values (1,'小红','女',23,'13813828824','1719',77,88,98), (2,'小明','男',23,'13713713711','1720',56,66,55), (3,'小李','男',23,'15915913911','1719',78,64,87), (4,'小张','男',23,'15915913912','1720',77,76,77), (5,'小白','女',24,'15915913913','1719',90,89,98), (6,'小陈','女',19,'15915913914','1719',84,100,81), (7,'小钱','女',20,'15915913915',null,45,99,93);
1、查询字段 select
(1)查询所有内容格式:select * from 表名 ;
* 表示的所有
案例:select * from student2 ;

(2)查询部分字段内容格式:select 字段1,字段2 from 表名案例:select name,age from student2 ;
用逗号区分

(3)查询的字段设置成别名 as格式:select 字段名1 as "别名1",字段2 "别名2" from 表;
案例:select name as "姓名",age "年龄" from student2 ;
注意点:as 可以省略不写

(4)查询内容可以接条件where +条件
条件1:=,!=,>,<,<>,>=,<=

条件2:and 、or、between...and ,in ,not in,is null,is not null
(1)and 同时满足多个条件案例:select * from student2 where id >=5 and age >21;

(2)or 满足 一个条件,或者多个条件案例:select * from student2 where id >5 or age >23;

(3)between...and 在什么范围之间案例:select * from student2 where id BETWEEN 5and 7 ;

(4)in 在一个范围集内案例:select * from student2 where id in( 1,4,9,6)

(5)not in 不在一个范围集内案例:select * from student2 where id not in( 1,4,9,6)

(6)is null 为空案例:select * from student2 where class is null

(7)is not null 不为空 案例:select * from student2 where class is not null

(5)order by 排序a、降序 desc格式:select * from 表名 order by 字段名 desc;
案例:select * from student2 ORDER BY english desc ;
降序b、升序asc 可以省略不写select * from student2 ORDER BY english asc ;
升序select * from student2 ORDER BY english ;
升序(省略不写)

c、二次排序(第一次排序存在相同,就排第二个字段)
案例:select * from student2 ORDER BY english desc,chinese asc;

(6)like 模糊查询% :
表示匹配1个字符或多个字符_:下划线表示一个字符
案例:#like 模糊匹配select * from student2 where chinese LIKE "8%" # 匹配8开头的分数
select * from student2 where chinese LIKE "%8" -- 匹配8结尾的分数
select * from student2 where chinese LIKE "%8%" -- 匹配包含8的数据
select * from student2 where chinese LIKE "1__"

(7)limit 显示指定的行数,
格式:limt (索引位,步长)索引:一个表格中的索引位从0开始,
举例:第一行索引就0步长:显示多少行
案例:1、select * from student2 limit 2;; 显示两行,默认从索引0开始,显示两行
2、select * from student2 limit 1,2;
1是显示从第2行开始索引1开始,2是步长
3、先降序在取指定的行数select * from student2 ORDER BY english desc LIMIT 0,3

(8)sql的聚合函数max 最大值min 最小值avg 平均值sum 求和count 统计distinct
去重案例:
max最大值select max(id) from student2 ;
min最小值select min(id) from student2 ;
avg平均值select avg(id) from student2 ;
-- sum 求和select sum(id) from student2 ;
-- count 统计select count(id) from student2 ;
-- distinct 去重select DISTINCT(class) from student2 ;

(9)分组 group by
案例:select class,sum(id) from student2 group by class
(10)having 和where的意思是一样,也是接条件案例:分组后接having 再接条件#GROUP BY by
select count(name) from student2 GROUP BY sex
select sum(english),sex from student2 GROUP BY sex#接条件:having ,与where意思,
group by 后面接having
select sum(english),sex from student2 GROUP BY sex HAVING sum(english) >260;
select sum(english) as s,sex from student2 GROUP BY sex HAVING s >260;
having一般接在group by后面

(11)改格式:UPDATE 表名 set 字段名=新值 where 条件 ;
案例:
UPDATE student2 set name="xiaohong" where id =1

(12)删除数据a、删除表中一条数据格式:DELETE from 表名 where 条件 ;
案例:
DELETE from student2 where id =1 ;
b、删除表中所有的数据格式:DELETE from
表名案例:DELETE from student2 ;
(13)删除的三种方法drop >truncate>delete 速度的优先级
a、truncate
删除大批量的数据truncate student2 ;
b、drop table 表名; 表名和表数据都删除
c、delete from 一般删除表内数据

(14)单行注释#多行注释:ctrl+/
取消多行注释ctrl+shift+/ 注意选中内容,注释
(15)备份1、在mysql中备份
a、备份表结构格式:create table 新表名 like 源表名;案例:CREATE table st2 like student2 ;
b、备份数据
(1)备份全部数据格式:INSERT into 新表结构 select * from 表格 ;
案例:
INSERT into st1 select * from student2;
(2)备份部分数据INSERT into st2(id,name)
select id,name from student2;
c、备份表和数据
(1)备份所有表结构和表数据格式:create table 新表名 as(select * from 表名)
案例:create table st3 as (select * from student2)
(2)备份部分表结构和数据create table st4 as (select id ,age from student2)
2、在linux中备份
(1)备份案例:mysqldump -u root -p ku1>/home/h5/sql77.sql

备份成功

(2)linux还原案例
:mysql -u root -p kk</home/h5/sql77.sql

浙公网安备 33010602011771号