站点图标 起风网

10分钟入门mysql(含常用的sql语句,mysql常见问题及解决方案)

开发中常用的sql语句

drop database if exists test;create database test default character set utf8 collate utf8_general_ci;use test;

解释下:

sql语句 用途
drop database if exists test; 创建test数据库之前先检测是否已存在,存在就删除
create database test default character set utf8 collate utf8_general_ci; 创建数据test并指定编码格式,utf8是我们常用的编码个格式,utf8_general_ci可以支持表情
use test; 使用test数据库,我们后面创建table时,需要先指定要使用那个数据库
create table class (  id int(11) not null auto_increment comment '班级id',  name varchar(50) not null comment '班级名',  primary key (id)) comment '班级表';create table student (  id int(11) not null auto_increment comment '学生id',  name varchar(50) not null comment '学生姓名',  age tinyint unsigned default 20 comment '学生年龄',  sex enum('male', 'famale') comment '性别',  score tinyint comment '入学成绩',  class_id int(11) comment '班级',  createTime timestamp default current_timestamp comment '创建时间',  primary key (id),  foreign key (class_id) references class (id)) comment '学生表';

解释下:

sql语句 用途
create table class 创建名叫class的表
id int(11) not null auto_increment comment ‘班级id’, 为class表创建id ,指定类型为int 长度11,不能为空,auto_increment自增长,描述信息‘班级id’
name varchar(50) not null comment ‘班级名’, 创建name属性,类型varchar,长度50,不能为空,描述信息为‘班级名’
primary key (id) 指定id为主键
foreign key (class_id) references class (id) 关联class表到student表

mysql开发中常见的问题

这里以java开发中使用myslq 为例,来讲解下我们在开发中常见的mysql相关的问题。

1,Caused by: java.sql.SQLException: The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

解决:通过最后一句我们知道,JDBC 8以上的版本必须配置时区
所以要在连接MySQL服务器地址的位置跟上?serverTimeZone=UTC

image.png
2,在创建数据表的时候sql语句里已经加了默认时间,但是还会报错:

SQL Error:1048,SQLState:23000
Colum createtime cannot be null

建表语句

报错信息

解决办法:在对应的bean上加以下两个注释

image.png

持续更新中。。。。。。

文章转载于:https://www.jianshu.com/p/cff2cd617375

原著是一个有趣的人,若有侵权,请通知删除

退出移动版