登录:mysql –uroot –p+mima 查看数据库:show databases; 添加数据库:create database +名字(zhangsan); 删除数据库: drop database zhangsan; 使用数据库(进入数据库):use zhangsan; 查看表:show tables; 删除表: drop table student; 修改表名: alter table student rename to students; 新建表:create table student(id varchar(20),name varchar(20),age varchar(20)); 录入表: insert into student(id,name,age) values('1','zhangsan','10'); 修改表内单个属性: update student set id="2",age=”20” where name="zhangsan"; 删除录入的信息: delete from student where id=’’;或者 where name=’’;或者其他条件都可以 修改表的字段: alter table student change column 原名 新名varchar(20); 删除表的字段: alter table student drop column 名字; 添加表的字段: alter table student add column 名字 varchar(20); 显示表的内容: select *from student; *代表所有 显示单个信息: select id,name from student; 根据条件显示信息: select *from student where id=’1’ ; 同时查询多个条件:(and 并且 or 或) select *from student where id=’1’ or id=’2’; select *from student where id=’1’ and name=’zhangsan’; 如果输入中文需要设置:set names gbk; 只在当前窗口有效,关闭重新开就无效。 主键(意思不能相同): primary key 自动增长: auto_increment 唯一约束: unique 默认值:defult 如果是int 比如 0,如果是varchar(String) ‘0’ 单引号,age int defult 0; 级联删除: alter table t_stu_cou add constraint foreign key(sid) references t_student(sid) on delete cascade; 例如:create table student(id int primary key auto_increment,name varchar(20)); 不能为空:not null; 查询是null: select *from student where name is null; 查询年龄不包括18,23,21的学员信息:select * from student where age not in(18,23,21) 介于那两个数之间:select * from student where age between 20 and 30; 相同内容只显示一个:select distinct id from student; * 查询名中含有"云"的学生 模糊查询(不完全匹配查询)格式:字段 like 条件 条件可以使用特殊符号 % ,表示使用的位置处内容不确定 '%云%' --> 任意位置 '%云' --> 云必须是最后一个,前面任意 '云%' --> 云必须是第一个,后面任意_ ,表示占用一个字符 select * from student where name like '%云%'; * 查询名中第二字还有"云"的学生 select * from student where name like '_云%'; 聚合函数: Max() 最大值 min()最小值 avg() 平均值 sum()总和 count(*) 计数 order by 排序(asc 升序,降序desc) group by分组 Select min(age) from sutdent; 查找最小值 查询表里面有多少条数据: select count(*) from student; 年龄从大到小排序:select age from student order by age desc; 查询两个班的平均年龄: Select avg(age) from student grop by class; Select sum(age)/count(*) from student group by class; 用 group by 的时候不能用where 用having; #求出根据分组的平均值小于15的 select avg(age) as pingjunzhi from student group by classass having pingjunzhi<15; 一对多表: create table student(id varchar(20),name varchar(20),age varchar(20)); create table book(id varchar(20),titile varchar(20),price int,student_id varchar(20)); 创建主表和从表的关系: alter table 从表名 add constraint foreign key (student_id) references 主表名(id); 查询每个学生借的书名: select s.name,b.title from student as s,book as b where s.id=b.student_id;