mysql200列_认识mysql(2)1、表字段的操作
1、语法 :alter table 表名 执⾏动作;
2、添加字段(add)
alter table 表名 add 字段名 数据类型;
alter table 表名 add 字段名 数据类型 first;
alter table 表名 add 字段名 数据类型 after 字段名;
3、删除字段(drop)
alter table 表名 drop 字段名;
varchar2最大长度4、修改数据类型(modify)
alter table 表名 modify 字段名 新数据类型;
5、表重命名(rename)
alter table 表名 rename 新表名;
6、练习
1、创建库 studb2
2、在库中创建表 t1 ,字段有3个:name、age、phnumber
use studb2;
create table t1(
name char(20),
age tinyint unsigned,
phnumber char(11)
);
3、查看表结构
desc t1;
4、在表中第⼀列添加⼀个 id 字段
alter table t1 add id int first;
5、把 phnumber 的数据类型改为 bigint
alter table t1 modify phnumber bigint;
6、在表中最后⼀列添加⼀个字段 address
alter table t1 add address varchar(50);
7、删除表中的 age 字段
alter table t1 drop age;
8、查看表结构
desc t1;
2、数据类型
1、数值类型
2、字符类型
1、字符类型宽度和数值类型宽度的区别
1、数值类型宽度为显⽰宽度,只⽤于select查询显⽰,和占⽤存储⽆关,可⽤zerofill查看效果
2、字符类型的宽度超过之后则⽆法存储
3、枚举类型
1、单选(enum) :字段名 enum(值1,值2,...)
2、多选(set) :字段名 set(值1,值2,...)
## 插⼊记录时: "F,study,Python"
create table t5(
id int(3) zerofill,
name varchar(15),
sex enum("M","F","Secret"),
likes set("F","M","study","Python")
);
insert into t5(likes) values("F,study,Python");
4、⽇期时间类型
1、date :"YYYY-MM-DD"
2、time :"HH:MM:SS"
3、datetime :"YYYY-MM-DD HH:MM:SS"
4、timestamp :"YYYY-MM-DD HH:MM:SS"
5、注意
1、datetime :不给值默认返回NULL值
2、timestamp :不给值默认返回系统当前时间
create table t7(
id int,
name varchar(15),
birthday date,
money int,
shijian datetime
);
insert into t7 values(2,"王",date(now()),10000,now());
3、⽇期时间函数
1、now() 返回服务器当前时间
2、curdate() 返回当前⽇期
3、curtime() 返回当前时间
4、year(date) 返回指定时间的年份
5、date(date) 返回指定时间的⽇期
6、time(date) 返回指定时间的时间
7、练习
1、在表中插⼊3条记录
insert into t7 values
(3,"⼩昭",19000520,3000,20180630000000),
(4,"赵敏",19000521,4000,20180702000000),
(5,"周芷若",19010522,3500,20180702100000);
2、查2018年7⽉2⽇有哪些⽤户充值了
select * from t7 where date(shijian)="2018-07-02";
3、查2018年7⽉份充值的信息
select * from t7
where
date(shijian)>="2018-07-01" and date(shijian)<="2018-07-31";
4、查7⽉30⽇10:00-12:00充值的信息
select * from t7
where
date(shijian)="2018-07-31" and
time(shijian)>="10:00:00" and
time(shijian)<="12:00:00";
4、⽇期时间运算
1、语法格式
select * from 表名
where 字段名 运算符 (时间-interval 时间间隔单位);
时间间隔单位:
1 day |
2 hour | 1 minute | 2 year |
3 month
2、练习
1、查询1天以内的记录
select * from t7
where shijian > (now()-interval 1 day);
age > 20
2、查询1年以前的记录
select * from t7
where shijian < (now()-interval 1 year);
3、查询1天以前,3天以内的记录
select * from t7
where
shijian < (now()-interval 1 day) and
shijian > (now()-interval 3 day);
5、表记录管理
1、删除表记录
1、delete from 表名 where 条件;
2、注意
delete语句后如果不加where条件,所有记录全部清空
2、更新表记录
1、update 表名 set 字段1=值1,字段2=值2,... where 条件;
2、注意
必须加where条件
3、练习(表hero)
1、查所有蜀国⼈的信息
select * from hero where country="蜀国";
2、查所有⼥英雄的姓名、性别和国家
select name,sex,country from hero
where sex="⼥";
3、把id为2的记录改为典韦,性别男,国家魏国
update hero set name="典韦",sex="男",country="魏国" where id=2;
4、删除所有蜀国英雄
delete from hero where country="蜀国";
5、把貂蝉的国籍改为魏国
update hero set country="魏国"
where name="貂蝉";
6、删除所有表记录
delete from hero;
4、运算符操作
1、数值⽐较/字符⽐较
1、数值⽐较 := != > >= < <=
2、字符⽐较 := !=
3、练习
1、查攻击⼒⾼于150的英雄的名字和攻击值
select name,gongji from sanguo where gongji>150;
2、将赵云的攻击⼒设置为360,防御⼒设置为68
update sanguo set gongji=360,fangyu=68
where name="赵云";
2、逻辑⽐较
1、and (两个或多个条件同时成⽴)
2、or (任意⼀个条件成⽴即可)
3、练习
1、出攻击值⾼于200的蜀国英雄的名字、攻击⼒
select name as n,gongji as g from sanguo
where gongji>200 and country="蜀国";
2、将吴国英雄中攻击值为110的英雄的攻击值改为100,防御⼒改为60 update sanguo set gongji=100,fangyu=60
where country="吴国" and gongji=110;
3、查蜀国和魏国的英雄信息
select * from sanguo
where country="蜀国" or country="魏国";
3、范围内⽐较
1、between 值1 and 值2
2、where 字段名 in(值1,值2,...)
3、where 字段名 not in(值1,值2,...)
4、练习
1、查攻击值100-200的蜀国英雄信息
select * from sanguo
where gongji between 100 and 200 and
country="蜀国";
2、到蜀国和吴国以外的国家的⼥英雄信息
select * from sanguo
where country not in("蜀国","吴国")