1. SELECT user(); -- 查看当前哪个用户登录到数据库
2. SHOW DATABASES; -- 查看DBMS中有哪些数据库
3. USE 数据库名 --更换要使用的数据库
4. SELECT database(); -- 查看当前正在使用哪个数据库
5. CREATE DATABASE 数据库名; -- 建立一个数据库
6. CREATE USER '用户名'@'%' IDENTIFIED BY '密码';
C:\Program Files\MySQL\MySQL Server 5.7\bin
mysql -u tom -p
FLUSH PRIVILEGES
7. GRANT ALL ON world.* to 'tom'@'%'; -- 授予tom用户在world数据库中所有的权限
8. REVOKE ALL ON world.* from 'tom'@'%'; -- 回收tom用户在world数据库中所有的权限
9. SELECT "Hello, World!" AS "String";
10. SELECT "Hello, World!" AS "String" FROM dual;
11. SELECT now(); -- 显示当前数据库系统中的日期
12. SELECT code, name, gnp FROM world.country; -- 选择world数据库中country表格中code, name, gnp三列的内容进行显示
13. SHOW TABLES; -- 当前数据库中存在哪些表格
14. \g可以替代分号作为结尾
15. \G 将表格中的数据纵向排列显示
16. DELIMITER $$ -- 改变结束符号
DELIMITER ;
17. status -- 查看一些基本信息
18. \c -- 放弃当前语句
19. SOURCE c:\789.txt -- 执行789.txt文件中的内容
\. c:\789.txt -- 同上句,意思完全相同
20. TEE /home/name/111.txt -- 打开记录日志功能
NOTEE; -- 关闭日志记录功能
insert into test(id, name) values(1, 'Tom');
update test set name='XX';
select * from test where name='Tom' and (age > 4 or hige like '%0' or sex in ('男','女')) order by id asc/desc; --%就相当于正则中的*
SELECT coalesce(name, '总数'), SUM(singin) as singin_count FROM employee_tbl GROUP BY name WITH ROLLUP;
delete from test where id=1; --清空表中的内容
21.
CREATE TABLE new_table1(
id int NOT NULL AUTO_INCREMENT,
name varchar(20) default femn,
birthday date,
salary double,
sex boolean,
primary key (id) --constraint pk_id primary key (id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8; -- 建立表innodb才支持事务
SHOW CREATE TABLE table_name \G;
22. DESC new_table1; -- 查看表结构
23. INSERT INTO new_table1(id, name, birthday, salary, sex) VALUES (1, 'Tom', DATE'1982-05-17', 5000.0, true); -- 插入一行数据
24. SELECT * FROM new_table1; -- 查看表中内容
25. TRUNCATE TABLE new_table1; -- 清除表中内容
26. DROP TABLE new_table1; -- 删除表
27. ALTER TABLE new_table1 modify id long; -- 修改列
28. ALTER TABLE new_table1 add xxx integer; -- 添加列
29. ALTER TABLE new_table1 drop xxx; -- 删除列
ALTER TABLE table_name CHANGE id id1 INT
ALTER TABLE testalter_tbl RENAME TO alter_tbl; --修改表名
主键约束=唯一性约束+非空约束
alter table test
add constraint pk_id primary key(id); 主键约束
alter table test
add constraint un_name unique(name); 唯一性约束:唯一但是允许非空
alter table test
add constraint ck_id check id >=1; 检查性约束
alter table emp
add constraint fk_deptno
foreign key(deptno) references dept(deptno);
外键就是另外一张表中的主键
alter table test
drop constraint `ck_id`;
select count(*) from test;
select max(id) from test;
select min(id) from test;
select avg(id) from test;
select sum(id) from test;
子查询 一个查询语句里面还套有一个查询语句.
select deptno from emp where ename='SCOTT';
select empno, ename, deptno from emp where deptno=20;
select empno, ename, deptno from emp
where deptno = (select deptno from emp where ename='SCOTT');
select empno, ename, deptno from emp as e1
where sal> (select avg(sal) from emp as e2 where e1.deptno=e2.deptno);--
-- 各部门有多少人高于本部门平均工资
select empno, ename, deptno from emp as e1
where sal<ANY(select sal from emp where deptno=30 ) AND deptno<>30;--