本文主要介紹了MySQL使用初步之MySQL資料庫的基本指令,需要的朋友可以參考下,希望能幫助到大家。
一、建立資料庫:
create data data _name;
php中建立資料庫的兩種方法:(mysql_create_db(),mysql_query())
$conn = mysql_connect(“localhost”,”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());
二、選取資料庫
在建立表格之前,必須要選取要建立的表格所在的資料庫
選取資料庫:
透過命令列用戶端:
use data _name
透過
php: mysql_select_db()
$conn = mysql_connect(“localhost”,”username”,”password”) or die ( “could not connect to localhost”); mysql_select_db(“test”,$conn) or die (“could not select data ”);
三、建立表格
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) )
在命令列客戶端需要鍵入整個命令
在php中使用,mysql_query()函數
如:
$conn = mysql_connect(“localhost”,”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());
四、建立索引
index index_name(indexed_column)
五、表的類型
ISAM MyISAM BDB Heap
# 聲明表類型的語法:
create table table_name type=table_type (col_name column attribute);
預設使用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 把新欄位放在已經存在的欄位的後面
例如:
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
注意:必須重新定義列的屬性! ! !
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,……)
如果要存入字串,則需要使用單引號「'」將字串括起來,但是需要注意字元的轉意
如:
insert into table_name (text_col,int_col) value (\'hello world\',1)
需要轉義的字有:單引號' 雙引號」 反斜線\ 百分號% 底線_
可以連續使用兩下位子\ 百分號% 底線_
個單引號轉義單引號
八、updata語句
updata table_name set col__1=vaule_1,col_1=vaule_1 where col=vaule
where部分可以有任何比較運算子
如:
# table folks
id fname iname salary
1 Don Ho 25000
2 Don Corleone 800000
3 Don Juan 32000
N1 ' where id=2
updata folks set fname='Vito' where fname='Don'
drop table table_name drop data data _name#在php可以透過mysql_query()函數使用drop table指令 在php中刪除資料庫需要使用mysql_drop_db()函數#C、列出資料庫中所有可用資料表(show tables) 注意:使用該命前必須先選取資料庫
在php中,可以使用mysql_list_tables()得到表格中的清單
show columns from table_name show fields from table_name使用mysql_field_name()、mysql_field_type()、mysql_field_len()可以得到類似資訊! ,以及要求的列名稱。 ##十三、where子句 限制從查詢(select)傳回的記錄行
select column_1,column_2,column_3 from table_name如果要將儲存字串(char、varchar等型別)的資料列進行比較,就需要在where子句中用單引號把要比較的字串括起來 如:
select * from table_name透過在where子句中加入and或是or,可以一次比較幾個運算符
select * from table_name where user_id = 2注意:空值不能和表中的任何運算符比較,對於空值,需要使用is null或是is not null謂詞
select * from users where city = ‘San Francisco'如果要找到包含任何值(除空值以外)的所有記錄,可以
select * from users where userid=1 or city='San Francisco' select 8 from users where state='CA' and city='San Francisco'十四、使用distinct 當使用distinct時,Mysql引擎會刪除有相同結果的行。
select * from users where zip!='1111′ or zip='1111′ or zip is null十五、使用between 使用between可以選擇在某個範圍內的值,between可用於數字,日期,文字字串。 如:
select * from table_name where zip is not null
十六、使用in/not in
若某一列可能回傳好幾個可能的值,就可以使用in謂詞
select distinct city,state from users where state='CA'
可改寫為:
select * from users where lastchanged between 20000614000000 and 20000614235959 select * from users where lname between ‘a' and ‘m'
若要達到相同的結果,但結果集相反,可使用not in 謂詞
select * from users where state='RI' or state='NH' or state='VT' or state='MA' or state='ME'十七、使用like # 如果需要使用萬用符,則要使用like
select * from users where state in (‘RI','NH','VY','MA','ME')Mysql中like不區分字母大小寫11、order by order by語句可以指定在查詢中傳回的行的順序,可對任意列類型排序,透過在末尾放置asc或是desc以設置按升序或是降序排列,如果不設置,默認使用asc
select * from user where state not in (‘RI','NH','VT','MA','ME')
可以按照需要根據任意多的列排序,也可以混合使用asc和desc
select * from users where fname like ‘Dan%' %匹配零个字符 select * from users where fname like ‘J___' 匹配以J开头的任意三字母词
十九、limit
limit限制從查詢中傳回的行數,可以指定開始的行數和希望傳回的行數
得到表格中的前5行:
select * from users limit 0,5 select * from users order by lname,fname limit 0,5
得到表的第二个5行:
select * from users limit 5,5
二十、group by 与聚合函数
使用group by后Mysql就能创建一个临时表,记录下符合准则的行与列的所有信息
count() 计算每个集合中的行数
select state,count(*) from users group by state
*号指示应该计算集合中的所有行
select count(*) from users
计算表中所有的行数
可以在任何函数或列名后使用单词as,然后指定一个作为别名的名称。如果需要的列名超过一个单词,就要使用单引号把文本字符串括起来
sum() 返回给定列的数目
min() 得到每个集合中的最小值
max() 得到每个集合中的最大值
avg() 返回集合的品均值
having
限制通过group by显示的行,where子句显示在group by中使用的行,having子句只限制显示的行。
二十一、连接表
在select句的from部分必须列出所有要连接的表,在where部分必须显示连接所用的字段。
select * from companies,contacts where companies.company_ID=contacts.company_ID
当对一个字段名的引用不明确时,需要使用table_name.column_name语法指定字段来自于哪个表
二十二、多表连接
在select后面添加额外的列,在from子句中添加额外的表,在where子句中添加额外的join参数–>
相关推荐:
以上是MySQL資料庫操作的基本指令實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

WebStorm Mac版
好用的JavaScript開發工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Atom編輯器mac版下載
最受歡迎的的開源編輯器