一、新建表
二、建表语句
create table student( 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 student 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、表结构的语句
(1)add 添加字段
格式:ALTER table 表名 add 字段名 字符类型(字符长度);
案例:ALTER table student add dcs int(10);
(2)change 修改字段
格式:ALTER table 表名 change 源字段名 新字段名 字符类型(字符长度);
案例:ALTER table student change dcs hzdcs int(10);
(3)drop 删除字段
格式:ALTER table 表名 drop 字段名;
案例:ALTER table student drop hzdcs ;
(4)rename 修改表名
格式:ALTER table 表名 rename 新表名 ;
案例:ALTER table student rename student2 ;
(5)modify after 字段的调换
格式:
alter table 表名 modify 源字段名 字符类型(字符长度) after 表中字段名 ;
案例:
alter table student2 modify math int(10) after name ;
(6)first 添加字段到第一位
格式:
ALTER table 表名 add 新字段名 字符类型(字符长度) first ;
案例:
ALTER table student2 add no int(10) first ;
2.27-1课堂笔记:
-- select * from student;#查询内容
-- ALTER table student add dcs int(10);#最后添加字段dcs
-- alter table student change dcs hzdcs int(10);#字段改名
-- alter table student drop dsc;#删除字段
-- alter table student rename stu;#改表名
-- select * from stu;
-- alter table stu rename student;
-- alter table student rename s;
-- desc student;#查看字段
-- alter table student modify math int(10) after name;
-- alter table student modify math int(10) after class;
#把字段math移到某字段后面
-- alter table student add num int(10) first;
#在最前面加字段
-- select * from student;
-- select * from s;#查看全部
-- select name as "姓名",math "数学" from s;
#显示某些字段,as可省略
#####查询
-- select name,math,english,chinese from s
-- where math>80 and english>80 and chinese>80;
#条件查询where
-- alter table s change math m int(10);#改字段名
-- alter table s change english e int(10);
-- alter table s change chinese c int(10);
-- select * from s where id between 2 and 5;
#查id在2到5之间的between
-- select * from s where id in (2,6,8);
#查id包含在2或6或8的
-- select * from s where id not in (2,6,8);
#查id不包含在2或6或8的
-- select * from s where class is null;
#查含空值的
######排序
-- select * from s order by m asc;#升序
-- select * from s order by m desc;#降序
-- select * from s order by age asc,m desc,c desc,e desc;
#多次排序
-- select * from s where m like "8%";#8开头
-- select * from s where m like "%8%";#含8
-- select * from s where m like "%8";#8结尾
-- select * from s where m like "8_";#查看80几
-- select * from s where m like "8__";#查看800多
-- select * from s limit 0,3;#从索引第一个开始取3个数据
-- select * from s limit 4;#索引前4行
#索引=行数-1
-- select max(m) from s;#数学最大值
-- select min(m) from s;#数学最小值
-- select sum(m) from s;#数学求和
-- select avg(m) from s;#数学平均值
-- select count(m) from s;#数学统计个数
-- select DISTINCT(m) from s;#数学去重
-- select class,avg(m) from s group by class;
-- select class,avg(m) from s group by class HAVING m<80;
#求每个班平均数小于80
-- UPDATE s set name="小红帽" where id=1;
-- UPDATE s set name="小红" where id=1;
#将id=1的人name字段改成小红帽
-- select * from s;
######删除
#方法1
-- delete from s where id=100;#删除id=100的数据
#方法2
-- -- truncate sb;#连表带数据全部删掉
#方法3
#drop(前面已讲)
#drop>truncate>delete
#######注释
#ctrl+/注释
#shift+ctrl+/取消注释
#备份
-- create table sb like s;#备份表结构,不复制数据
#备份部分数
#insert into sb(id,age) select id,age from s;
#备份全部数据
#truncate sb;
-- select * from sb;