我们都知道MySQL作为数据库管理系统的优势是无可比拟的,MySQL应用及其广泛,可以在几乎所有平台上运行UNIX, Linux,Windows,小到你可以安装服务器在自己的pc中,而且可靠,可拓展,运行速度飞快。今天,小编整理总结了MySQL常用语句,快来看看吧。
一、数据库的命令查看所有的数据库:show databases;
创建一个叫test的数据库:create database test;
删除一个叫test的数据库:drop database test;
选中库 ,在建表之前必须要选择数据库:use test;
在选中的数据库之中查看所有的表:show tables;
表名 (字段1 类型, 字段2 类型):create table ;
表名;查看所在的表的字段:desc ;
表名; 删除表:drop table ;
库名;查看创建库的详细信息:show create database;
表名; 查看创建表的详细信息:show create table ;
二、数据表的命令修改表名:ALTER TABLE 表名1 RENAME TO 表名2;
添加列:ALTER TABLE 表名 ADD COLUMN 列名 列类型(如:VARCHAR(30));
删除列:ALTER TABLE 表名 DROP COLUMN 列名;
选择:SELECT * FROM 表名 WHERE 范围;
插入:INSERT INTO 表名(字段名1,字段名2) VALUES(字段值1,字段值2);
删除:DELETE FROM 表名 WHERE 范围;
更新:UPDATE 表名 SET 字段名=字段值 WHERE 范围;
查找:SELECT * FROM 表名 WHERE 字段名 LIKE ’% 字段值 %’ ;
排序:SELECT * FROM 表名 ORDER BY 字段名1,字段名2 [DESC];
总数:SELECT COUNT AS totalcount FROM 表名;
求和:SELECT SUM(字段值) AS sumvalue FROM 表名;
平均:SELECT AVG(字段名) AS avgvalue FROM 表名;
最大:SELECT MAX(字段名) AS maxvalue FROM 表名;
最小:SELECT MIN(字段名) AS minvalue FROM 表名;
三、数据命令增加数据
INSERT INTO n VALUES (1, 'tom', '23'), (2, 'john', '22');
INSERT INTO n SELECT * FROM n; # 把数据复制一遍重新插入
删除数据
DELETE FROM n WHERE id = 2;
更改数据
UPDATE n SET name = 'tom' WHERE id = 2;
数据查找
SELECT * FROM n WHERE name LIKE '%h%';
数据排序(反序)
SELECT * FROM n ORDER BY name, id DESC ;
四、多表联合查询1.内连接
隐式内连接 select username,name from user,goods where user,gid=gods,gid;
显示内连接
select username,from user inner join goods on user.gid=goods.gid;
select * from user left join goods on user.gid=goods.gid;
2.外链接
左连接 包含所有的左边表中的记录以及右边表中没有和他匹配的记录
右连接
select * from user where gid in(select gid from goods);
select * from user right jOin goods on user.gid=goods.gid;
子嵌套查询
数据联合查询
select * from user left join goods on user.gid=goods.gid union select * from user right join goods on user.gid=goods.gid;
两个表同时更新
update user u, goods g set u.gid=12,g.price=1 where u.id=2 and u.gid=g.gid;
五、视图命令创建视图
CREATE VIEW v AS SELECT id, name FROM n;
CREATE VIEW v(id, name) AS SELECT id, name FROM n;
查看视图(与表操作类似)
SELECT * FROM v;
DESC v;
查看创建视图语句
SHOW CREATE VIEW v;
更改视图
CREATE OR REPLACE VIEW v AS SELECT name, age FROM n;
ALTER VIEW v AS SELECT name FROM n ;
删除视图
DROP VIEW IF EXISTS v;
五、用户命令增加用户
CREATE USER
'test'@'localhost'
IDENTIFIED BY 'test';
INSERT INTO mysql.user(Host, User, Password) VALUES ('localhost', 'test', Password('test')); # 在用户表中插入用户信息,直接操作User表不推荐
删除用户
DROP USER
'test'@'localhost'
;
DELETE FROM mysql.user WHERE User='test' AND Host='localhost';
FLUSH PRIVILEGES ;
更改用户密码
SET PASSWORD FOR
'test'@'localhost'
= PASSWORD('test');
UPDATE mysql.user SET Password=Password('t') WHERE User='test' AND Host='localhost';
FLUSH PRIVILEGES ;
用户授权
GRANT ALL PRIVILEGES ON *.* TO
test@localhost
IDENTIFIED BY 'test';
授予用'test'密码登陆成功的
test@localhost
用户操作所有数据库的所有表的所有的权限
FLUSH PRIVILEGES ; # 刷新系统权限表,使授予权限生效
撤销用户授权
REVOKE DELETE ON *.* FROM
'test'@'localhost'
; # 取消该用户的删除权限
六:事物命令mysql> use RUNOOB;
Database changed
mysql> CREATE TABLE runoob_transaction_test( id int(5)) engine=innodb;
Query OK, 0 rows affected (0.04 sec)
mysql> select * from runoob_transaction_test;
Empty set (0.01 sec)
mysql> begin; # 开始事务
Query OK, 0 rows affected (0.00 sec)
mysql> insert into runoob_transaction_test value(5);
Query OK, 1 rows affected (0.01 sec)
mysql> insert into runoob_transaction_test value(6);
Query OK, 1 rows affected (0.00 sec)
mysql> commit; # 提交事务
Query OK, 0 rows affected (0.01 sec)
mysql> select * from runoob_transaction_test;
+------+
| id |
+------+
| 5 |
| 6 |
+------+
2 rows in set (0.01 sec)
mysql> begin; # 开始事务
Query OK, 0 rows affected (0.00 sec)
mysql> insert into runoob_transaction_test values(7);
Query OK, 1 rows affected (0.00 sec)
mysql> rollback; # 回滚
Query OK, 0 rows affected (0.00 sec)
mysql> select * from runoob_transaction_test; # 因为回滚所以数据没有插入
+------+
| id |
+------+
| 5 | | 6 |
+------+
2 rows in set (0.01 sec)
七、备份与还原数据库备份
mysqldump -u root -p db_name > file.sql
mysqldump -u root -p db_name table_name > file.sql
数据库还原
mysql -u root -p < C:\file.sql