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

开发中常用的sql语句

1,创建一个数据库并指定编码格式

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时,需要先指定要使用那个数据库

2,创建一个简单的数据表

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

cff2cd617375

image.png

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

SQL Error:1048,SQLState:23000

Colum createtime cannot be null

cff2cd617375

建表语句

cff2cd617375

报错信息

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

cff2cd617375

image.png

持续更新中。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值