语法结构
CREATE DATABASE database_name;
示例
CREATE DATABASE demo;
语法结构
CREATE TABLE 表名 (
列1 数据类型,
列2 数据类型,
...
);
示例
CREATE TABLE new_user (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT
);
语法格式
create view <视图名> as select查询语句
示例
create view user_view as select id,user_id,user_name from `user`
select * from user_view
索引名建议以 idx开头。
语法格式
CREATE INDEX 索引名 ON 表名 (列名);
示例
CREATE INDEX idx_user_id ON user (user_id);
可通过explain进行验证
explain select * from user where user_id = 'wu'
语法结构
ALTER TABLE 表名 ADD 列名 数据类型;
示例
ALTER TABLE user ADD age int(11) DEFAULT null COMMENT '年龄';
语法结构
ALTER TABLE 表名 DROP 列名;
示例
ALTER TABLE user DROP user_age;
语法结构
ALTER TABLE 表名 CHANGE 旧列名 新列名 数据类型;
示例
ALTER TABLE user CHANGE age user_age int(11) DEFAULT null COMMENT '新年龄';
语法结构
ALTER TABLE 表名 MODIFY 列名 新数据类型;
示例
ALTER TABLE user MODIFY user_age varchar(16);
语法结构
ALTER TABLE 表名 RENAME TO 新表名;
示例
ALTER TABLE user RENAME TO new_user;
语法结构
ALTER TABLE 表名 ADD INDEX 索引名 (列名);
示例
ALTER TABLE user ADD INDEX idx_user_id (user_id);
语法结构
ALTER TABLE 表名 DROP INDEX 索引名;
示例
ALTER TABLE user DROP INDEX idx_user_id;
语法结构
DROP DATABASE 数据库名称
示例
DROP DATABASE demo
语法结构
DROP TABLE 表名;
示例
DROP TABLE new_user;
语法结构
DROP VIEW view_name;
示例
DROP VIEW user_view;
INSERT INTO USER (user_id,user_name,create_time,dept_id) VALUES('sa','saname',now(),1)
INSERT INTO USER (
user_id,
user_name,
create_time,
dept_id
)
VALUES
('sa1', 'saname1', now(), 1),
('sa2', 'saname2', now(), 1),
('sa3', 'saname3', now(), 1)
UPDATE USER
SET dept_id = 2
WHERE
user_id = 'sa2'
delete from user where user_id = 'sa1'
select <列名>
from <表名>
where <筛选条件>
group by <列名>
having <聚合筛选条件>
order by <排序字段>
limit <条数限制>
大于
select * from user where id > 5
小于
select * from user where id < 5
不等于
select * from user where id != 5
select * from user where id <> 5
注意:是包括在内,如下所示 大于等于,而不是大于
select * from user where id BETWEEN 1 and 3
select * from user where id >=1 and id <=3
select * from user where id in (1,2,4)
select * from user where id = 2 and user_id = 'wu'
select * from user where id =1 or user_id = 'wu'
select DISTINCT(user_name) from user
select * from user where user_id like 'sa%'
倒序
select * from user order by id DESC
顺序
select * from user order by id ASC
select * from user GROUP BY dept_id
select * from user GROUP BY dept_id HAVING id > 1
select * from user limit 0,10;
select * from user limit 1;
SELECT
*
FROM
USER t1
INNER JOIN dept t2 ON t1.dept_id = t2.id
查询出左表和右表所有数据,但是去除两表的重复数据;
SELECT
*
FROM
USER t1
FULL JOIN dept t2 ON t1.dept_id = t2.id
以上代码执行会出现错误,因为MYSQL 不支持全连接!!!
左连接是左边表的所有数据都有显示出来,右边的表数据只显示共同有的那部分,没有对应的部分只能补空显示,所谓的左边表其实就是指放在left join的左边的表;
select * from user t1 left join dept t2 on t1.dept_id = t2.id
右连接正好是和左连接相反的,这里的右边也是相对right join来说的,在这个右边的表就是右表;
select * from user t1 right join dept t2 on t1.dept_id = t2.id
示例
select count(1) from user
select count(*) from user
select count(id) from user
示例
select sum(id) from user
示例
select max(id) from user
select min(id) from user
示例
select user_id , LENGTH(user_id) from user
语法结构
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE result
END
示例
SELECT
CASE
WHEN dept_id is NULL THEN '无部门'
ELSE '有部门'
END as DeptId,
user_id,user_name
FROM
USER;