參考:劉道成的mysql視頻
(此文章只用于自己之后復(fù)習(xí))
理論知識(shí)
1、表-->庫-->mysql數(shù)據(jù)庫服務(wù)器。
2、mysql是數(shù)據(jù)庫服務(wù)器,navcat,cmd的都是客戶端。
3、連接服務(wù)器:mysql -h localhost -u UserName -p Password(無分號(hào)結(jié)尾),
如果不寫-h默認(rèn)連localhost。
4、information_schema數(shù)據(jù)庫是MySQL系統(tǒng)自帶的數(shù)據(jù)庫,它提供了數(shù)據(jù)庫元數(shù)據(jù)的訪問方式。
PERFORMANCE_SCHEMA數(shù)據(jù)庫,主要用于收集數(shù)據(jù)庫服務(wù)器性能參數(shù)。
mysql數(shù)據(jù)庫存放mysql的信息,好比你登錄數(shù)據(jù)庫的帳號(hào)密碼都在mysql庫中的user表中。
所以以上三數(shù)據(jù)庫不可以動(dòng)!!!
5、mysql不能更改數(shù)據(jù)庫的名字,一些客戶端工具能做到是他們先復(fù)制內(nèi)容到新名字的數(shù)據(jù)庫,然后刪了原來的數(shù)據(jù)庫而達(dá)到的。!?。?!但是表名是可以更改的。
6、語句打錯(cuò)了,可以用\c退語句。注意\。
7、
數(shù)值型
——整型(類如int(D))

比如:alter table student add id int(5) unsigned zerofill not null default 0;
unsigned :無符號(hào),影響存儲(chǔ)范圍。
M:代表寬度,在zerofill時(shí)候才有意義。
zerofill:用0填充,如果某一列是zerofill,那么默認(rèn)unsigned。
not null default 0:設(shè)置默認(rèn)值,一般數(shù)值為0,字符串為"";
(M設(shè)置為1,依舊可以輸入35這樣的數(shù),它對數(shù)據(jù)無影響)
——小數(shù)型
浮點(diǎn)型(M,D)——float(M,D)
定點(diǎn)型(M,D)——decimal(M,D)
M:精度,總位數(shù)(不包含點(diǎn))
D:標(biāo)度(小數(shù)位)
(float(6,2)——>-9999.99-9999.99
float(6,2) unsigned ——>0-9999.99
區(qū)別整型)
小數(shù)點(diǎn)超位數(shù)會(huì)四舍五入,但是不同于我們以往的四舍五入。
(此處可百度下,反正我現(xiàn)在是是知道的,如果你以后忘了......那就百度百度)
定點(diǎn)比浮點(diǎn)更精確
字符型
char:定長char(M)
varchar:變長varchar(M)
M代表寬度,即可容納的字符數(shù)
用varchar,若存的字符數(shù)為N,N<M則實(shí)際占N個(gè)字符(多出1-2個(gè)字節(jié),用來標(biāo)志字符長度)varchar利用率永遠(yuǎn)小于100,char可能能達(dá)到100
char不夠M個(gè)字符用空格補(bǔ),讀取的時(shí)候會(huì)把尾部空格去掉,所以如果原來內(nèi)容后面就有空格,就會(huì)丟失。
速度:定長快一些。

日期型(要加雙引號(hào))
——年--->year
范圍:1901-2155
如果輸入兩位:00-69表示2000-2069
如果輸入兩位:70-99表示1970-1999
——年-月-日--->date
典型格式:1992-08-12
范圍:1000-01-01~9999-12-31
——09:00:00--->time
典型格式:hh:mm:ss
范圍:-838:59:59~838:59:59
——年-月-日 hh:mm:ss --->datetime
典型格式:1980-05-12 12:34:24
范圍:1000-01-01 00:00:00~9999-12-31 23:59:59
一般default:1000-01-01 00:00:00
——時(shí)間戳(用int):1970-01-01 00:00:00到當(dāng)前的秒數(shù),一般注冊時(shí)間商品發(fā)布時(shí)間都是用時(shí)間戳表示而非datetime,因?yàn)閐t不容易計(jì)算。
8、還有enum型,set型,不符合關(guān)系型數(shù)據(jù)庫設(shè)計(jì)理解,而且內(nèi)存用的也沒有少很多,所以不多用,有需要以后自己查吧。
9、charset 字符集
engine存儲(chǔ)引擎
auto_increment 值自動(dòng)增長
primary key 主鍵值不可能重復(fù)
代碼
1、基礎(chǔ)語句
1.show databases;
2.create database DBname;
3.use DBname;
4.show tables;
2、增刪改查
//增
//創(chuàng)建表(舉例,tablename = student)之簡單型
create table student
(id int,
name char(5),
phone_number varchar(10),
age int
);
//創(chuàng)建表(舉例,tablename = student)之復(fù)雜型(解釋見理論9)
create table stydent(
id int primary key auto_increment,
name char(3),
age tinyint unsigned not null default 0,
email varchar(30) not null default "",
tel char(11) not null default "",
intro varchar(1000) not null default "",
salary decimal(7,2) not null default 1800.68,
riqi date not null default "2012-01-01"
)charset utf8;
//多行插入,單行應(yīng)該就會(huì)了哈
insert into student
(id,name,phone_number,age)
values
(1,'Mike','87492345','20'),
(2,'Jack','87498765','30');
//增加列
alter table student add id int(5) unsigned zerofill not null default 0;
//刪
drop database DBname;
drop table tablename;
//不加where導(dǎo)致全刪
delete from student
where id = 2;
//改
rename table oldName to newName;
);
update student
set
id = 2,
name = "Bill"
where name = "Mike";//類似的還有 where 1>0;where 1>2;
//查
desc tablename;(查看表結(jié)構(gòu),其實(shí)就是description)
select * from tablename;