本篇的主要目的:對于數(shù)據(jù)庫如何去增加刪除查詢修改
創(chuàng)建數(shù)據(jù)庫create
主要細(xì)節(jié)在于選項(xiàng)問題,編碼選項(xiàng)
CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [,
create_specification] ...]
create_specification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
說明:
大寫的表示關(guān)鍵字
[] 是可選項(xiàng)
CHARACTER SET: 指定數(shù)據(jù)庫采用的字符集
COLLATE: 指定數(shù)據(jù)庫字符集的校驗(yàn)規(guī)則
- 查看當(dāng)前用戶數(shù)據(jù)庫的列表show databases;
- 創(chuàng)建數(shù)據(jù)庫create database db_name;
當(dāng)我們創(chuàng)建數(shù)據(jù)沒有指定字符集和校驗(yàn)規(guī)則時,系統(tǒng)使用默認(rèn)字符集:utf8,校驗(yàn)規(guī)則:utf_general_ci;
簡單驗(yàn)證一下:創(chuàng)建一個數(shù)據(jù)庫create database d1,然后去/var/lib/mysql/d1/db.opt查看:、
- 刪除數(shù)據(jù)庫drop database db_name;
創(chuàng)建數(shù)據(jù)庫:create database db_name(本質(zhì)就是Linux在/var/lib/mysql創(chuàng)建一個目錄),刪除數(shù)據(jù)庫:drop database db_name;(刪除目錄)
比如我們在/var/lib/mysql下創(chuàng)建一個目錄youcanseeme,而用mysql命令show databases;自然也可以看到。(但是在/var/lib/mysql手動mkdir創(chuàng)建目錄這是非常不合理的)
- 創(chuàng)建不存在(if not exists)的數(shù)據(jù)庫create database if not exists database1;
- 數(shù)據(jù)庫編碼問題
創(chuàng)建數(shù)據(jù)庫的時候,有兩個編碼集:1.數(shù)據(jù)庫編碼集 2.數(shù)據(jù)庫校驗(yàn)集
數(shù)據(jù)庫編碼集——數(shù)據(jù)庫未來存儲數(shù)據(jù)
數(shù)據(jù)庫校驗(yàn)集——支持?jǐn)?shù)據(jù)庫進(jìn)行字段比較使用的編碼,本質(zhì)也是一種讀取數(shù)據(jù)庫中數(shù)據(jù)采用的編碼格式
數(shù)據(jù)庫無論對數(shù)據(jù)做任何操作,**都必須保證操作和編碼必須是編碼一致的!**防止亂碼
- 查看系統(tǒng)默認(rèn)字符集以及校驗(yàn)規(guī)則
編碼集:show variables like ‘character_set_database’;
校驗(yàn)集:show variables like ‘collation_database’;
除了database之外,其他的呢:show variables like ‘collation_%’;
如果想查看數(shù)據(jù)庫支持的字符集:show charset;
如果想查看數(shù)據(jù)庫支持的字符集校驗(yàn)規(guī)則:show collation;
- 創(chuàng)建指定編碼集的數(shù)據(jù)庫
如果沒有指定,默認(rèn)以配置文件為主
兩種方式創(chuàng)建字符集為utf8的表:
create database d2 charset=utf8;
create database d3 character set utf8;
同時設(shè)置字符集與校驗(yàn)集:
- 校驗(yàn)規(guī)則對數(shù)據(jù)庫的影響
為了說明這個情況,現(xiàn)在我們創(chuàng)建兩個數(shù)據(jù)庫:
test1數(shù)據(jù)庫校驗(yàn)集設(shè)置為utf8_general_ci;字符集默認(rèn)為utf8;校驗(yàn)規(guī)則使用utf8_ general_ ci[不區(qū)分大小寫]
test2數(shù)據(jù)庫校驗(yàn)集設(shè)置為utf8_bin;字符集默認(rèn)為utf8;校驗(yàn)規(guī)則使用utf8_ bin[區(qū)分大小寫]
- test1數(shù)據(jù)庫
現(xiàn)在先來看數(shù)據(jù)庫test1,往數(shù)據(jù)庫test1插入數(shù)據(jù),先使用數(shù)據(jù)庫use test1;創(chuàng)建表person
create table if not exists person (name varchar(20));
插入數(shù)據(jù):
mysql> insert into person (name) values ('a');
Query OK, 1 row affected (0.01 sec)
mysql> insert into person (name) values ('b');
Query OK, 1 row affected (0.00 sec)
mysql> insert into person (name) values ('A');
Query OK, 1 row affected (0.01 sec)
mysql> insert into person (name) values ('B');
Query OK, 1 row affected (0.00 sec)
mysql> insert into person (name) values ('c');
Query OK, 1 row affected (0.01 sec)
mysql> insert into person (name) values ('D');
Query OK, 1 row affected (0.00 sec)
此時的表person,查詢表person:
查詢person表中的a:結(jié)果是大寫小寫都能夠查出來,則是utf8_general_ci不區(qū)分大小寫:
看一下排序結(jié)果:
test1中的校驗(yàn)規(guī)則是utf8_general_ci進(jìn)行比較的時候,進(jìn)行校驗(yàn)的時候大小寫不做區(qū)分,校驗(yàn)集是會影響結(jié)果的,一般我們按照默認(rèn)的
- test2數(shù)據(jù)庫
現(xiàn)在再來看數(shù)據(jù)庫test2,使用數(shù)據(jù)庫use test2;,創(chuàng)建表person:
create table if not exists person (name varchar(20));
插入數(shù)據(jù),查看表person:
查詢person表中的a:結(jié)果是只查出來小寫:這是utf8_bin區(qū)分大小寫:
對于排序,數(shù)據(jù)庫test2中的person默認(rèn)是升序的,按照ascii值小到大:
查看數(shù)據(jù)庫show
show databases;
- 使用數(shù)據(jù)庫use db_name;
查看到之后并不能直接使用數(shù)據(jù)庫,想使用數(shù)據(jù)庫使用use+數(shù)據(jù)庫名稱;如使用數(shù)據(jù)庫helloworld;
use helloworld;
- 確認(rèn)當(dāng)前使用的數(shù)據(jù)庫select database();
select database();
當(dāng)前正在使用test1數(shù)據(jù)庫
刪除數(shù)據(jù)庫drop
DROP DATABASE [IF EXISTS] db_ name;
執(zhí)行刪除之后的結(jié)果:
數(shù)據(jù)庫內(nèi)部看不到對應(yīng)的數(shù)據(jù)庫
對應(yīng)的數(shù)據(jù)庫文件夾被刪除,級聯(lián)刪除,里面的數(shù)據(jù)表全部被刪注意:不要隨意刪除數(shù)據(jù)庫
//刪除數(shù)據(jù)d5\d4\d3;
mysql> drop database d5;
Query OK, 0 rows affected (0.00 sec)
mysql> drop database d4;
Query OK, 0 rows affected (0.00 sec)
mysql> drop database d3;
Query OK, 0 rows affected (0.00 sec)
修改數(shù)據(jù)庫alter
ALTER DATABASE db_name
[alter_spacification [,alter_spacification]...]
alter_spacification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
采用alter的方式對數(shù)據(jù)庫進(jìn)行修改
對數(shù)據(jù)庫的修改主要指的是修改數(shù)據(jù)庫的字符集,校驗(yàn)規(guī)則
alter database test2 charset=gbk collate gbk_chinese_ci;
修改完查看數(shù)據(jù)庫test2的信息:
文章來源:http://www.zghlxwxcb.cn/news/detail-530672.html
/*!40100 DEFAULT CHARACTER SET gbk */:這個不是注釋,表示當(dāng)前mysql版本大于4.01版本,就執(zhí)行這句話文章來源地址http://www.zghlxwxcb.cn/news/detail-530672.html
到了這里,關(guān)于【MySql】數(shù)據(jù)庫的增刪改查的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!