标签:max 正则表达 删除 incr 常用sql useful 转义 rom locate
CREATE USER user_name IDENTIFIED BY ‘password‘;
SET PASSWORD FOR user_name = PASSWORD(‘newpassword‘);
SHOW GRANTS FOR user_name; --查看name用户权限
GRANT SELECT ON db_name.* TO user_name; --给name用户db_name数据库的所有权限
REVOKE SELECT ON db_name.* TO name; --GRANT的反操作,去除权限
SHOW DATABASES;
CREATE DATABASE db_name;
USE db_name;
DROP DATABASE db_name;
CREATE TABLE table_name(
id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT, --ID值,无符号、非空、递增(唯一性,适做主键)
name VARCHAR(60) NOT NULL,
score TINYINT UNSIGNED NOT NULL DEFAULT 0, --默认值0
PRIMARY KEY(id)
)ENGINE=InnoDB --设置表的存储引擎,一般常用InnoDB和MyISAM。InnoDB可靠,支持事务;MyISAM高效不支持全文检索
DEFAULT charset = utf8; --设置默认编码,防止数据库中文乱码
如果有条件地创建数据表还可以使用
CREATE TABLE IF NOT EXISTS tb_name(...)
CREATE TABLE tb_name2 SELECT * FROM tb_name;
或者复制部分表:
CREATE TABLE tb_name2 SELECT id,name FROM tb_name;
CREATE TABLE TEMPORARY TABLE tb_name; -- 和创建普通表一样
SHOW TABLES;
DESCRIBE tb_name;
或者:
SHOW COLUMNS IN tb_name; -- FROM也行
DROP [TEMPORARY] TABLE [IF EXISTS] tb_name [,tb_name2];
RENAME TABLE name_old TO name_new;
或者:
ALTER TABLE useless RENAME useful;
ALTER TABLE tb_name ADD [CHANGE,DROP,RENAME] -- 要更改的内容
实例:
ALTER TABLE tb_name ADD COLUMN address varchar(60) NOT NULL;
ALTER TABLE tb_name DROP address;
ALTER TABLE tb_name CHANGE column_name_old column_name_new INT(6);
INSERT INTO camp(camp_id,camp_name,camp_leader) VALUES (null,‘艾米莉亚阵营‘,2),(null,‘菲鲁特阵营‘,3);
INSERT INTO charactor VALUES (null,‘艾米莉亚‘,null,2,1),(null,‘菲鲁特‘,null,0,2);
INSERT INTO useful SELECT * FROM charactor;
*代表所有字段,也可以挨个写入需要的字段
UPDATE charactor SET charactor_name=‘冰之魔女艾米莉亚‘ WHERE charactor_id=2;
UPDATE tablename SET columnName=NewNalue [WHERE condition];
DELETE FROM charactor WHERE charactor_id=1;
SELECT * FROM charactor WHERE charactor_camp_id=1;
2.HAVING 语句
SELECT charactor_camp_id as ‘阵营编号‘,count(*) as ‘阵营人数‘
FROM charactor
GROUP BY charactor_camp_id
HAVING count(*)>1;
tips:
①执行顺序:
where 早于 group by 早于 having
②where是约束声明,在返回结果前起作用
③having是过滤声明,对查询结果进行过滤操作
④having后面可以使用聚合函数:sum\count\max\avg
SELECT * FROM tb_name WHERE name REGEXP ‘^[A-D]‘; -- 找出以A-D为开头的name
1.字符串链接:
SELECT CONCAT(charactor_name,‘来自于阵营‘,charactor_camp_id) as ‘角色出身‘ FROM charactor;
AVG
SUM
MAX
MIN
COUNT
TRIM
LOCATE
UPPER
LOWER
SUBSTRING
DATE()
CURTIME()
DAY()
YEAR()
NOW()
SELECT COUNT(*) FROM tb_name GROUP BY score HAVING COUNT(*)>1;
ORDER BY DESC|ASC 降序排序和升序排序
(1 )from
(2) on
(3) join
(4) where
(5) group by (开始使用select中的别名,后面的语句中都可以使用)
(6) avg,sum....
(7) having
(8) select
(9) distinct
(10) order by
标签:max 正则表达 删除 incr 常用sql useful 转义 rom locate
原文地址:https://www.cnblogs.com/ylq167/p/14406552.html