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