MySQL
Posted Alasola
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL相关的知识,希望对你有一定的参考价值。
基础
use DBname;选择数据库
set names utf8;设置字符集
语法
select 选择
select [distinct column_name...]|*
from tableName
where column_name [operator] value
{[joiner column_nameX operator valueX]...}
group by column_nameY desc|asc;
operator:
=,<>,>,<,>=,<=,BETWEEN,LIKE,IN
joiner:
AND,OR
insert 插入
第一种:
INSERT INTO table_name
VALUES (value1,value2,value3,...);
第二种:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
update 更新
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
delete 删除
DELETE FROM table_name
WHERE some_column=some_value;
其他
create 创建
• DATABASE
CREATE DATABASE dbname;
• TABLE
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
• INDEX
列值不唯一索引:
CREATE INDEX index_name
ON table_name (column_name);
添加索引
ALTER table tableName ADD INDEX indexName(columnName);
删除索引
DROP INDEX [indexName] ON mytable;
展示索引
SHOW INDEX FROM table_name;
列值唯一索引:
CREATE UNIQUE INDEX index_name
ON table_name (column_name);
修改唯一索引
ALTER table table_name ADD UNIQUE [indexName] (username(length));
alter 变更
• TABLE
添加列:
ALTER TABLE table_name
ADD column_name datatype
删除列:
ALTER TABLE table_name
DROP COLUMN column_name
改变列的数据类型:
ALTER TABLE table_name
MODIFY COLUMN column_name datatype
添加主键
ALTER TABLE table_name ADD PRIMARY KEY (i);
修改主键
ALTER TABLE table_name MODIFY i INT NOT NULL;
删除主键
ALTER TABLE table_name DROP PRIMARY KEY;
drop 删除
• DATABASE
DROP DATABASE database_name;
• TABLE
删除表:
DROP TABLE table_name
删除表内数据
TRUNCATE TABLE table_name
• INDEX
Oracle:
DROP INDEX index_name
mysql:
ALTER TABLE table_name DROP INDEX index_name
高级
返回指定记录数
MySql:
SELECT column_name(s)
FROM table_name
LIMIT number;
Oracle:
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;
通配符
- 通配符
描述
%
替代 0 个或多个字符
_
替代一个字符
[charlist]
字符列中的任何单一字符
[^charlist] 或 [!charlist]
不在字符列中的任何单一字符
- 别名
方式一:
SELECT column_name AS alias_name
FROM table_name;
方式二:
SELECT column_name(s)
FROM table_name AS alias_name;
- 连接
SQL INNER JOIN 语法
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
或:
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
SQL LEFT JOIN 语法
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
或:
SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;
SQL RIGHT JOIN 语法
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
或:
SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;
SQL FULL OUTER JOIN 语法
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
SQL UNION 语法
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
SQL UNION ALL 语法
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
SQL SELECT INTO 复制表
MySQL:
SQL INSERT INTO SELECT 语法以从一个表中复制所有的列插入到另一个已存在的表中:
INSERT INTO table2
SELECT * FROM table1;
只复制希望的列插入到另一个已存在的表中:
INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;
其他数据库:
我们可以复制所有的列插入到新表中:
SELECT *
INTO newtable [IN externaldb]
FROM table1;
只复制希望的列插入到新表中:
SELECT column_name(s)
INTO newtable [IN externaldb]
FROM table1;
约束
- NOT NULL:指示某列不能存储 NULL 值
- UNIQUEL:保证某列的每行必须有唯一的值
- PRIMARY KEY:主键
- FOREIGN KEY:外键
- CHECK:保证列中的值符合指定的条件
- DEFAULT规定没有给列赋值时的默认值
- AUTO_INCREMENT:自动地创建主键字段的值
- 试图创建:
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
- 更新:
CREATE OR REPLACE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
- 删除:
DROP VIEW view_name
函数
从列中取得的值,返回一个单一的值
• AVG() - 返回平均值
SELECT AVG(column_name) FROM table_name
• COUNT() - 返回行数
SELECT COUNT([distinct] column_name|*) FROM table_name;
• FIRST() - 返回第一个记录的值
MySQL:
SELECT column_name FROM table_name
ORDER BY column_name ASC
LIMIT 1;
Oracle:
SELECT column_name FROM table_name
ORDER BY column_name ASC
WHERE ROWNUM <=1;
• LAST() - 返回最后一个记录的值
MYSQL:
SELECT column_name FROM table_name
ORDER BY column_name DESC
LIMIT 1;
Oracle:
SELECT column_name FROM table_name
ORDER BY column_name DESC
WHERE ROWNUM <=1;
• MAX() - 返回最大值
SELECT MAX(column_name) FROM table_name;
• MIN() - 返回最小值
SELECT MIN(column_name) FROM table_name;
• SUM() - 返回总和
SELECT SUM(column_name) FROM table_name;
基于输入值,返回一个单一的值
• UCASE() - 将某个字段转换为大写
SQL UCASE() 语法
SELECT UCASE(column_name) FROM table_name;
用于 SQL Server 的语法
SELECT UPPER(column_name) FROM table_name;
• LCASE() - 将某个字段转换为小写
SQL LCASE() 语法
SELECT LCASE(column_name) FROM table_name;
用于 SQL Server 的语法
SELECT LOWER(column_name) FROM table_name;
• MID() - 从某个文本字段提取字符
MySql 中使用
SELECT MID(column_name,start[,length]) FROM table_name;
• SubString(字段,1,end) - 从某个文本字段提取字符
• LEN() - 返回某个文本字段的长度
SQL LEN() 语法
SELECT LEN(column_name) FROM table_name;
MySQL 中函数为 LENGTH():
SELECT LENGTH(column_name) FROM table_name;
• ROUND() - 对某个数值字段进行指定小数位数的四舍五入
SELECT ROUND(column_name,decimals) FROM table_name;
• NOW() - 返回当前的系统日期和时间
SELECT NOW() FROM table_name;
• FORMAT() - 格式化某个字段的显示方式
SELECT FORMAT(column_name,format) FROM table_name;
以上是关于MySQL的主要内容,如果未能解决你的问题,请参考以下文章
连接MySQL出现错误:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)(代码片段
使用 json rereiver php mysql 在片段中填充列表视图
关于mysql驱动版本报错解决,Cause: com.mysql.jdbc.exceptions.jdbc4Unknown system variable ‘query_cache_size(代码片段
修改MySQL密码报错“ERROR 1819 (HY000): Your password does not satisfy the current policy requirements“(代码片段