mysql資料庫常用文法

來源:互聯網
上載者:User

標籤:mysql資料庫、mysql文法、mysql常用命令、mysql增刪改查

1)登入mysql資料庫。

mysql –uroot –poldboy123

mysql

2) 查看當前登入的使用者。

 selectuser();

3) 建立資料庫oldboy,並查看已建庫完整語句。

create database oldboy;

show databases;

show create database oldboy;

4)建立使用者oldboy,使之可以管理資料庫oldboy。

create user [email protected]‘localhost‘ identified by‘oldboy123‘;

grant all on oldboy.* to [email protected]‘localhost‘;

grant all on oldboy.* [email protected]‘localhost‘ indetified by oldboy123;

5) 查看建立的使用者oldboy擁有哪些許可權。

show grants for [email protected]‘localhost‘;

6) 查看當前資料庫裡有哪些使用者。

select user,host from mysql.user;

7) 進入oldboy資料庫。

Use oldboy;

8) 查看當前所在的資料庫。

selectdatabase();

9) 建立一張表test,欄位id和name varchar(16)。

create table test( id int(4) not null , namevarchar(16) not null);

10) 查看建表結構及表結構的SQL語句。

desc test;

show full columns from test;

11) 插入一條資料“1,oldboy”

insertinto test(id,name) values(1,‘oldboy‘);

select * from test;

12) 再批量插入2行資料 “2,老男孩”,“3,oldboyedu”。

insert into test(id,name) values(2,‘老男孩‘),(3,‘oldboyedu‘);

select * from test; 

13) 查詢名字為oldboy的記錄。

select * from test where name=‘oldboy‘;

14) 把資料id等於1的名字oldboy更改為oldgirl。

update test set name=‘oldgirl‘ where id=1;

select * from test;

15) 在欄位name前插入age欄位,類型tinyint(2)。

alter table test add age tinyint(2) after id;

desc test;

16) 不退出Database Backupoldboy資料庫。

system mysqldump -uroot -poldboy123 -B oldboy >/opt/oldboy1.sql;

17) 刪除test表中的所有資料,並查看。

delete  fromtest;

truncate test;

18) 刪除表test和oldboy資料庫並查看

表:

show tables ;

drop table test;

庫:

drop database oldboy;

show databases;

19) 不退出資料庫恢複以上刪除的資料。

source /opt/oldboy1.sql

20) 在把id列設定為主鍵,在Name欄位上建立普通索引。

主鍵:

create table test (

id int(4) not null , -- 自增ID

name char(16) not null,

primary key (id) );

普通鍵:

alter table test add index intex_name(name);

21) 在欄位name後插入手機號欄位(shouji),類型char(11)。

alter table test add shouji char(11) after name;

desc test;

22) 所有欄位上插入2條記錄(自行設定資料)

 insert into test(id,name,shouji)values(1,‘aige‘,‘13555555‘),(2,‘oldboy‘,‘1388888888‘);

insert into test(id,name,shouji)values(3,‘oldboy‘,‘135555555‘);

select * from test;

23) 刪除Name列的索引。

drop index intex_name on test;

24) 查詢手機號以135開頭的,名字為oldboy的記錄(提前插入)。

select * from test  where shouji like ‘135%‘ and name like‘oldboy‘;

25) 收回oldboy使用者的select許可權。

revoke select on oldboy.* from [email protected]‘localhost‘;

shell終端執行  使用-e參數調用mysql內部命令

mysql -uroot -poldboy123 -e "show grants [email protected]‘localhost‘" | grep -i select

26) 刪除oldboy使用者。

select user,host from mysql.user;

drop user [email protected]‘localhost‘;

select user,host from mysql.user;

27) 刪除oldboy資料庫。

drop database oldboy;

28) 使用mysqladmin關閉資料庫。

mysqladmin -uroot -poldboy123 shutdown

ps -ef | grep mysql


本文出自 “為人民服務” 部落格,請務必保留此出處http://junhun.blog.51cto.com/12852949/1926334

mysql資料庫常用文法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.