暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

汇总MySQL数据库的基本命令(一)

北亚数据安全救援 2018-03-08
244

汇总MySQL数据库的基本命令(一)

创建数据库:

 create database database_name;

 php中创建数据库的两种方法:(mysql_create_db(),mysql_query())

 $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

 删除表、数据库

 drop table table_name

 drop database database_name

 在php中可以通过mysql_query()函数使用drop table命令

 在php中删除数据库需要使用mysql_drop_db()函数

列出数据库中所有可用表(show tables)

 注意:使用该命前必须先选定数据库

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


【联系方式】

地址:北京市海淀区丰慧中路7号新材料创业大厦B座205

联系方式:4006505646

邮箱:songguojian@frombyte.com

文章转载自北亚数据安全救援,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论