且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

mysql数据库基本命令

更新时间:2022-09-12 22:32:12

一、创建数据库:
create database database_name;
切入数据库:
use 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());

四、删除表、数据库
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()可以得到类似信息!

七、查看参数信息
查看全局参数:show global variables like '%关键字%';
查看局部参数:show variables like '%关键字%';

八、查看数据库bin-log日志信息
[root@localhost][db1]> show master logs;

_name         | File_root@localhostdb1root@localhostdb1_name         | File_root@localhostdb1root@localhostdb1_name         | File_,如需转载请自行联系原作者