mysql一些常用語句 mysql一些常用語句

2021-10-19 18:50:31 字數 3265 閱讀 4845

一、從命令列登入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語句,儲存引擎為myisam,欄位id為主鍵、唯一索引。

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` )

) engine = myisam ;

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表中檢索指定的字段:username和password

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' with grant option;

flush privileges;

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 'sina'@'192.168.1.%' identified by 'zhangyan';

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

grant select , insert , update , delete on rewin.zhangyan to 'sina'@'192.168.1.24' 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'@'%';

Mysql 一些常用語句

mysqldump u root p anclgk tmp.sql 匯出為sql mysql uroot p123456 tmp.sql 匯入 mysql use abc 使用已建立的資料庫 mysql set names utf8 設定編碼 mysql source home abc abc.sq...

mysql常用語句 MySQL常用語句

create table student id int primary key auto increment comment 學號 name varchar 200 comment 姓名 age int comment 年齡 comment 學生資訊 修改表注釋 alter table studen...

matlab小白一些常用語句

1 clear清除workspace clc清除命令視窗 clf清除圖形視窗 2 load 載入資料檔案 3 meshgrid函式生成網格矩陣,可用於將向量轉變為矩陣。生成二維網格,用法為 x y meshgrid a b a 和b是一維陣列,如a 1 2 3 b 2 3 4 則生成的 x 和 y ...