資料庫命令列操作

2021-07-08 19:15:05 字數 3566 閱讀 3411

一、從命令列登入mysql資料庫伺服器

1、登入使用預設3306埠的mysql

/usr/local/mysql/bin/mysql -u root -p

2、通過tcp連線管理不同埠的多個mysql(注意:mysql4.1以上版本才有此項功能)

/usr/local/mysql/bin/mysql -u root -p --protocol=tcp --host=localhost --port=3307

3、通過socket套接字管理不同埠的多個mysql

/usr/local/mysql/bin/mysql -u root -p --socket=/tmp/mysql3307.sock

4、通過埠和ip管理不同埠的多個mysql

/usr/local/mysql/bin/mysql -u root -p -p 3306 -h 127.0.0.1

二、資料庫操作sql語句

1、顯示伺服器上當前存在什麼資料庫

show databases;

2、建立名稱為rewin的資料庫

create database rewin;

3、刪除名稱為rewin的資料庫

drop database rewin;

4、選擇rewin資料庫

use rewin;

三、表操作sql語句(登入後必須用以上的use命令選擇乙個資料庫,再進行表操作)

1、顯示當前資料庫中存在什麼表

show tables;

2、建立資料庫表zhangyan

在mysql>後貼上以下sql語句

create table `zhangyan` (

`id` int( 5 ) unsigned not null auto_increment ,

`username` varchar( 20 ) not null ,

`password` char( 32 ) not null ,

`time` datetime not null ,

`number` float( 10 ) not null ,

`content` text not null , primary key ( `id` )

) ;3、檢視zhangyan表結構

describe zhangyan;

4、從表中檢索資訊

4.1、從zhangyan表中檢索所有記錄

select * from zhangyan;

4.2、從zhangyan表中檢索特定的行 (欄位username等於abc,欄位number等於1,按欄位id降序排列)

select * from zhangyan where username = abc and number=1 order by id desc;

4.3、從zhangyan表中檢索指定的字段

select username, password from zhangyan;

4.4、從zhangyan表中檢索出唯一的不重覆記錄

select distinct username from zhangyan;

5、插入資訊到zhangyan表

insert into zhangyan (id, username, password, time, number, content) values (, abc, 123456,

2007-08-06 14:32:12, 23.41, hello world);

6、更新zhangyan表中的指定資訊

update zhangyan set content = hello china where username = abc;

7、刪除zhangyan表中的指定資訊

delete from zhangyan where id = 1;

8、清空zhangyan表

delete from zhangyan;

9、刪除zhangyan表

drop table zhangyan;

10、更改表結構 (將zhangyan表username欄位的字段型別改為char(25))

alter table zhangyan change username username char(25);

11、將當前目錄下的mysql.sql匯入資料庫

source ./mysql.sql;

四、資料庫許可權操作sql語句

1、建立乙個具有root許可權,可從任何ip登入的使用者sina,密碼為zhangyan

grant all privileges on *.* to

sina@%

identified by zhangyan;

2、建立乙個具有"資料操作"、"結構操作"許可權,只能從192.168.1.***登入的使用者sina,密碼為zhangyan

grant select , insert , update , delete , file , create , drop , index , alter , create

temporary tables , create view , show view , create routine, alter routine, execute on

*.* to

[email protected].%

identified by zhangyan;

3、建立乙個只擁有"資料操作"許可權,只能從192.168.1.24登入,只能操作rewin資料庫的zhangyan表的使用者

sina,密碼為zhangyan

grant select , insert , update , delete on  rewin.zhangyan to

[email protected]

identified by

zhangyan;

4、建立乙個擁有"資料操作"、"結構操作"許可權,可從任何ip登入,只能操作rewin資料庫的使用者sina,密碼為

zhangyan

grant select , insert , update , delete , create , drop , index , alter , create temporary

tables , create view , show view , create routine, alter routine, execute on rewin.* to

sina@%

identified by zhangyan;

5、刪除使用者

drop user

sina@%;

6.mysql中將字串aaa批量替換為bbb的sql語句

update 表名 set 欄位名 = replace (欄位名, aaa, bbb);

7.修復損壞的表      

①、用root帳號從命令列登入mysql:        mysql -u root -p

②、輸入root帳號的密碼。

③、選定資料庫名(本例中的資料庫名為student):   use student;

④、修復損壞的表(本例中要修復的表為smis_user_student):   repair table smis_user_student;udent;

基礎資料庫命令列命令

刪除操作 運算元據 exit 回車 show databases use 資料庫名 create database 庫名 show tables use 庫名 create table 表名 欄位名字,資料型別,修飾,describe 表名 desc 表名 select from 表名 drop d...

MySQL 資料庫命令列工具

mysql 包括乙個伺服器程序管理資料庫,以及一系列訪問資料庫和建立應用的工具 mysql 在 mysql 中執行 sql 查詢,也可以執行在檔案中儲存的sql命令 mysqlaccess 管理使用者 mysqladmin 管理資料庫伺服器,包括資料庫的建立和移除 mysqld 實際的 mysql ...

MySQL 資料庫命令列工具

mysql 包括乙個伺服器程序管理資料庫,以及一系列訪問資料庫和建立應用的工具 mysql 在 mysql 中執行 sql 查詢,也可以執行在檔案中儲存的sql命令 mysqlaccess 管理使用者 mysqladmin 管理資料庫伺服器,包括資料庫的建立和移除 mysqld 實際的 mysql ...