1.DDL:数据定义语言 1)针对数据库 create database oldboy; Syntax: CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [create_specification] ... create_specification: [DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name 增: mysql> create database IF NOT EXISTS zls CHARACTER SET=utf8 COLLATE=utf8_general_ci; 删: drop database oldboy; 改: mysql> alter database zls CHARACTER SET=gbk; 2)针对表的 增: create table student( sid INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT ‘学号’, sname VARCHAR(20) NOT NULL COMMENT ‘学生姓名’, sage TINYINT UNSIGNED COMMENT ‘学生年龄’, sgender ENUM('m','f') NOT NULL DEFAULT ‘m’ COMMENT ‘学生性别’, cometime DATETIME NOT NULL COMMENT ‘入学时间’)chatset utf8 engine innodb; 删: mysql> drop table oldboy2; 改: mysql> alter table stu add yyf varchar(20) first; mysql> alter table stu add dengyifan varchar(20) after egon; mysql> alter table stu add yuanhu varchar(10); mysql> alter table stu drop egon; mysql> alter table stu rename stu1; mysql> alter table stu add test varchar(20),add qq int; 2.DCL:数据控制语言 grant #其他参数(扩展) max_queries_per_hour:一个用户每小时可发出的查询数量 max_updates_per_hour:一个用户每小时可发出的更新数量 max_connetions_per_hour:一个用户每小时可连接到服务器的次数 max_user_connetions:允许同时连接数量 grant all on *.* to root@'%'; 所有库,所有表wu grant all on mysql.* to root@'%'; mysql中的所有表:单库级别 grant all on mysql.user to root@'%'; mysql库的user表:单表级别 脱敏:单列级别 grant select(name,age,sex) on mysql.user to dev@'%'; 运维或者DBA给开发人员开数据库用户: grant select,insert,update on *.* to dev@'%' identified by '123'; revoke mysql> revoke select on *.* from root@'%'; SELECT,INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE 3.DML:数据操作语言 insert: insert into stu values('linux01',1,NOW(),'zhangsan',20,'m',NOW(),110,123456); insert into stu(classid,birth.sname,sage,sgender,comtime,telnum,qq) values('linux01',1,NOW(),'zhangsan',20,'m',NOW(),110,123456); insert into stu(classid,birth.sname,sage,sgender,comtime,telnum,qq) values('linux01',1,NOW(),'zhangsan',20,'m',NOW(),110,123456), ('linux02',2,NOW(),'zhangsi',21,'f',NOW(),111,1234567); update: mysql> update oldboy set id=11; 企业中 规范用法:where条件 mysql> update oldboy set id=10 where id=1; mysql> update oldboy set id=12 where 1=1; delete: mysql> delete from student; mysql> delete from student where sid=3; update代替delete做伪删除: 1)给表,添加一个状态列 mysql> alter table oldboy add status enum('1','0') default '1'; 2)删除数据 mysql> update oldboy set status='0' where id=2; 3)查询数据 mysql> select * from oldboy where status='1'; 4.DQL数据查询语言 select: mysql> select * from city; mysql> select * from city where countrycode='CHN'; mysql> select * from city where countrycode='CHN' limit 10; mysql> select * from city limit 10,10; mysql> select name,population from city where countrycode='CHN' and district='heilongjiang'; #世界上小于100人的人口城市是哪个国家的? 国家名, 城市名, 人口数量 country.name city.name city.population 1.传统连接 select country.name,city.name,city.population from city,country where city.countrycode=country.code and city.population<100; 2.自连接:两张表中,有相同的列名字 城市名 国家简称 语言 城市人口 SELECT city.name,city.countrycode ,countrylanguage.language ,city.population FROM city NATURAL JOIN countrylanguage WHERE population > 1000000 ORDER BY population limit 10; 3.内连接:小表在前,大表在后 城市名字 国家名 城市人口 select city.name,country.name,city.population from city join country on city.countrycode=country.code where city.population<100; 范式: 减少数据冗余,防止产生一致性问题,把一个表作为一个原子,把一张表拆到不能再拆为止。(开发阶段设计规范)