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()得到表中的清单

相关内容