MySQL简介:
RDBMS: Relational database management system: 关系型数据库管理系统
MySQL目录结构:
bin :
mysqld.exe .服务端程序,打开它,才能被客户端访问( cmd –> net start[stop] mysql {也可以在服务中手动开启} )
mysql.exe: 客户端程序, mysql -u root -p 123 -h localhost;{ 用户名,密码,主机 } 退出:exit | quit;{bye}
SQL语句:
结构化查询语言,各自数据库存在方言!
DDL :定义语言 ,操作数据库,操作表
SHOW DATABASES; USE mydb1; CREATE DATABASE [IF NOT EXISTS] mydb1; DROP DATABASE [IF EXISTS] mydb1; ALTER DATABASE mydb1 CHARACTER SET utf8; SHOW TABLES; //查看emp表结构; DESC emp; ALTER TABLE stu ADD (classname varchar(100)); ALTER TABLE stu MODIFY gender CHAR(2); ALTER TABLE stu change gender sex CHAR(2); ALTER TABLE stu RENAME TO student;DML :操作语言
INSERT INTO stu(sid, sname,age,gender) VALUES(‘s_1001‘, ‘zhangSan‘, 23, ‘male‘); INSERT INTO stu VALUES(‘s_1002‘, ‘liSi‘, 32, ‘female‘); UPDATE stu SET sname=’zhangSanSan’, age=’32’, gender=’female’ WHERE sid=’s_1001’; DELETE FROM stu WHERE sid=’s_1001’003B //TRUNCATE和DELETE都可以删除表的所有记录, 但前者无法回滚 TRUNCATE TABLE stu;DCL :控制语言
//创建用户: CREATE USER 用户名@地址 IDENTIFIED BY ‘密码‘; CREATE USER [email protected] IDENTIFIED BY ‘123’; //给用户授权,GRANT 权限1, … , 权限n ON 数据库.* TO 用户名 GRANT CREATE,ALTER,DROP,INSERT,UPDATE,DELETE,SELECT ON mydb1.* TO [email protected]; //撤销授权,REVOKE权限1, … , 权限n ON 数据库.* FORM 用户名 REVOKE CREATE,ALTER,DROP ON mydb1.* FROM [email protected]; //查看用户权限,SHOW GRANTS FOR 用户名 SHOW GRANTS FOR [email protected]; //删除用户,DROP USER 用户名 DROP USER [email protected]; //修改用户密码 ;UPDATE USER SET PASSWORD=PASSWORD(‘密码’) WHERE User=’用户名’ and Host=’IP’; UPDATE USER SET PASSWORD=PASSWORD(‘1234‘) WHERE User=‘user2‘ and Host=’localhost’; FLUSH PRIVILEGES;DQL :查询语言
语法: SELECT selection_list /*要查询的列名称*/ FROM table_list /*要查询的表名称*/ WHERE condition /*行条件*/ GROUP BY grouping_columns /*对结果分组*/ HAVING condition /*分组后的行条件*/ ORDER BY sorting_columns /*对结果分组*/ LIMIT offset_start, row_count /*结果限定*/
基础查询,条件查询,模糊查询,字段控制查询,排序,聚合函数,分组查询,LIMIT
主外键,连接查询,子查询,
SELECT e.ename, e.sal, d.dname, d.loc FROM emp e, (SELECT dname,loc,deptno FROM dept) d WHERE e.deptno=d.deptno AND e.empno=1006
时间: 2024-10-16 11:36:43