欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL數(shù)據(jù)庫操作的基本命令

 更新時(shí)間:2017年05月19日 15:11:18   投稿:mrr  
這篇文章主要介紹了MySQL使用初步之MySQL數(shù)據(jù)庫的基本命令,需要的朋友可以參考下

一、創(chuàng)建數(shù)據(jù)庫:

 create data data _name;

 php中創(chuàng)建數(shù)據(jù)庫的兩種方法:(mysql_create_db(),mysql_query())

 $conn = mysql_connect(“l(fā)ocalhost”,”username”,”password”) or
 die ( “could not connect to localhost”);
 mysql_create_db(“data _name”) or
 die (“could not create data ”);
 $string = “create data data _name”;
 mysql_query( $string) or
 die (mysql_error()); 

二、選定數(shù)據(jù)庫

在創(chuàng)建表之前,必須要選定要創(chuàng)建的表所在的數(shù)據(jù)庫

選定數(shù)據(jù)庫:

 通過命令行客戶端:

use data _name

 通過

php: mysql_select_db()
 $conn = mysql_connect(“l(fā)ocalhost”,”username”,”password”) or
 die ( “could not connect to localhost”);
 mysql_select_db(“test”,$conn) or
 die (“could not select data ”);

三、創(chuàng)建表

create table table_name

如:

 create table table_name
 (
 column_1 column_type column attributes,
 column_2 column_type column attributes,
 column_3 column_type column attributes,
 primary key (column_name),
 index index_name(column_name)
 )

在命令行客戶端需要鍵入整個(gè)命令

在php中使用,mysql_query()函數(shù)

如:

 $conn = mysql_connect(“l(fā)ocalhost”,”username”,”password”) or
 die ( “could not connect to localhost”);
 mysql_select_db(“test”,$conn) or
 die (“could not select data ”);
 $query = “create table my_table (col_1 int not null primary key,
  col_2 text
  )”;
 mysql_query($query) or
 die (mysql_error());

四、創(chuàng)建索引

 index index_name(indexed_column) 

五、表的類型

 ISAM MyISAM BDB Heap

 聲明表類型的語法:

 create table table_name type=table_type
 (col_name column attribute);

默認(rèn)使用MyISAM

六、修改表

 alter table table_name

更改表名

 alter table table_name rename new_table_name

或者(高版本中)

 rename table_name to new_table_name

添加和刪除列

添加列:

alter table table_name add column column_name colomn attributes

例如:

 alter table my_table add column my_column text not null

first 指定插入的列位于表的第一列

after 把新列放在已經(jīng)存在的列的后面

    例如:

alter table my_table add column my_next_col text not null first
alter table my_table add column my_next_col text not null after my_other _column

刪除列:

alter table table_name drop column column name

添加和刪除索引:

 alter table table_name add index index_name (column_name1,column_name2,……)
 alter table table_name add unique index_name (column_name)
 alter table table_name add primary key(my_column)
 alter table table_name drop index index_name

如:

alter table_name test10 drop primary key

更改列定義:

  用change或是modify命令可以更改列的名稱或是屬性。要更改列的名稱,還必須重新定義列的屬性。例如:  

 alter table table_name change original_column_name new_column_name int not null

  注意:必須要重新定義列的屬性?。。?/p>

 alter table table_name modify col_1 clo_1 varchar(200) 

七、向表中輸入信息(insert)

 insert into table_name (column_1,column_2,column_3,…..)
 values (value1,value2,value3,……)

 如果要存入字符串,則需要使用單引號“'”將字符串括起來,但是需要注意字符的轉(zhuǎn)意

 如:

insert into table_name (text_col,int_col) value (\'hello world\',1)

 需要轉(zhuǎn)義的字符有:單引號' 雙引號”  反斜杠\  百分號%  下劃線_

 可以連續(xù)使用兩個(gè)單引號轉(zhuǎn)義單引號

八、updata語句

 updata table_name set col__1=vaule_1,col_1=vaule_1 where col=vaule

  where部分可以有任何比較運(yùn)算符

 如:

  table folks
  id  fname  iname  salary
  1  Don  Ho  25000
  2  Don  Corleone 800000
  3  Don  Juan  32000
  4  Don  Johnson  44500
  updata folks set fname='Vito' where id=2
  updata folks set fname='Vito' where fname='Don'
  updata folks set salary=50000 where salary<50000

九、刪除表、數(shù)據(jù)庫

 drop table table_name
 drop data data _name

在php中可以通過mysql_query()函數(shù)使用drop table命令

 在php中刪除數(shù)據(jù)庫需要使用mysql_drop_db()函數(shù)

十、列出數(shù)據(jù)庫中所有可用表(show tables)

 注意:使用該命前必須先選定數(shù)據(jù)庫

 在php中,可以使用mysql_list_tables()得到表中的清單 

十一、查看列的屬性和類型

 show columns from table_name
 show fields from table_name

使用mysql_field_name()、mysql_field_type()、mysql_field_len()可以得到類似信息!

十二、基本的select語句

 要求指出進(jìn)行選擇的表,以及要求的列名稱。若要選定所有的列,可用*代表所有的字段名

 select column_1,column_2,column_3 from table_name

 或者

 select * from table_name

用mysql_query()可向Mysql發(fā)送查詢

十三、where子句

 限制從查詢(select)返回的記錄行

 select * from table_name where user_id = 2

如果要對存儲字符串(char、varchar等類型)的列進(jìn)行比較,就需要在where子句中用單引號把要比較的字符串括起來

 如:

select * from users where city = ‘San Francisco'

 通過向where子句添加and或是or,可以一次比較幾個(gè)運(yùn)算符

 select * from users where userid=1 or city='San Francisco'
 select 8 from users where state='CA' and city='San Francisco'

注意:空值不能和表中的任何運(yùn)算符比較,對于空值,需要使用is null或是is not null謂詞

 select * from users where zip!='1111′ or zip='1111′ or zip is null

如果要找到包含任何值(除空值以外)的所有記錄,可以

 select * from table_name where zip is not null

十四、使用distinct

 當(dāng)使用distinct時(shí),Mysql引擎將刪除有一樣結(jié)果的行。

 select distinct city,state from users where state='CA'

十五、使用between

 使用between可以選擇在某個(gè)范圍內(nèi)的值,between可用于數(shù)字,日期,文本字符串。

 如:

 select * from users where lastchanged between 20000614000000 and 20000614235959
 select * from users where lname between ‘a(chǎn)' and ‘m'

十六、使用in/not in

 若某列可能返回好幾個(gè)可能的值,就可以使用in謂詞

 select * from users where state='RI' or state='NH' or state='VT' or state='MA' or state='ME'

    可改寫為:

select * from users where state in (‘RI','NH','VY','MA','ME') 

 如果要達(dá)到相同的結(jié)果,但結(jié)果集相反,可使用not in 謂詞

 select * from user where state not in (‘RI','NH','VT','MA','ME')

十七、使用like

 如果需要使用通配符,則要使用like

 select * from users where fname like ‘Dan%' %匹配零個(gè)字符
 select * from users where fname like ‘J___' 匹配以J開頭的任意三字母詞

Mysql中l(wèi)ike不區(qū)分字母大小寫

十八、order by

 order by語句可以指定查詢中返回的行的順序,可對任意列類型排序,通過在末尾放置asc或是desc以設(shè)置按升序或是降序排列,如果不設(shè)置,默認(rèn)使用asc 

 select * from users order by lname,fname

可以按照需要根據(jù)任意多的列排序,也可以混合使用asc和desc

 select * from users order by lname asc, fname desc

十九、limit

 limit限制從查詢中返回的行數(shù),可以指定開始的行數(shù)和希望返回的行數(shù)

  得到表中的前5行:

 select * from users limit 0,5
  select * from users order by lname,fname limit 0,5

  得到表的第二個(gè)5行:

  select * from users limit 5,5

二十、group by 與聚合函數(shù)

 使用group by后Mysql就能創(chuàng)建一個(gè)臨時(shí)表,記錄下符合準(zhǔn)則的行與列的所有信息

 count()   計(jì)算每個(gè)集合中的行數(shù)

 select state,count(*) from users group by state

  *號指示應(yīng)該計(jì)算集合中的所有行

 select count(*) from users

  計(jì)算表中所有的行數(shù)

 可以在任何函數(shù)或列名后使用單詞as,然后指定一個(gè)作為別名的名稱。如果需要的列名超過一個(gè)單詞,就要使用單引號把文本字符串括起來

 sum() 返回給定列的數(shù)目
 min() 得到每個(gè)集合中的最小值
 max() 得到每個(gè)集合中的最大值
 avg() 返回集合的品均值
 having

 限制通過group by顯示的行,where子句顯示在group by中使用的行,having子句只限制顯示的行。

二十一、連接表

 在select句的from部分必須列出所有要連接的表,在where部分必須顯示連接所用的字段。

select * from companies,contacts where companies.company_ID=contacts.company_ID

 當(dāng)對一個(gè)字段名的引用不明確時(shí),需要使用table_name.column_name語法指定字段來自于哪個(gè)表

二十二、多表連接

 在select后面添加額外的列,在from子句中添加額外的表,在where子句中添加額外的join參數(shù)–>

相關(guān)文章

  • MySQL慢查詢優(yōu)化解決問題

    MySQL慢查詢優(yōu)化解決問題

    這篇文章主要介紹了MySQL慢查詢優(yōu)化解決問題,MySQL的慢查詢,全名是慢查詢?nèi)罩荆荕ySQL提供的一種日志記錄,用來記錄在MySQL中響應(yīng)時(shí)間超過閥值的語句,下文詳細(xì)介紹慢查詢的調(diào)優(yōu)情況,需要的小伙伴可以參考一下
    2022-03-03
  • MySQL需要關(guān)注的參數(shù)及狀態(tài)變量解讀

    MySQL需要關(guān)注的參數(shù)及狀態(tài)變量解讀

    這篇文章主要介紹了MySQL需要關(guān)注的參數(shù)及狀態(tài)變量解讀,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Mysql如何查看表的索引

    Mysql如何查看表的索引

    這篇文章主要介紹了Mysql如何查看表的索引問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • MySQL 數(shù)據(jù)恢復(fù)的多種方法匯總

    MySQL 數(shù)據(jù)恢復(fù)的多種方法匯總

    日常工作中,總會有因手抖、寫錯(cuò)條件、寫錯(cuò)表名、錯(cuò)連生產(chǎn)庫造成的誤刪庫表和數(shù)據(jù)的事情發(fā)生。但是,如果每次刪庫都跑路的話,怕是再也不好找工作了吧!所以,刪庫跑路不是上上策
    2021-06-06
  • MySQL 聲明變量及存儲過程分析

    MySQL 聲明變量及存儲過程分析

    這篇文章主要介紹了MySQL 聲明變量及存儲過程的相關(guān)內(nèi)容,小編覺得挺不錯(cuò)的,這里分享給大家,需要的朋友可以參考下。
    2017-10-10
  • Mysql分片,大數(shù)據(jù)量時(shí)擴(kuò)容解決方案

    Mysql分片,大數(shù)據(jù)量時(shí)擴(kuò)容解決方案

    這篇文章主要介紹了Mysql分片,大數(shù)據(jù)量時(shí)擴(kuò)容解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • MySQL SHOW PROCESSLIST協(xié)助故障診斷全過程

    MySQL SHOW PROCESSLIST協(xié)助故障診斷全過程

    這篇文章主要給大家介紹了關(guān)于MySQL SHOW PROCESSLIST協(xié)助故障診斷的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • mysql查詢上下級機(jī)構(gòu)的方法實(shí)例

    mysql查詢上下級機(jī)構(gòu)的方法實(shí)例

    大家應(yīng)該都知道表里有上下級機(jī)構(gòu)的,下面這篇文章主要給大家介紹了關(guān)于mysql查詢上下級機(jī)構(gòu)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • Kettle的MySQL數(shù)據(jù)源版本問題及解決

    Kettle的MySQL數(shù)據(jù)源版本問題及解決

    這篇文章主要介紹了Kettle的MySQL數(shù)據(jù)源版本問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Linux安裝mysql并配置外網(wǎng)訪問的實(shí)例

    Linux安裝mysql并配置外網(wǎng)訪問的實(shí)例

    今天小編就為大家分享一篇Linux安裝mysql并配置外網(wǎng)訪問的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05

最新評論