mysql資料庫基本命令

2021-09-20 19:12:25 字數 2313 閱讀 7608

一、建立資料庫:

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@localhost

db1root@localhost

db1_name | file_

root@localhost

db1root@localhost

db1_name | file_

mysql資料庫的基本命令

登入 mysql uroot pmysql 退出quit 或者 exit 或者ctrl d 檢視版本 select version 檢視當前時間 select now 資料庫的操作 檢視所有資料庫 show databases 檢視當前使用的資料庫 select databases 使用資料庫 us...

資料庫表基本命令mysql

我們都要笑的燦爛如花,縱然摻雜著萬般心碎.create database 資料庫名 create database db 1.直接建立資料庫 create database db character set gbk 2.建立時設定編碼格式 use 資料庫名 use db 先使用資料庫才能建立表 cr...

資料庫基本命令

增刪改查 select 從資料庫中提取資料 update 更新資料庫中的資料 delete 從資料庫中刪除資料 insert into 向資料庫中插入新資料 唯一性查詢 sql select distinct 插入語句 insert into websites name,url,alexa,coun...