一、常用命令
列出數(shù)據(jù)庫(kù):show databases;
選擇數(shù)據(jù)庫(kù):use databaseName;
列出表格:show tables;
建庫(kù)create database 庫(kù)名;
建表create table 表名;
顯示數(shù)據(jù)表的結(jié)構(gòu):desc 表名;
刪庫(kù) drop database 庫(kù)名;
刪表 drop table 表名;
將表中記錄清空:delete from 表名;
顯示表中的記錄:select * from 表名;
二、連接MYSQL。
格式: mysql -h主機(jī)地址 -u用戶名 -p用戶密碼
1、例1:連接到本機(jī)上的MYSQL。
首先在打開DOS窗口,鍵入命令mysql -uroot -p,回車后提示你輸密碼。
2、例2:連接到遠(yuǎn)程主機(jī)上的MYSQL。假設(shè)遠(yuǎn)程主機(jī)的IP為:192.168.1.5,用戶名為root,密碼為root。則鍵入以下命令: mysql -h192.168.1.5 -uroot -proot
3、退出MYSQL命令: exit (回車)
三、其它命令:
查詢時(shí)間:select now();
查詢數(shù)據(jù)庫(kù)版本:select version();
查詢當(dāng)前用戶:select user();
查詢當(dāng)前使用的數(shù)據(jù)庫(kù):select database();
匹配字符:可以用通配符_代表任何一個(gè)字符,%代表任何字符串;
增加一個(gè)字段:alter table tabelName add column fieldName dateType;
增加多個(gè)字段:alter table tabelName add column fieldName1 dateType,add columns fieldName2 dateType;
修改密碼:mysqladmin -u用戶名 -p舊密碼 password新密碼
以下轉(zhuǎn)載:
增加新用戶:
格式:grant select on 數(shù)據(jù)庫(kù).* to 用戶名@登錄主機(jī) identified by "密碼"
例1、增加一個(gè)用戶test1密碼為abc,讓他可以在任何主機(jī)上登錄,并對(duì)所有數(shù)據(jù)庫(kù)有查詢、插入、修改、刪除的權(quán)限。首先用以root用戶連入MYSQL,然后鍵入以下命令:
grant select,insert,update,delete on *.* to test1@"%" Identified by "abc";
但例1增加的用戶是十分危險(xiǎn)的,你想如某個(gè)人知道test1的密碼,那么他就可以在internet上的任何一臺(tái)電腦上登錄你的mysql數(shù)據(jù)庫(kù)并對(duì)你的數(shù)據(jù)可以為所欲為了,解決辦法見例2。
例2、增加一個(gè)用戶test2密碼為abc,讓他只可以在localhost上登錄,并可以對(duì)數(shù)據(jù)庫(kù)mydb進(jìn)行查詢、插入、修改、刪除的操作(localhost指本地主機(jī),即MYSQL數(shù)據(jù)庫(kù)所在的那臺(tái)主機(jī)),這樣用戶即使用知道test2的密碼,他也無(wú)法從internet上直接訪問數(shù)據(jù)庫(kù),只能通過MYSQL主機(jī)上的web頁(yè)來(lái)訪問了。
grant select,insert,update,delete on mydb.* to test2@localhost identified by "abc";
例3.如果你不想test2有密碼,可以再打一個(gè)命令將密碼消掉。
grant select,insert,update,delete on mydb.* to test2@localhost identified by "";
例4.增加一個(gè)管理員帳戶:grant all on *.* to user@localhost identified by "password";
其它命令集:
從已經(jīng)有的表中復(fù)制表的結(jié)構(gòu)create table table2 select * from table1 where 1<>1;
復(fù)制表create table table2 select * from table1;
對(duì)表重新命名alter table table1 rename as table2;
修改列的類型:
alter table table1 modify id int unsigned;//修改列id的類型為int unsigned
alter table table1 change id sid int unsigned;//修改列id的名字為sid,而且把屬性修改為int unsigned
創(chuàng)建索引:
alter table table1 add index ind_id (id);
create index ind_id on table1 (id);
create unique index ind_id on table1 (id);//建立唯一性索引
刪除索引:
drop index idx_id on table1;
alter table table1 drop index ind_id;
聯(lián)合字符或者多個(gè)列(將列id與":"和列name和"="連接):
select concat(id,':',name,'=') from students。
limit(選出10到20條)<第一個(gè)記錄集的編號(hào)是0>:
select * from students order by id limit 9,10。
MySQL不支持的功能:事務(wù),視圖,外鍵和引用完整性,存儲(chǔ)過程和觸發(fā)器。
posted @ 2009-03-26 10:02 斷點(diǎn) 閱讀(79) | 評(píng)論 (0)