mysql資料庫基礎知識
sql:結構化查詢語句
rdbms:關係型資料庫管理系統
資料庫設計三大正規化:
1:原子性。資料庫的欄位都是具有單一屬性的,不可再分。
2:唯一性。每個非主屬性都完全函式依賴於鍵碼。記錄具有唯一標識。每列都跟主鍵有關係。(以一對多為例)
3:消除傳遞依賴。每個非主屬性都不偉遞領帶於鍵碼。即任何欄位不能由其他欄位派生出來,它要求欄位沒有冗餘。
沒有冗餘的資料庫設計可以做到。但是,沒有冗餘的資料庫未必是最好的資料庫,有時為了提高執行效率,就必須降低正規化標準,適當保留冗餘資料。具體做法是:在概念資料模型設計時遵守第三正規化,降低正規化標準的工作放到物理資料模型設計時考慮。降低正規化就是增加欄位,允許冗餘。
INNER JOIN(內連線,或等值連線):獲取兩個表中欄位匹配關係的記錄。LEFT JOIN(左連線):獲取左表所有記錄,即使右表沒有對應匹配的記錄。RIGHT JOIN(右連線): 與 LEFT JOIN 相反,用於獲取右表所有記錄,即使左表沒有對應匹配的記錄。
[email protected]# mysql -u root -p password;Enter password:******* mysql>use RUNOOB;Database changed mysql> SELECT * FROM tcount_tbl;+-----------------+----------------+接下來我們就使用MySQL的INNER JOIN(也可以省略 INNER 使用 JOIN,效果一樣)來連線以上兩張表來讀取runoob_tbl表中所有runoob_author欄位在tcount_tbl表對應的runoob_count欄位值:| runoob_author | runoob_count |+-----------------+----------------+| mahran |20|| mahnaz | NULL ||Jen| NULL ||Gill|20||JohnPoul|1||Sanjay|1|+-----------------+----------------+6 rows inset(0.01 sec) mysql> SELECT *from runoob_tbl;+-------------+----------------+-----------------+-----------------+| runoob_id | runoob_title | runoob_author | submission_date |+-------------+----------------+-----------------+-----------------+|1|Learn PHP |JohnPoul|2007-05-24||2|LearnMySQL|Abdul S |2007-05-24||3| JAVA Tutorial|Sanjay|2007-05-06|+-------------+----------------+-----------------+-----------------+3 rows inset(0.00 sec) mysql>
mysql> SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl a INNER JOIN tcount_tbl b ON a.runoob_author = b.runoob_author;+-----------+---------------+--------------+| runoob_id | runoob_author | runoob_count |+-----------+---------------+--------------+|1|JohnPoul|1||3|Sanjay|1|+-----------+---------------+--------------+2 rows inset(0.00 sec)等價於:
mysql> SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl a, tcount_tbl b WHERE a.runoob_author = b.runoob_author;
mysql> SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl a LEFT JOIN tcount_tbl b ON a.runoob_author = b.runoob_author;+-------------+-----------------+----------------+| runoob_id | runoob_author | runoob_count |+-------------+-----------------+----------------+|1|JohnPoul|1||2|Abdul S | NULL ||3|Sanjay|1|+-------------+-----------------+----------------+3 rows inset(0.02 sec)
mysql> SELECT b.runoob_id, b.runoob_author, a.runoob_count FROM tcount_tbl a RIGHT JOIN runoob_tbl b ON a.runoob_author = b.runoob_author;+-------------+-----------------+----------------+| runoob_id | runoob_author | runoob_count |+-------------+-----------------+----------------+|1|JohnPoul|1||2|Abdul S | NULL ||3|Sanjay|1|+-------------+-----------------+----------------+3 rows inset(0.02 sec)
http://www.runoob.com/mysql/mysql-join.html
DML 部分:
SELECT - 從資料庫表中獲取資料
UPDATE - 更新資料庫表中的資料
DELETE - 從資料庫表中刪除資料
INSERT INTO - 向資料庫表中插入資料
DDL 語句:
CREATE DATABASE - 建立新資料庫
ALTER DATABASE - 修改資料庫
CREATE TABLE - 建立新表
ALTER TABLE - 變更(改變)資料庫表
DROP TABLE - 刪除表
CREATE INDEX - 建立索引(搜尋鍵)
DROP INDEX - 刪除索引
更新重點句子:
alter table add column price int(10)
alter table drop name;
drop table cq_user;
select distinct name from cq_itemtype;
select * from cq_user where namein('haha2','haha1;);
select * from cq_user wherename='haha1' or name='haha2';
select * from cq_itemtype where namelike '%石%';
select * from cq_itemtype where namelike '50朵_ _玫瑰';(注意是兩個_,因為白、紅是漢字)
select * from cq_itemtype where id like'111_ _ _';(111開頭的6位數的id)
select * from cq_action where paramlike '!%data%' escape '!';
select count(*) from usertype;
select max(level) from cq_user;
select * from cq_itemtype group byname;
select * from cq_itemtype order bydesc/asc;
update cq_itemtype set name='白色',level=10 where id=10;
update cq_user set level=level+1;
delete from cq_action where id=1;
型別屬性:
◦ZEROFILL
–適用於所有數值型別資料資料列
–作用:如果數值的寬度小於定義的顯示寬度,則在數值前填充0
◦UNSIGNED
–作用:不允許資料列中出現負數,無符號型
◦AUTO_INCREMENT 自增長
◦NULL/NOT NULL
–作用:控制資料列的值是否為空
主要的資料型別:
整數資料型別:int 4個位元組
浮點資料型別:float4個位元組,double8個位元組
日期時間型別:date(0000-00-00),datetime(0000-00-0000:00:00)
字串型別:char(n),varchar(n)
建立表:
CREATE TABLE<表名>
( <列名> <資料列型別><列屬性> [{<約束>}],
<列名> <資料列型別><列屬性>[{<約束>}],
……
)
create table cq_action(
id int(4) not null primary key,
name int(4))
create table cq_action(
id int(4) not null,
name int(4),
primary key(id))
表的修改:
alter table <表名> add<列定義>|<列約束>
alter table cq_action add price int(4)
alter tabke cq_action alter priceint(10)
alter table cq_action drop name
刪除表“
drop table cq_name
SELECT命令的格式
select [all|distinct] 列名 [[as] 別名]
from 表名[[as]表別名]
[where <檢索條件>]
[group by <列名>[having<條件表示式>]]
[order by <列名>[asc|desc]]
[limit 行數]
select * from cq_action
select distinct name from cq_action(顯示錶cq_action中的name欄位,去除重複)
select name as username from cq_action(查詢顯示的結果為別名)
select a.id,b.name from cq_userasa,cq_subject as b where a.id=b.typeId
條件查詢:
=,>,<,>=,<=,!=,<>比較大小
and ,or,not多重條件
in確定集合
like/not like字元匹配
is null空值
between and(>= and <=)確定範圍
select * from user where money>0andlevel>100
select * from user where id between 1and 4
select * from itemType where name='彎刀' or name='菜刀'
MySql的like語句中的萬用字元:百分號、下劃線和escape
%代表任意多個字元
Sql程式碼
select * from user where usernamelike'%huxiao';
select * from user where usernamelike'huxiao%';
select * from user where usernamelike'%huxiao%';
_代表一個字元
Sql程式碼
select * from user where username like'_';
select * from user where usernamelike'huxia_';
select * from user where usernamelike'h_xiao';
如果我就真的要查%或者_,怎麼辦呢?使用escape,轉義字元後面的%或_就不作為萬用字元了,注意前面沒有轉義字元的%和_仍然起萬用字元作用
Sql程式碼
select username from gg_user whereusernamelike '%xiao/_%' escape '/';
select username from gg_user where usernamelike'%xiao/%%' escape '/';
select * from user limit 100;
常用庫函式:
count接列值計算個數,就是一共有幾行,不計算空值,但可以計算到0。
max,min,avg,sum
select count(*) from itemtype
select max(level) from user;
分組查詢:group by將查詢結果按屬性或屬性列組合在行的方向上進行分組
select * from itemtype group by name
order by id desc(降序) asc(升序)
資料查詢:當查詢的時候涉及到兩個以上的表就是連線查詢。兩種方式:1)表與表之間滿足一定條件的列和行進行連線,from子句指明進行連線的表名,where子句指明連線的列名及其連線條件;
2)用join進行連線,用join關鍵字的時候,from子句中應該有關鍵詞on與之對應(個人不是很習慣這種方式)
select * from cq_goods,cq_itemtype
where cq_goods.itemtype=cq_itemtype.id
資料更新:
插入資料:insert into <表名> [<列名1>,<列名2>…] values(<值>)
列名是可選的
列名的順序沒有要求,但是如果有指定列名的話values後面的值也要是相對應的
insert intocq_action(id,id_next,id_nextfail,type,data,param) values
(500,0,505,503, 10000,'')
insert into cq_action values
(500,0,505,503, 10000,'')
修改資料:
update <表名>
set <列名>=<表示式>[,<列名>=<表示式>…..]
[where<條件>]
update cq_user set level=lever+1
刪除資料:
delete from <表名>
[where<條件>]
delete from cq_user where id=1000
delete from cq_user
--------------分界線--------下面的東西初級的用得少
數值型別:
整型:
BIT[(M)]
位欄位型別。M表示每個值的位數,範圍為從1到64。如果M被省略, 預設為1
TINYINT[(M)] [UNSIGNED][ZEROFILL]
很小的整數。
SMALLINT[(M)] [UNSIGNED] [ZEROFILL]
小的整數。
MEDIUMINT[(M)] [UNSIGNED][ZEROFILL]
中等大小的整數。
INT[(M)] [UNSIGNED] [ZEROFILL]
普通大小的整數。
BIGINT[(M)] [UNSIGNED][ZEROFILL]
大整數。
浮點型:
FLOAT[(M,D)] [UNSIGNED][ZEROFILL]
小(單精度)浮點數。如果M和D沒有寫,那麼單精度浮點數精確到大約7位小數位
DOUBLE[(M,D)] [UNSIGNED][ZEROFILL]
普通大小(雙精度)浮點數。如果M和D沒有寫,那麼雙精度浮點數精確到大約15位小數位
日期和時間型別:
DATETIME型別
需要同時包含日期和時間資訊的值時使用,以‘YYYY-MM-DDHH:MM:SS’格式檢索和顯示DATETIME值,支援的範圍為'1000-01-0100:00:00'到'9999-12-31 23:59:59‘
DATE型別
只需要日期值而不需要時間部分,用'YYYY-MM-DD'格式檢索和顯示DATE值
TIMESTAMP[(M)]
時間戳。範圍是'1970-01-0100:00:00'到2037年
TIME型別
以'HH:MM:SS'格式顯示TIME值,但允許使用字串或數字為TIME列分配值。
YEAR[(2|4)]型別
兩位或四位格式的年。預設是四位格式。在四位格式中,允許的值是1901到2155和0000。在兩位格式中,允許的值是70到69,表示從1970年到2069年。
字元型別:
—CHAR
◦是固定長度的,每個值佔用相同的位元組,不夠的位數MySQL會在它的右邊用空格字元補足。
—VARCHAR
◦是一種可變長度的型別,每個值佔用其剛好的位元組數再加上一個用來記錄其長度的位元組即L+1位元組。
—BINARY
◦二進位制字串
—varbinary
◦可變長度的二進位制字串