首页 > 代码库 > mysql的简单操作

mysql的简单操作

创建数据库并设定字符集:

CREATE  DATABASE hidb CHARACTER SET ‘utf8’;

使用数据库:

use hidb;

删除数据库:

DROP DATABASE hidb;

 

SHOW DATABASES LIKE ‘%db’

创建数据库表:

CREATE TABLE TBL2 (id SMALLINT UNSIGND NOT NULL AUTO INCREMENT UNIQUE KEY,name

HELP DESC

DESC tbl2

增加数据库表的字段:

ALTER TABLE tbl3 ADD gender ENUM(‘F’,’M’) after id;

修改字段:

ALTER TABLE tbl3 CHANGE id stuid SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;                     修改字段名

ALTER TABLE students MODIFIY birthdate DATE;   修改字段的数值类型

删除字段:

ALTER TABLE tbl3 DROP gender;

添加索引:

ALTER TABLE tbl3 ADD INDEX name(name);

CREATE INDEX user ON tbl8(User);

CREATE UNIQUE INDEX host ON tbl8(Host,User);

删除索引:

ALTER TABLE tbl3 DROP INDEX id;

 

查询索引:

SHOW INDEXES FROM tbl3;

查询表:

SHOW TABLES;

查询表结构:

DESC students;

查询数据库:

SHOW DATABASES;

 

 

复制表结构;

CREATE TABLE tbl7 LIKE mysql.user

CREATE TABLE tbl8 SELECT host,user,password FROM mysql.user;

 

表中插入数据:

INSERT INTO students VALUES (1,’Yang Guo’,’M’,’899-04-06’,3);

INSERT INTO students(name,gender) VALUES(‘Guo Jing’,’M’),(‘Ding Dian’,’M’);

REPLACE INTO students VALUES ();  有此行则代替原有行,没有就插入一行数据;

 

SELECT * FROM students WHERE classid IS |NOT IS NULL;

SELECT * FROM students WHERE classid IN (1,2,3);

SELECT * FROM students WHERE name LIKE ‘D%’;

SELECT * FROM students WHERE NOT name LIKE ‘D%’;  或许是name NOT LIKE

SELECT * FROM students WHERE name RLIKE ‘^D.*$’;        D开头的名字

SELECT * FROM students ORDER BY name DESC; 降序排序

 

删除100行:

DELETE FROM students ORDER BY age DESC LIMIT 100;

 

创建用户:

CREATE USER ‘tom’@’172.16.%.%’ IDENTIFIED BY ‘magedu’;   创建tom可以在172.16的网络登录;

 

删除用户:

DROP USER ‘tom’@’172.16.%.%’;

给用户授权,也能创建用户:

‘tom’@’172.16.%.%’表示:允许tom用哪些IP的客户端登录

GRANT ALL ON hidb.* TO ‘tom’@’172.16.%.%’ IDENTIFIED BY ‘magedu’;

 

回收权限:     mydb数据库的tbl1表

REVOKE DELETE,UPDATE ON mydb.tbl1 FROM ‘tom’@172.16.%.%’;

 

查看权限:

SHOW GRANTS;

SHOW GRANTS FOR ‘tom’@’172.16.%.%’;

 

刷新授权表:

FLUSH PRIVILEGES;

 

MySQL的-e 使用:

 

]# mysql –utom –h172.16.0.67 –pmagedu ‘INSERT INTO hidb.students (name,gender,age) VALUES (‘tom’,’M’,18);

mysql的简单操作