MariaDB基礎(chǔ)

關(guān)系型數(shù)據(jù)庫

關(guān)系數(shù)據(jù)庫(英語:Relational database),是創(chuàng)建在關(guān)系模型基礎(chǔ)上的數(shù)據(jù)庫,借助于集合代數(shù)數(shù)學(xué)概念和方法來處理數(shù)據(jù)庫中的數(shù)據(jù)?,F(xiàn)實(shí)世界中的各種實(shí)體以及實(shí)體之間的各種聯(lián)系均用關(guān)系模型來表示。關(guān)系模型是由埃德加·科德于1970年首先提出的,并配合“科德十二定律”。現(xiàn)如今雖然對此模型有一些批評意見,但它還是數(shù)據(jù)存儲的傳統(tǒng)標(biāo)準(zhǔn)。標(biāo)準(zhǔn)數(shù)據(jù)查詢語言SQL就是一種基于關(guān)系數(shù)據(jù)庫的語言(SQL不是基于關(guān)系數(shù)據(jù)庫的語言),這種語言執(zhí)行對關(guān)系數(shù)據(jù)庫中數(shù)據(jù)的檢索和操作。

關(guān)系模型由關(guān)系數(shù)據(jù)結(jié)構(gòu)、關(guān)系操作集合、關(guān)系完整性約束三部分組成。------引自維基百科

MariaDB

MariaDB數(shù)據(jù)庫管理系統(tǒng)是MySQL的一個分支,主要由開源社區(qū)在維護(hù),采用GPL授權(quán)許可。開發(fā)這個分支的原因之一是:甲骨文公司收購了MySQL后,有將MySQL閉源的潛在風(fēng)險(xiǎn),因此社區(qū)采用分支的方式來避開這個風(fēng)險(xiǎn)。

MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能輕松成為MySQL的代替品。在存儲引擎方面,10.0.9版起使用XtraDB(名稱代號為Aria)來代替MySQL的InnoDB。------引自維基百科

MariaDB和MySql的區(qū)別

其實(shí)MariaDB和MySql是沒有什么區(qū)別的,在Sun公司被Oracle收購后,按照Oracle的風(fēng)格MySql是肯定會被閉源的,考慮到這種情況下所以MySql之父就另開了MariaDB分支。
MariaDB跟MySQL在絕大多數(shù)方面是兼容的,對于開發(fā)者來說,幾乎感覺不到任何不同。目前MariaDB是發(fā)展最快的MySQL分支版本,新版本發(fā)布速度已經(jīng)超過了Oracle官方的MySQL版本。
MariaDB 是一個采用Aria存儲引擎的MySQL分支版本,是由原來 MySQL 的作者M(jìn)ichael Widenius創(chuàng)辦的公司所開發(fā)的免費(fèi)開源的數(shù)據(jù)庫服務(wù)器。

SQL

SQL是一個標(biāo)準(zhǔn)的數(shù)據(jù)庫語言,是面向集合的描述性非過程化語言。
它功能強(qiáng),效率高,簡單易學(xué)易維護(hù)。SQL語言分為以下四大類:
DDL、DML、DCL、DQL。

DDL(數(shù)據(jù)庫定義語言)

用來創(chuàng)建數(shù)據(jù)庫中的各種對象-----表、視圖、索引、同義詞、聚簇等,常用的命令如create, drop, alter等

  • create:用來創(chuàng)建數(shù)據(jù)庫,表,索引,視圖等。常用語法:
# 創(chuàng)建數(shù)據(jù)庫:
CREATE DATABASE [IF NOT EXISTS] db_name;

# 創(chuàng)建表:
CREATE TABLE [IF NOT EXISTS] tbl_name
    (create_definition,...)
    [table_options];
  • drop:用來刪除數(shù)據(jù)庫,表,索引,視圖等。常用語法:
# 刪除數(shù)據(jù)庫
DROP DATABASE [IF EXISTS] db_name;

# 刪除表
DROP TABLE [IF EXISTS]  tbl_name [, tbl_name] ..;
  • alter:用來修改數(shù)據(jù)庫,表或者表字段,索引,視圖等信息。常用語法:
# 修改數(shù)據(jù)庫信息
ALTER DATABASE [db_name] [DEFAULT] CHARACTER SET [=] charset_name;

# 修改表的信息:
ALTER TABLE tbl_name [alter_specification [, alter_specification] ...];

#alter_specification可以是一下內(nèi)容:
alter_specification:
    ADD [COLUMN] col_name column_definition
        [FIRST | AFTER col_name ]
|   ADD [CONSTRAINT] PRIMARY KEY
|   ALTER [COLUMN] col_name {SET DEFAULT value | DROP DEFAULT}
|   DROP [COLUMN] col_name
|   DROP PRIMARY KEY
|   CHANGE [COLUMN] old_col_name new_col_name column_definition
        [FIRST|AFTER col_name]
|   MODIFY [COLUMN] col_name column_definition
        [FIRST | AFTER col_name];

DML(數(shù)據(jù)庫操縱語言)

DML是對數(shù)據(jù)中的表數(shù)據(jù)的可以執(zhí)行的操作的語言。數(shù)據(jù)操作指令包括:update、insert、delete。

  • insert:向指定的數(shù)據(jù)表中插入數(shù)據(jù),可以是一條或者多條數(shù)據(jù),語法:
INSERT [INTO] tbl_name [(col_name,...)]
    {VALUES | VALUE} (value1),(value2)...;
  • update:用來修改表中的數(shù)據(jù)。語法:
UPDATE  table_name
    SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
    [WHERE where_condition];
  • delete:刪除表中的數(shù)據(jù)。語法:
DELETE [col_name]  FROM tbl_name
    [WHERE where_condition];

注意:在更新和刪除表里面的數(shù)據(jù)時(shí)一定要使用where字句,否則默認(rèn)是操作所有行,在delete中語句中如果指定col_name則是刪除該字段的數(shù)據(jù)。

DCL(數(shù)據(jù)庫控制語言)

DCL用來授予或回收訪問數(shù)據(jù)庫的某種特權(quán),并控制數(shù)據(jù)庫操縱事務(wù)發(fā)生的時(shí)間及效果,對數(shù)據(jù)庫實(shí)行監(jiān)視等。對事務(wù)的處理主要是rollback和commit。主要還是對權(quán)限的控制。語法如下:

GRANT privileges ON database.table TO 'username'@'host' IDENTIFIED BY('password');

說明:database,table用*表示所有,host用%表示任意參數(shù)。

DQL(數(shù)據(jù)庫查詢語言)

數(shù)據(jù)查詢語言DQL基本結(jié)構(gòu)是由SELECT子句,F(xiàn)ROM子句,WHERE
子句組成。語法:

SELECT
    [ALL | DISTINCT  ]  select_expr1 [, select_expr2 ...]
    [FROM table_references
    [WHERE where_condition]
    [GROUP BY {col_name | expr | position}
      [ASC | DESC] [HAVING where_condition]]
    [ORDER BY {col_name | expr | position}
      [ASC | DESC], ...]
    [LIMIT {[offset,] row_count | row_count OFFSET offset}]

這里只是初略的介紹一下sql語句的一些基礎(chǔ)用法,其實(shí)在sql中其實(shí)最重要的是DQL語句中的一些高級應(yīng)用。如子查詢,復(fù)合查詢等。這將在后邊的文章里面介紹。

練習(xí):

學(xué)生選課系統(tǒng):

  1. 創(chuàng)建學(xué)生選課系統(tǒng)
  2. 切換數(shù)據(jù)庫
  3. 創(chuàng)建學(xué)生表 TbStudent,主鍵stuid ,姓名stuname,性別stusex,生日stubirth,電話stutel,住址stuaddr,照片stuphoto(以二進(jìn)制存)
  4. 創(chuàng)建課程表TbCourse
    主鍵cosid, 班級名稱cosname,學(xué)分coscredit,課程描述cosintro
  5. 學(xué)生選課記錄表TbSC
    主鍵scid,學(xué)生外鍵sid ,班級外鍵cid,創(chuàng)建日期scdate, 分?jǐn)?shù)score

代碼如下:

# 創(chuàng)建SCC數(shù)據(jù)庫,默認(rèn)字符集為utf-8:
create database if not exists SCC default charset utf8;

use SCC;

#創(chuàng)建tbstudent學(xué)生表,默認(rèn)字符集為utf-8,存儲引擎為innodb:
create table if not exists tbstudent(
stuid int(15) not null primary key,
stuname varchar(20) not null,
stusex tinyint(1) default 1,
stubirth datetime,
stutel varchar(11),
stuaddr varchar(255),
stuphoto longblob
)engine innodb default charset utf8;

# 創(chuàng)建tbcourse課程表,默認(rèn)字符集為utf-8,存儲引擎為innodb:
create table if not exists tbcourse(
cosid int not null primary key,
cosname varchar(20) not null,
coscredit int not null,
cosintro varchar(200)
)engine innodb default charset utf8;

#創(chuàng)建tbsc選課記錄表,默認(rèn)字符集為utf-8,存儲引擎為innodb:
create table if not exists tbsc(
scid int not null primary key auto_increment,
sid int not null,
cid int not null,
scdate datetime not null,
score decimal(3,1)
)engine innodb default charset utf8;

# 為三張表創(chuàng)建外鍵約束:
aler table tbsc add constrait fs_sid foreign key(sid) references tbstudent(stuid) on delete cascade on update cascade;
aler table tbsc add constrait fs_cid foreign key(cid) references tbsourse(cosid) on delete set null on update cascade

# 向?qū)W生表中插入數(shù)據(jù):
insert into tbstudent (stuid, stuname, stusex, stubirth, stuaddr, stuphoto) values
(1001, '張三豐', default, '1978-1-1', '成都市一環(huán)路西二段17號', null);
insert into tbstudent (stuid, stuname,stubirth) values
(1002, '郭靖', '1980-2-2');
insert into tbstudent (stuid, stuname, stusex, stubirth, stuaddr) values
(1003, '黃蓉', 0, '1982-3-3', '成都市二環(huán)路南四段123號');
insert into tbstudent (stuid, stuname, stusex, stubirth, stuaddr, stuphoto) values 
(1004, '張無忌', 1, '990-4-4', null, null),
(1005, '丘處機(jī)', 1, '1983-5-5', '北京市還定去寶勝北里西區(qū)28號', null),
(1006, '王處一', 1, '1985-6-6', '深圳市寶安區(qū)寶安大道5010號',null),
(1007, '劉處玄', 1, '1987-7-7', '鄭州市金水區(qū)緯五路21號', null),
(1008, '孫不二', 0, '1989-8-8', '武漢市光谷大道61號', null),
(1009, '平一指', 1, '1992-9-9', '西安市雁塔區(qū)高新六路52號', null),
(1010, '老不死', 1, '1993-10-10', '廣州市天河區(qū)元崗路310號', null),
(1011, '王大錘', 0, '1994-11-11', null, null),
(1012, '隔壁老王', 1, '1995-12-12', null, null),
(1013, '郭嘯天', 1, '1977-10-25', null, null);

# 刪除學(xué)生表中id為1004的學(xué)生信息:
delete from tbstudent where stuid=1004;

# 更新學(xué)生表中id為1002的學(xué)生的信息:
update tbstudent set stubirth='1980-12-12',stuaddr='上海市寶山區(qū)同濟(jì)支路199號' where stuid=1002;

# 向課程標(biāo)準(zhǔn)插入數(shù)據(jù)
insert into tbcourse values 
(1111, 'C語言程序設(shè)計(jì)', 3,'大神級講師教授需要搶座'),
(2222, 'Java程序設(shè)計(jì)', 3, null),
(3333, '數(shù)據(jù)庫概論', 2, null),
(4444, '操作系統(tǒng)原理', 4, null);

# 向?qū)W生選課記錄表中插入數(shù)據(jù):
insert into tbsc (sid, cid, scdate, score) values
(1001, 1111, '2016-9-1', 95),
(1002, 1111, '2016-9-1', 94),
(1001, 2222, now(), null),
(1001, 3333, '2017-3-1', 85),
(1001, 4444, now(), null),
(1002, 4444, now(), null),
(1003, 2222, now(), null),
(1003, 3333, now(), null),
(1005, 2222, now(), null),
(1006, 1111, now(), null),
(1006, 2222, '2017-3-1', 80),
(1006, 3333, now(), null),
(1006, 4444, now(), null),
(1007, 1111, '2016-9-1', null),
(1007, 3333, now(), null),
(1007, 4444, now(), null),
(1008, 2222, now(), null),
(1010, 1111, now(), null);
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Lamp之MariaDB 數(shù)據(jù)庫結(jié)構(gòu)模型分類 1、層次模型 2、網(wǎng)狀模型 3、關(guān)系模型 關(guān)系模型的組成部分 二維關(guān)...
    魏鎮(zhèn)坪閱讀 7,442評論 0 2
  • 轉(zhuǎn) # https://www.cnblogs.com/easypass/archive/2010/12/ 08/...
    呂品?閱讀 10,139評論 0 44
  • 數(shù)據(jù)庫簡介關(guān)系型數(shù)據(jù)庫MySQL安裝和使用SQL語言 一、數(shù)據(jù)庫簡介 (一)數(shù)據(jù)庫的發(fā)展 文件系統(tǒng):磁盤文件存儲數(shù)...
    哈嘍別樣閱讀 481評論 0 1
  • 倏忽間走到了23。黃粱一夢,卻也尚未驚慌失措。16歲前莫名覺得自己過不了20,如今已然飛逝了七年。 去年今日。畢業(yè)...
    欖仁小姐寫字的地方閱讀 576評論 3 2
  • 秀婷 中周進(jìn)行到了第二天 你已經(jīng)精疲力盡了 兩天來 各種狀態(tài)都活現(xiàn)出來了 哭哭笑笑 打打鬧鬧 都來了 真是佩服你 ...
    洪秀婷閱讀 119評論 0 0

友情鏈接更多精彩內(nèi)容