1. 程式人生 > >MySQL(MariaDB)常用SQL語句詳解

MySQL(MariaDB)常用SQL語句詳解

DDL(Data Definition Language)資料定義語言

這些語句定義了不同的資料段、
資料庫、表、列、索引等資料庫物件的定義。常用的語句關鍵字主要包括 create、drop、alter
等。

資料庫管理

--刪除資料庫
drop database if exits bookDB;
--建立資料庫
create database bookDB;
--修改資料庫
alter database bookDB charset=utf8;

表的管理(主要是各種約束的管理)

--表的建立
create table bookInfo(
    book_id int auto_increment unique,              --自增長,唯一約束
    author_id int,
    book_name varchar(10) not null,                 --非空約束
    book_price decimal(10,2) check (book_price>0),  --檢查約束(mysql中不支援檢查約束,但是加上並不報錯)
    book_shelf bit default 0,                       --預設約束
    primary key (book_id),                          --主鍵
    key fk_author (author_id),                      --外來鍵詳細寫法
    constraint fk_author foreign key (author_id) references authorInfo(author_id)                                       
    --foreign key (author_id) references authorInfo (author_id)
    --主外來鍵的建立也可以直接在欄位上面新增,這種寫法是為了方便管理
);
-- 拿到資料建立一個表格
create table bookInfo as select * from book_table;
--建立臨時表
create temporary table if not exists book_table(....);
--刪除表
drop table bookInfo;

--重新命名
alter table bookInfo rename [to] book_info;

--修改表(列的管理)
--新增列
alter table bookInfo 
    add column book_press varchar(20);                  --column關鍵字可以省略
alter table bookInfo 
    add book_press varchar(20) after book_price;        --指定位置
alter table bookInfo 
    add (book_press varchar(20),book_date datetime);    --批量新增

--修改列型別
alter table bookInfo
    modify book_press varchar(200);
--修改列名(同時也可修改列的型別)
alter table bookInof
    change book_press bookPress varchar(25);

--刪除列
alter table bookInfo
    drop column book_press;

--修改表(約束的管理)
--使用modify關鍵字可以更改資料型別,使用change關鍵字可以更改列名和資料型別
--新增約束
alter table bookInfo 
    add primary key (book_id);                          --新增主鍵
alter table bookInfo
    add modify book_id int primary key;                 --使用modify關鍵字           
alter table bookInfo
    add constraint fk_author foreign key (author_id) 
        references authorInfo(author_id)                --新增外來鍵
alter table bookInfo
    add constraint unique (book_id);                    --新增唯一約束
alter table bookInfo
    modify book_shelf int default 0;                    --新增預設約束

 --刪除約束
alter table bookInfo
    modify book_shelf int;                              --刪除預設約束
alter table bookInfo
    change book_id book_id int;                         --去除auto_increment
alter table bookInfo
    drop primary key;                                   --刪除主鍵(先刪除自增長)
alter table bookInfo
    drop foreign key (fk_author);                       --刪除外來鍵

--設定自增長值
alter table bookInfo auto_increment=13;
--設定表的位元組編碼
alter table bookInfo character set='utf8';

DML(Data Manipulation Language)資料操作語言

主要用於新增、刪除、更新和查
詢資料庫記錄,並檢查資料完整性,常用的語句關鍵字主要包括 insert、delete、udpate 和
select 等。

--插入資料
insert into bookInfo values(0,2,'head frist java',49.8,0);
--把一個表的書籍插入到另一個表中
insert into bookInfo select * from bookInfo;
--刪除資料
delete from bookInfo;
--修改資料
update bookInfo set book_name='head first C#',book_press='南方出版社' where book_id=1;
--查詢資料
select * from bookInfo where book_id=1;
--查詢資料的各種條件的排序
select * from bookInfo 
    [where 條件]
        [group by 列名]
            [having 條件] 
                [order by 列名 desc|asc]
                    [limit 數量] 

DCL(Data Control Language)資料控制語言

用於控制不同資料段直接的許可和
訪問級別的語句。這些語句定義了資料庫、表、欄位、使用者的訪問許可權和安全級別。主要的
語句關鍵字包括 grant、revoke 等。

使用者管理:

-- 檢視使用者
select current_user(), user();
select * from mysql.user;

--建立使用者
-- 特別需要注意,在 MySQL 中,賬號由兩部分組成:
-- 1. user
-- 2. host
-- 即使 user 相同,只要 host 不同,也會被認為是不同賬號。
-- 這樣可以非常方便對來自不同 ip 地址的訪問進行精細的許可權控制。
-- 預設情況下,建立的使用者 host 為 '%',這是一個匹配符,跟模糊查詢裡的意思一樣,表示匹配所有
create user user_name identified by '密碼';             -- 所有連線
create user 
[email protected]
'127.0.0.1' identified by '密碼'; -- 本地連線 create user [email protected]'192.168.%' identified by '密碼'; -- 192.168 網段的連線 --修改使用者密碼 set password for '使用者名稱'@'服務地址' = password('新密碼'); --刪除使用者 drop user user_name; -- 增加使用者 insert into mysql.user(host, user, password) values (xx, yy, zz); -- 修改密碼 update mysql.user set password=password('新密碼') where user='user_name' and host='%'; -- 修改許可權 update mysql.user set event_priv='Y' where user='user_name' and host='%'; -- 注意,使用 sql 語句修改使用者跟許可權之後,需要手動重新整理許可權表 flush privileges;

許可權管理

--授予許可權
grant all on *.* to [email protected]'127.0.0.1';        -- 將所有資料庫上的所有權利都授予通過本機連線的使用者!
grant all privileges on database_name.* to [email protected]'%'; -- 將資料庫  上的所有權利都授予所有連線的 使用者!
grant select on database_name.table_name to [email protected]'%';  -- 將資料庫上的 表資料表的訪問許可權開放給所有使用者。

--檢視使用者許可權
show grants for [email protected]'%';

-- 授權的相對完整語法為:
grant all/alter/create/drop/select/update/delete
  on *.* -- db.*/db.table
  to 'user'@'host'
  identified by '密碼'
  with max_user_connections 2
       max_connections_pser_hour 5;