MySql 實用 資料庫 命令

2021-04-29 00:57:48 字數 2406 閱讀 7250

1、mysql常用命令

create database name; 建立資料庫

use databasename; 選擇資料庫

drop database name 直接刪除資料庫,不提醒

show tables; 顯示表

describe tablename; 表的詳細描述

select 中加上distinct去除重複字段

mysqladmin drop databasename 刪除資料庫前,有提示。

顯示當前mysql版本和當前日期

select version(),current_date;

2、修改mysql中root的密碼:

shell>mysql -u root -p

mysql> update user set password=password(」xueok654123″) where user=』root』;

mysql> flush privileges //重新整理資料庫

mysql>use dbname; 開啟資料庫:

mysql>show databases; 顯示所有資料庫

mysql>show tables; 顯示資料庫mysql中所有的表:先use mysql;然後

mysql>describe user; 顯示表mysql資料庫中user表的列資訊);

3、grant

建立乙個可以從任何地方連線伺服器的乙個完全的超級使用者,但是必須使用乙個口令something做這個

mysql> grant all privileges on *.* to user@localhost identified by 』something』 with

增加新使用者

格式:grant select on 資料庫.* to 使用者名稱@登入主機 identified by 「密碼」

grant all privileges on *.* to monty@localhost identified by 』something』 with grant option;

grant all privileges on *.* to monty@」%」 identified by 』something』 with grant option;

刪除授權:

mysql> revoke all privileges on *.* from root@」%」;

mysql> delete from user where user=」root」 and host=」%」;

mysql> flush privileges;

建立乙個使用者custom在特定客戶端it363.com登入,可訪問特定資料庫fangchandb

mysql >grant select, insert, update, delete, create,drop on fangchandb.* to custom@ it363.com identified by 『 passwd』

mysql > alter table t1 rename t2;

4、mysqldump

備份資料庫

shell> mysqldump -h host -u root -p dbname >dbname_backup.sql

恢復資料庫

shell> mysqladmin -h myhost -u root -p create dbname

shell> mysqldump -h host -u root -p dbname < dbname_backup.sql

如果只想卸出建表指令,則命令如下:

shell> mysqladmin -u root -p -d databasename > a.sql

如果只想卸出插入資料的sql命令,而不需要建表命令,則命令如下:

shell> mysqladmin -u root -p -t databasename > a.sql

那麼如果我只想要資料,而不想要什麼sql命令時,應該如何操作呢?

mysqldump -t./ phptest driver

其中,只有指定了-t引數才可以卸出純文字檔案,表示卸出資料的目錄,./表示當前目錄,即與mysqldump同一目錄。如果不指定driver 表,則將卸出整個資料庫的資料。每個表會生成兩個檔案,乙個為.sql檔案,包含建表執行。另乙個為.txt檔案,只包含資料,且沒有sql指令。

5、可將查詢儲存在乙個檔案中並告訴mysql從檔案中讀取查詢而不是等待鍵盤輸入。可利用外殼程式鍵入重定向實用程式來完成這項工作。例如,如果在檔案my_file.sql 中存放有查

詢,可如下執行這些查詢:

例如,如果您想將建表語句提前寫在sql.txt中:

mysql > mysql -h myhost -u root -p database < sql.txt

Mysql資料庫命令

create database mydata use mydata create table dept deptno int primary key,dname varchar 14 loc varchar 13 create table emp empno int primary key,enam...

Mysql資料庫命令

mysql命令集錦 測試環境 mysql 5.0.45 注 可以在mysql中通過mysql select version 來檢視資料庫版本 一 連線mysql。格式 mysql h主機位址 u使用者名稱 p使用者密碼 1 連線到本機上的mysql。首先開啟dos視窗,然後進入目錄mysql bin...

mysql資料庫命令

一修改表名 alter table 舊表名 rename to 新錶名二 新增字段 alter table 表名 add 欄位名 型別 屬性 三 修改字段 alter table 表名 change 舊的字段 新的字段4.刪除字段 alter table 表名 drop 欄位名 5 新增主鍵 alt...