首页 » PHP教程 » phpshowfieldsfrom技巧_MYSQL运用初步流程介绍

phpshowfieldsfrom技巧_MYSQL运用初步流程介绍

访客 2024-11-22 0

扫一扫用手机浏览

文章目录 [+]

一、创建数据库

create database database_name;

phpshowfieldsfrom技巧_MYSQL运用初步流程介绍

PHP中创建数据库的两种方法:(MYSQL_create_db(),MYSQL_query())

phpshowfieldsfrom技巧_MYSQL运用初步流程介绍
(图片来自网络侵删)

$CONN = MYSQL_CONNect(“localhost”,”username”,”password”) or

die ( “could not CONNect to localhost”);

1.

MYSQL_create_db(“database_name”) or

die (“could not create database”);

2.

$string = “create database database_name”;

MYSQL_query( $string) or

die (MYSQL_error());

二、选天命据库

在创建表之前,必须要选定要创建的表所在的数据库

选天命据库:

通过命令行客户端:use database_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 database”);

三、创建表

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 database”);

$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 database database_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参数–>

标签:

相关文章