二、數據庫操作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'@'%';
如果你是在命令提示符下鍵入SQL語句,結尾需要加分號,回車即可,如果你是在MYSQL的一個集成開發環境下操作,將SQL語句鍵入頁面上方的一個框裡,然後按菜單欄上的三角箭頭
update tb1 set dt_start =from_unixtime(unix_timestamp(dt_start)-10*60) where dt_start>'2012-10-1 10:10:10';