資料庫 6 SQL基本操作 庫操作

2022-03-22 09:25:02 字數 3058 閱讀 9756

基本語法:create database 資料庫名字 [庫選項];

庫選項用來約束資料庫,分為兩個選項:1.字符集設定:charset/character set 具體字符集(資料儲存的編碼格式)                     常用的字符集:gbk和utf8(utf8中間不能加中劃線或者下劃線)

2.校對集設定:collate 具體校對集(資料比較的規則)

建立乙個擴充套件名為sql的檔案,比如sql_2018_0503.sql(將其內容複製到mysql命令視窗中)

-- sql_2018_0503.sql內容:

-- 雙中劃線+空格:注釋(單行注釋),也可以使用#號

# 建立資料庫

create database mydatabase charset utf8;-- 建立乙個名為mydatabase的資料庫

其中:資料庫名字不能用關鍵字(已經被使用的字元)或者保留字(將來可能會用到的)

# 建立關鍵字資料庫(出錯)

create database database charset utf8;-- 建立乙個名為database的資料庫

如果非要使用關鍵字或者保留字,name必須使用兩個反引號(esc鍵下面的鍵在英文狀態下的輸出:`)

# 使用反引號

使用中文資料庫是可以的,但是有前提條件:保證伺服器能夠識別(建議不用)

-- 建立中文資料庫(直接建立是錯誤的,無論加不加反引號),本身不需要加反引號

create database 中國 charset utf8;

create database `中國` charset utf8;

-- 解決方法:告訴伺服器當前中文的字符集是什麼(檢視方法:命令視窗左上角->屬性->選項)

set names gbk;

create database 中國 charset utf8;-- 沒有必要加反引號

當建立資料庫的sql語句執行之後,發生了什麼?

.在資料庫系統中,增加了對應的資料庫資訊

會在儲存資料的資料夾下:data目錄,建立乙個對應資料庫名字的資料夾

3.每個資料庫下都有乙個opt 檔案:儲存了資料庫選項

注:校對集依賴字符集指的是:字符集改變校對集也會改變

-- 檢視所有資料庫

show databases;

show databases like 'pattern';  --pattern是匹配模式,  %:表示匹配多個字元, _:表示匹配單個字元

-- 舉例 建立兩個資料庫

create database informationtest charset utf8;

create database information_schema charset utf8;

-- 匹配以informationtes開頭的資料庫

show databases like 'informationtes_';

-- 匹配以information開頭的資料庫

show databases like 'information%';

-- 匹配以information_開頭的資料庫:_需要被轉義

show databases like 'information\_%'; --如果不轉義將匹配到informationtest,information_schema

-- 檢視資料庫建立語句

show create database mydatabase;

show create database `database`; --關鍵字需要使用反引號

資料庫名字不可以修改。

資料庫的修改僅限庫選項:字符集和校對集(校對集依賴字符集,字符集改動,校對集會跟隨改動)

alter database 資料庫名字 [庫選項];

charset/character set [=] 字符集

collate [=] 校對集

-- 修改資料庫informationtest的字符集()

alter database informationtest charset gbk; -- 以前是utf8的

所有的操作中:刪除是最簡單的

drop database 資料庫名字;

-- 刪除資料庫

drop database informationtest;

當刪除資料庫語句執行之後,發生了什麼?

1.在資料庫內部看不到對應的資料庫    利用show databases;檢視

2.在對應的資料庫儲存的資料夾內:資料庫對應的資料夾也被刪除(級聯刪除:裡面的資料表全部刪除)

注意:資料庫的刪除不是鬧著玩的,不要隨意刪除,應該先進行備份後操作。(刪除不可逆,需負法律責任)

SQL資料庫基本操作

1 建立表 create create table dba test 建立表,表名為dba test col1 number,col2 varchar2 10 第一列預設值是0 第二列不准許有空值 第一列預設值是0 第二列不准許有空值 2 檢索操作 select select 1 from 2 其中...

SQL資料庫的基本操作

一丶基本命令列操作 1 顯示當前資料庫伺服器中的資料庫列表 mysql show databases 2 建立資料庫 mysql create database 資料庫名 3 建立資料表 mysql use 庫名 mysql create table 表名 欄位名 varchar 20 欄位名 ch...

SQL資料庫基本操作學習

資料查詢操作 1 讀取最新的10條資料 select top 10 from database dbo tablename order by columnname desc 2 刪除資料表,刪除資料較慢,可以恢復 delete from basename dbo tablename 3 刪除資料表的...