mysql 10分钟_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 ‘?D1ú±ê×?ê±??’ 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
原著是一個有趣的人,若有侵權,請通知刪除
總結
以上是生活随笔為你收集整理的mysql 10分钟_10分钟入门mysql(含常用的sql语句,mysql常见问题及解决方案)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: chrome js 读取文件夹_使用Ja
- 下一篇: mysql insert 1062_一则