首页 > 代码库 > 35个常用的MYSQL基本操作命令
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
--查看hurujiuserSHOW 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_userSELECT * 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 "myuser@qq.com";
14、修改字段位置MODIFY
--将email字段移至addr字段之后ALTER TABLE hurujiuser MODIFY email VARCHAR(200) NOT NULL DEFAULT "myuser@qq.com" 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改为15UPDATE hurujiuser SET age=15;--将id=1的记录的age改为2,email为id@qq.comUPDATE hurujiuser SET age=2,email="id@qq.com" WHERE id=1;
22、彻底清空表格TRUNCATE
TRUNCATE TABLE hurujiuser;
23、查询数据SELECT ... FROM ...
--查询所有记录SELECT * FROM hurujiuser;--查询记录中的id和usernameSELECT 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_ageFROM 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_ageFROM cms_userWHERE id>=1GROUP BY sexHAVING COUNT(*)>=2 ORDER BY age DESC LIMIT 2;
32、内连接查询
SELECT u.id,u.username,u.email,u.sex,p.pronameFROM cms_user AS uINNER JOIN provinces AS p ON u.proid=p.id;SELECT u.id,u.username,p.proNameFROM provinces AS pCROSS JOIN cms_user AS uON p.id=u.proid;SELECT u.id,u.username,u.sex,p.proNameFROM cms_user AS u JOIN provinces AS pON u.proid=p.idWHERE 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
Skype.2881064155@qq.com
35个常用的MYSQL基本操作命令