一、建立数据库:
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
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
九、删除表、数据库
drop table table_name drop data data _name
在php中能够经由过程mysql_query()函数运用drop table敕令
在php中删除数据库需要运用mysql_drop_db()函数
十、列出数据库中一切可用表(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()能够获得相似信息!
十二、基础的select语句
请求指出举行挑选的表,以及请求的列称号。若要选定一切的列,可用*代表一切的字段名
select column_1,column_2,column_3 from table_name
或许
select * from table_name
用mysql_query()可向Mysql发送查询
十三、where子句
限定从查询(select)返回的纪录行
select * from table_name where user_id = 2
假如要对存储字符串(char、varchar等范例)的列举行比较,就需要在where子句顶用单引号把要比较的字符串括起来
如:
select * from users where city = ‘San Francisco'
经由过程向where子句增加and或是or,能够一次比较几个运算符
select * from users where userid=1 or city='San Francisco' select 8 from users where state='CA' and city='San Francisco'
注重:空值不能和表中的任何运算符比较,关于空值,需要运用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
当运用distinct时,Mysql引擎将删除有一样效果的行。
select distinct city,state from users where state='CA'
十五、运用between
运用between能够挑选在某个范围内的值,between可用于数字,日期,文本字符串。
如:
select * from users where lastchanged between 20000614000000 and 20000614235959 select * from users where lname between ‘a' and ‘m'
十六、运用in/not in
若某列能够返回好几个能够的值,就可以够运用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')
假如要到达雷同的效果,但效果集相反,可运用not in 谓词
select * from user where state not in (‘RI','NH','VT','MA','ME')
十七、运用like
假如需要运用通配符,则要运用like
select * from users where fname like ‘Dan%' %婚配零个字符 select * from users where fname like ‘J___' 婚配以J开首的恣意三字母词
Mysql中like不辨别字母大小写
十八、order by
order by语句能够指定查询中返回的行的递次,可对恣意列范例排序,经由过程在末端安排asc或是desc以设置按升序或是降序分列,假如不设置,默许运用asc
select * from users order by lname,fname
能够依据需要依据恣意多的列排序,也能够夹杂运用asc和desc
select * from users order by lname asc, fname desc
十九、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参数–>
相干引荐:
TP5的数据库操纵
PHP运用ORM举行数据库操纵
MySQL教程--经由过程配置文件衔接数据库操纵详解
以上就是MySQL数据库操纵的基础敕令实例详解的细致内容,更多请关注ki4网别的相干文章!