1、當需要通過yum安裝mysql資料庫
首先需要進行yum源的更新
[root@server ~]# yum install mysql-community-server mysql-community-devel
2、源碼包安裝
3、建立資料庫
<code>mysql> create database database_name </code><code>default</code> <code>character set utf8;</code>
4、標明資料庫
<code>mysql> use database_name;</code>
5、建立表
<code>mysql> create table table_name</code>
<code> </code><code>->(</code>
<code> </code><code>->column_1 column_type column attributes,</code>
<code> </code><code>->column_2 column_type column attributes,</code>
<code> </code><code>->column_3 column_type column attributes,</code>
<code> </code><code>->primary key (column_name),</code>
<code> </code><code>->index index_name(column_name)</code>
<code> </code><code>->) engine=innodb </code><code>default</code> <code>charset=utf8 auto_increment=1;</code>
6、建立索引
<code>mysql> alter table table_name add index index_name(column_name);</code>
<code>mysql> create index index_name on table_name(column_name);</code>
<code>mysql> create unique index index_name on table_name(column_name); </code><code>#建立唯一索引</code>
7、修改表
1)更改表名
<code>mysql> alter table table_name rename new_table_name;</code>
2)添加列
<code>mysql> alter table table_name add column column_name colomn attributes;</code>
例如:
<code>mysql> alter table my_table add column my_column text not </code><code>null</code><code>;</code>
first 指定插入的列位于表的第一列
after 把新列放在已經存在的列的後面
例如:
<code>mysql> alter table my_table add column my_col text not </code><code>null</code> <code>first;</code>
<code>mysql> alter table my_table add column my_col text not </code><code>null</code> <code>after my_other _column;</code>
3)删除列
<code>mysql> alter table table_name drop column column_name;</code>
4)添加索引
<code>mysql> alter table table_name add index index_name (column_name1,column_name2,……);</code>
<code>mysql> alter table table_name add unique index_name (column_name);</code>
<code>mysql> alter table table_name add primary key(my_column);</code>
删除索引
<code>mysql> alter table table_name drop index index_name;</code>
如:
<code>mysql> alter table test10 drop primary key;</code>
5)更改列定義
用change或是modify指令可以更改列的名稱或是屬性。要更改列的名稱,還必須重新定義列的屬性。例如:
<code>mysql> alter table table_name change original_column_name new_column_name int not </code><code>null</code><code>;</code>
注意:必須要重新定義列的屬性!!!
<code>mysql> alter table table_name modify col_1 clo_2 varchar(200);</code>
8、插入表
<code>mysql> insert into table_name (column_1,column_2,…..) values (value1,value2,……);</code>
如果要存入字元串,則需要使用單引号“’”将字元串括起來,但是需要注意字元的轉意
如:
<code>mysql> insert into table_name (text_col,int_col) value (\’hello world\’,1);</code>
需要轉義的字元有:單引号’ 雙引号” 反斜杠\ 百分号% 下劃線_
可以連續使用兩個單引号轉義單引号
9、更新表
<code>mysql> updata table_name set col__1=vaule_1 where col=vaule;</code>
10、删除表/庫
<code>mysql> drop table table_name;</code>
<code>mysql> drop database database_name;</code>
11、檢視表/庫
<code>mysql> show tables;</code>
<code>mysql> show databases;</code>
12、檢視列的屬性和類型
<code>mysql> show columns from table_name;</code>
<code>mysql> show fields from table_name;</code>
13、查找語句
<code>mysql> select column_1,column_2,column_3 from table_name;</code>
14、修改密碼
<code>mysql> update mysql.user set authentication_string=password(</code><code>'123456'</code><code>) where user=</code><code>'root'</code> <code>and Host = </code><code>'localhost'</code><code>;</code>
<code>mysql> alter user root@localhost identified by </code><code>'123456'</code><code>;</code>
<code>mysql> UPDATE user SET Password=PASSWORD(</code><code>'123456'</code><code>) where USER=’root’;</code>
<code>mysqladmin -uroot -p old_password password new_password</code>
15、使用者授權
<code>mysql> GRANT ALL PRIVILEGES ON mysql.* TO tom@% identified by </code><code>'123456'</code><code>;</code>
第一個*号 是代表所有表 ,第二個* 是代表改資料庫下的所有表。
16、使用where
限制從查詢(select)傳回的記錄行
<code>mysql> select * from table_name where user_id = 2;</code>
如果要對存儲字元串(char、varchar等類型)的列進行比較,就需要在where子句中用單引号把要比較的字元串括起來
<code>mysql> select * from users where city =‘San Francisco’;</code>
通過向where子句添加and或是or,可以一次比較幾個運算符
<code>mysql> select * from users where userid=1 or city=‘San Francisco’;</code>
<code>mysql> select 8 from users where state=’CA’ and city=’San Francisco’;</code>
注意:空值不能和表中的任何運算符比較,對于空值,需要使用is null或是is not null謂詞
<code>mysql> select * from users where zip!=’1111′ or zip=’1111′ or zip is </code><code>null</code><code>;</code>
如果要找到包含任何值(除空值以外)的所有記錄,可以
<code>mysql> select * from table_name where zip is not </code><code>null</code><code>;</code>
17、使用between
使用between可以選擇在某個範圍内的值,between可用于數字,日期,文本字元串。
<code>mysql> select * from users where lastchanged between 20000614000000 and 20000614235959;</code>
<code>mysql> select * from users where lname between ‘a’ and ‘m’;</code>
18、使用in/not in
若某列可能傳回好幾個可能的值,就可以使用in謂詞
<code>mysql> select * from users where state=’RI’ or state=’NH’ or state=’VT’ or state=’MA’ or state=’ME’;</code>
可改寫為:
<code>mysql> select * from users where state </code><code>in</code> <code>(‘RI’,</code><code>'NH’,'</code><code>VY’,</code><code>'MA’,'</code><code>ME’);</code>
如果要達到相同的結果,但結果集相反,可使用not in 謂詞
<code>mysql> select * from user where state not </code><code>in</code> <code>(‘RI’,</code><code>'NH’,'</code><code>VT’,</code><code>'MA’,'</code><code>ME’);</code>
19、使用like
如果需要使用通配符,則要使用like
<code>mysql> select * from users where fname like ‘Dan%’; </code><code>#%比對零個字元</code>
<code>mysql> select * from users where fname like ‘J___’; </code><code>#比對以J開頭的任意三字母詞</code>
<code> </code><code>#mysql中like不區分字母大小寫</code>
20、order by
order by語句可以指定查詢中傳回的行的順序,可對任意列類型排序,通過在末尾放置asc或是desc以設定按升序或是降序排列,如果不設定,預設使用asc
<code>mysql> select * from users order by lname,fname;</code>
可以按照需要根據任意多的列排序,也可以混合使用asc和desc
<code>mysql> select * from users order by lname asc, fname desc;</code>
21、limit
limit限制從查詢中傳回的行數,可以指定開始的行數和希望傳回的行數
得到表中的前5行:
<code>mysql> select * from users limit 0,5;</code>
<code>mysql> select * from users order by lname,fname limit 0,5;</code>
得到表的第二個5行:
<code>mysql> select * from users limit 5,5;</code>
22、group by 與聚合函數
使用group by後Mysql就能建立一個臨時表,記錄下符合準則的行與列的所有資訊
count() 計算每個集合中的行數
<code>mysql> select state,count(*) from users group by state;</code>
*号訓示應該計算集合中的所有行
<code>mysql> select count(*) from users;</code>
計算表中所有的行數
可以在任何函數或列名後使用單詞as,然後指定一個作為别名的名稱。如果需要的列名超過一個單詞,就要使用單引号把文本字元串括起來
sum() 傳回給定列的數目
min() 得到每個集合中的最小值
max() 得到每個集合中的最大值
avg() 傳回集合的品均值
having
限制通過group by顯示的行,where子句顯示在group by中使用的行,having子句隻限制顯示的行。
23、連接配接表
在select語句的from部分必須列出所有要連接配接的表,在where部分必須顯示連接配接所用的字段。
<code>mysql> select * from companies,contacts where companies.company_ID=contacts.company_ID;</code>
當對一個字段名的引用不明确時,需要使用table_name.column_name文法指定字段來自于哪個表
本文轉自MQ_douer 51CTO部落格,原文連結:http://blog.51cto.com/douer/1934762,如需轉載請自行聯系原作者