MySQL
一,如何判断更新删除语句是否执行成功
在连接字符串后加: useAffectedRows=true
即: jdbc:mysql://${jdbc.host}/${jdbc.db}?useAffectedRows=true
目的:在数据库更新数据是可以返回受影响的行数以此判断是否更新成功。
函数相关
- 获取当前时间:
NOW()获取当前日期加时间:2019-01-21 14:09:12(执行SQL时已确定)
SYSDATE()获取当前日期加时间:2019-01-21 14:10:53(运行到函数处才求时间)
CURDATE()获取当前日期:2019-01-21
CURTIME()获取当前时间:14:14:24
UTC时间:获得当前 UTC 日期时间函数:utc_date():日期, utc_time():时间, utc_timestamp():日期加时间
- 时间格式化函数
时间类型格式化: DATE_FORMAT(a.record_date,'%Y-%m-%d %h:%m:%s')
- 字符串截取函数
left('10%',length('10%')-1)
列相关
- 添加列
alter table 表名 add column 列名 varchar(30);
- 删除列
alter table 表名 drop column 列名;
- 修改列名
alter table bbb change nnnnn hh int;
- 修改列属性
alter table t_book modify name varchar(22);
表相关
- 修改表名
alter table t_book rename to bbb;
约束相关
- 添加主键约束
alter table 表名 add constraint 主键 (形如:PK_表名) primary key 表名(主键字段);
- 添加外键约束
alter table 从表 add constraint 外键(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);
- 删除主键约束
alter table 表名 drop primary key;
- 删除外键约束
alter table 表名 drop foreign key 外键(区分大小写);
索引相关
- 创建表时直接设置
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`stu_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`stu_id`),
UNIQUE KEY `UK_student_name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;
- 创建唯一索引
create unique index UK_student_name on student (name);
命令相关
- 查看表的字段信息
desc 表名;
- 查看表的所有信息
show create table 表名;
格式匹配
![](https://img2018.cnblogs.com/blog/1542415/201902/1542415-20190221095521393-528994690.png)