mysql 10分钟_10分钟入门mysql(含常用的sql语句,mysql常见问题及解决方案)
開發(fā)中常用的sql語句
1,創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)并指定編碼格式
drop database if exists test;create database test default character set utf8 collate utf8_general_ci;use test;
解釋下:
sql語句
用途
drop database if exists test;
創(chuàng)建test數(shù)據(jù)庫(kù)之前先檢測(cè)是否已存在,存在就刪除
create database test default character set utf8 collate utf8_general_ci;
創(chuàng)建數(shù)據(jù)test并指定編碼格式,utf8是我們常用的編碼個(gè)格式,utf8_general_ci可以支持表情
use test;
使用test數(shù)據(jù)庫(kù),我們后面創(chuàng)建table時(shí),需要先指定要使用那個(gè)數(shù)據(jù)庫(kù)
2,創(chuàng)建一個(gè)簡(jiǎn)單的數(shù)據(jù)表
create table class ( id int(11) not null auto_increment comment '班級(jí)id', name varchar(50) not null comment '班級(jí)名', primary key (id)) comment '班級(jí)表';create table student ( id int(11) not null auto_increment comment '學(xué)生id', name varchar(50) not null comment '學(xué)生姓名', age tinyint unsigned default 20 comment '學(xué)生年齡', sex enum('male', 'famale') comment '性別', score tinyint comment '入學(xué)成績(jī)', class_id int(11) comment '班級(jí)', createTime timestamp default current_timestamp comment '創(chuàng)建時(shí)間', primary key (id), foreign key (class_id) references class (id)) comment '學(xué)生表';
解釋下:
sql語句
用途
create table class
創(chuàng)建名叫class的表
id int(11) not null auto_increment comment ‘班級(jí)id’,
為class表創(chuàng)建id ,指定類型為int 長(zhǎng)度11,不能為空,auto_increment自增長(zhǎng),描述信息‘班級(jí)id’
name varchar(50) not null comment ‘班級(jí)名’,
創(chuàng)建name屬性,類型varchar,長(zhǎng)度50,不能為空,描述信息為‘班級(jí)名’
primary key (id)
指定id為主鍵
foreign key (class_id) references class (id)
關(guān)聯(lián)class表到student表
mysql開發(fā)中常見的問題
這里以java開發(fā)中使用myslq 為例,來講解下我們?cè)陂_發(fā)中常見的mysql相關(guān)的問題。
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以上的版本必須配置時(shí)區(qū)
所以要在連接MySQL服務(wù)器地址的位置跟上?serverTimeZone=UTC
image.png
2,在創(chuàng)建數(shù)據(jù)表的時(shí)候sql語句里已經(jīng)加了默認(rèn)時(shí)間,但是還會(huì)報(bào)錯(cuò):
SQL Error:1048,SQLState:23000
Colum createtime cannot be null
建表語句
報(bào)錯(cuò)信息
解決辦法:在對(duì)應(yīng)的bean上加以下兩個(gè)注釋
image.png
持續(xù)更新中。。。。。。
文章轉(zhuǎn)載于:https://www.jianshu.com/p/cff2cd617375
原著是一個(gè)有趣的人,若有侵權(quán),請(qǐng)通知?jiǎng)h除
總結(jié)
以上是生活随笔為你收集整理的mysql 10分钟_10分钟入门mysql(含常用的sql语句,mysql常见问题及解决方案)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: chrome js 读取文件夹_使用Ja
- 下一篇: mysql insert 1062_一则