35个常用的MYSQL基本操作命令

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了35个常用的MYSQL基本操作命令相关的知识,希望对你有一定的参考价值。

  

1、创建数据库CREATE DATABASE _databasename

--创建一个mytest的数据库
CREATE DATABASE mytest;
--创建一个mytest的数据库并设置默认字符编码
CREATE DATABASE mytest DEFAULT CHARACTER SET "utf8";

2、查看已创建的所有数据库SHOW DATABASES

SHOW DATABASES;

3、使用数据库USE _databasename

--使用mytest数据库
USE mytest;

4、创建数据表CREATE TABLE _tablename

--创建一个名为user的数据表
CREATE TABLE ‘user‘(
id SMALLINT,
username VARCHAR(20),
age TINYINT
);

5、查看当前数据库中所有的数据表SHOW TABLES

SHOW TABLES;

6、查看当前数据表中的结构DESC _tablename

--查看hurujiuser表
DESC hurujiuser;
技术分享
 


7、查看创建数据库时的描述SHOW CREATE TABLE _tablename

--查看hurujiuser
SHOW CREATE TABLE hurujiuser;

8、插入记录INSERT tablename VALUES

--向user表中插入一条记录
INSERT user VALUES("1","huruji","18");
--向user表中插入一条记录只包含名字
INSERT user(username) VALUES("xie");
--向user表中同时插入多条记录
INSERT user VALUES(2,,"xie",10),
(3,"zhi",12),
(4,"huruwa",20);

9、查看数据表中的所有记录SELECT * FROM _tablename

--查看cms_user
SELECT * FROM cms_user;
技术分享
 


10、修改数据表名字 RENAME...

ALTER TABLE hurujiuser RENAME TO xieuser;

ALTER TABLE xieuser RENAME AS hurujiuser;

ALTER TABLE hurujiuser RENAME xieuser;

RENAME TABLE xieuser TO hurujiuser;

11、添加字段ADD

--添加一个字段
ALTER TABLE hurujiuser ADD card CHAR(18);
--一次添加多个字段
ALTER TABLE hurujiuser ADD test CHAR,
ADD test2 INT(11),
ADD test3 VARCHAR(20);

12、删除字段DROP

--删除一个字段
ALTER TABLE hurujiuser DROP test;
--一次删除多个字段
ALTER TABLE hurujiuser DROP test2,
DROP test3;
--同时添加和删除字段
ALTER TABLE hurujiuser ADD email VARCHAR(50),
DROP age;

13、修改字段MODIFY

ALTER TABLE hurujiuser MODIFY eamil VARCHAR(200) NOT NULL DEFAULT "[email protected]";

14、修改字段位置MODIFY

--将email字段移至addr字段之后
ALTER TABLE hurujiuser MODIFY email VARCHAR(200) NOT NULL DEFAULT "[email protected]" AFTER addr;
--将username字段移至第一位
ALTER TABLE hurujiuser MODIFY username CARCHAR(20) NOT NULL UNIQUE FIRST;

15、添加主键ADD PRIMARY KEY

ALTER TABLE hurujiuser ADD PRIMARY KEY(username);

16、添加唯一ADD UNIQUE

ALTER TABLE hurujiuser ADD UNIQUE(email);

17、为两个字段添加唯一索引ADD UNIQUE INDEX

ALTER TABLE hurujiuser ADD UNIQUE INDEX mul_age_num(age,num);

18、删除唯一索引DROP

ALTER TABLE hurujiuser DROP mul_age_num;

19、删除数据表DROP TABLE _tablename

DROP TABLE hurujiuser;

20、将查询结果插入到新的表中

--将testinsert 中的id,username插入表mytest中
INSERT mytest SELECT id,username FROM testinsert;

21、更改数据UPDATE

--将所有记录的age改为15
UPDATE hurujiuser SET age=15;
--将id=1的记录的age改为2,email为[email protected]
UPDATE hurujiuser SET age=2,email="[email protected]" WHERE id=1;

22、彻底清空表格TRUNCATE

TRUNCATE TABLE hurujiuser;

23、查询数据SELECT ... FROM ...

--查询所有记录
SELECT * FROM hurujiuser;
--查询记录中的id和username
SELECT id username FROM hurujiuser;
--查询id=1的记录
SELECT * FROM hurujiuser WHERE id=1;
--查询id>=6的所有记录
SELECT * FROM hurujiuser WHERE id>=6;

24、模糊查询LIKE
%:代表0个、1个或者多个任意字符
_:代表1个任意字符

--搜索名字中含有张字的用户
SELECT * FROM cms_user WHERE username LIKE "%张%";
--搜索名字中姓张的用户
SELECT * FROM cms_user WHERE username LIKE "张%";
--搜索姓名为4个字的用户
SELECT * FROM cms_user WHERE username LIKE "____";

25、分组查询GROUP BY

SELECT * FROM cms_user GROUP BY proid;

26、显示分组后的详情GROUP_CONCAT

--显示每个组中的姓名
SELECT id,sex,GROUP_CONCAT(username) FROM cms_user GROUP BY sex;

27、二次筛选HAVING

SELECT sex,GROUP_CONCAT(username) AS users,
COUNT(*) AS totalUsers,
MAX(age) AS max_age,
SUM(age) AS total_age
FROM cms_user GROUP BY sex HAVING COUNT(*)>2;

28、排序ORDER BY

--升序默认
SELECT * FROM cms_user ORDER BY id;
SELECT * FROM cms_user ORDER BY id ASC;
--降序
SELECT * FROM cms_user ORDER BY id DESC;

29、随机显示RAND()

SELECT * FROM cms_user RAND();

30、限制记录LIMIT

--显示前3条
SELECT * FROM cms_user LIMIT 3;
SELECT * FROM cms_user LIMIT 0,3;
--显示4,5,6三条
SELECT * FROM cms_user LIMIT 3,3;

31、聚合函数MAX(),MIN(),AVG(),SUM()

SELECT id,sex,age,GROUP_CONCAT(username),
COUNT(*) AS totalusers,
MAX(age) AS max_age,
MIN(age) AS min_age,
AvG(age) AS avg_age,
SUM(age) AS sum_age
FROM cms_user
WHERE id>=1
GROUP BY sex
HAVING COUNT(*)>=2 ORDER BY age DESC LIMIT 2;

32、内连接查询

SELECT u.id,u.username,u.email,u.sex,p.proname
FROM cms_user AS u
INNER JOIN provinces AS p 
ON u.proid=p.id;

SELECT u.id,u.username,p.proName
FROM provinces AS p
CROSS JOIN cms_user AS u
ON p.id=u.proid;

SELECT u.id,u.username,u.sex,p.proName
FROM cms_user AS u JOIN 
provinces AS p
ON u.proid=p.id
WHERE u.sex="男";

33、联合查询UNION

SELECT username FROM employee UNION SELECT username FROM cms_user;

34、子查询

SELECT id username FROM employee WHERE depId IN(SELECT id FROM department);

35、添加外键FOREIGN KEY

CREATE TABLE IF NOT EXISTS employee(
id SMALLINT UNSIGNED AUTO_INCREMENT KEY,
username VARCHAR(20) NOT NULL UNIQUE,
depId TINYINT UNSIGNED,
CONSTRAINT emp_fk_dep FOREIGN KEY(depId) REFERENCES department(id)
)ENGINE=INNODB;

ALTER TABLE employee ADD CONSTRAINT emp_fk_dep FROEIGN KEY(depId) REFERENCES department(id);


鼎峰小胡
QQ.2881064155
[email protected]

 

以上是关于35个常用的MYSQL基本操作命令的主要内容,如果未能解决你的问题,请参考以下文章

C#程序员经常用到的10个实用代码片段 - 操作系统

提效小技巧——记录那些不常用的代码片段

必须学会使用的35个Jquery小技巧

JS常用代码片段-127个常用罗列-值得收藏

web前端开发JQuery常用实例代码片段(50个)

MySQL常用命令基础操作