准备工作
创建数据库
create database test;
使用数据库
use test;
创建表
create table student;
下面的数据库语句将以此表为例
一 添加数据
1 列出字段名
insert into student (sid,sname,score) values(100,'李四',60);
2 不指定字段名
insert into student values(100,'李四',60);
3 列出部分字段名
insert into student (sid,sname) values(100,'李四',60);
注意:未列出字段将为默认值
//一条语句添加多条记录
insert into student (sid,sname,score) values(100,'aaa',60),(101,'bbb',78);
二 删除数据
1 删除部分数据
//删除姓名为李四的数据
delete from student where sname='李四';
//删除成绩在60以下的数据
delete from student where score<60;
2 删除全部数据
delete form student;
三 更新数据
1 更新部分数据
//将姓名为李四的学生,学号改为6
update student set sid=6 where sname='李四';
//将成绩在60以下的学生,成绩都改到60
update student set score=60 where score<60;
//将姓名为李四的学生,学号改为1,成绩改为90
update student set sid=1,score=90 where sname='李四';
更新全部数据
//将所有学生成绩改为0分
update student set score=0;
四 数据查询
1 简单查询
//查询所有字段
desc student;
select * from student;
select sid,sname,score from student;
//查询sname字段,只显示sname
select sname from student;
2 按条件查询(where)
//查询sid为1的学生
select * from student where sid=1;
//查询sid为1的学生的姓名、成绩
select sname,score from student where sid=1;
//查询80分以上学生
select * from student where score >80;
3 特定条件查询
查询在指定集合中的记录(in)
//查询学生表中是否有('张三','李四','王五')
select * from student where sname in('张三','李四','王五');
//查询不在集合中的记录
select * from student where sid not in(1,2,3);
查询在指定范围中的记录(between …and)
//查询学号在1-100之间的记录
select * from student sid between 1 and 100;
//查询学号不在1-100之间的记录
select * from student sid not between 1 and 100;
空值查询(is null)
//查询表中姓名为空的记录
select * from student where sname is null;
过滤重复信息查询(distinct)
//查询学生的sid
select distinct sid from student;
//查询学生的成绩
select distinct score from student;
模糊查询(通配符%)
//查询score字段中,以5开头的记录
select * from student where score like "5%";
//查询score字段中,以5结尾的记录
select * from student where score like "%5";
//查询score字段中,含有5的记录
select * from student where score like "%5%";
区别查询(通配符_)
** 我们发现,这两条记录,只有一个字母不同,该如何找出?**
select * from student where sname like "wang_ao";
注意:“_” 只匹配单个字符
精准查询(and/or)
select * from student where sid=1 and score>20;
select * from student where sid=1 or score>20;
五 表的约束
1 主键约束
//创建表时指定单字段为主键
create table student
(
sid int primary key,
sname varcahr(20),
score int
);
//创建表时指定多字段为主键
create table student
(
sid int,
sname varcahr(20),
score int,
primary key(sid,sname)
);
//已存在的表添加主键
alter table student add primary key(sid);
2 非空约束
//创建表时指定字段为非空
create table student
(
sid int not null,
sname varcahr(20) not null,
score int not null
);
//已存在的字段添加非空约束
alter table student modify sname varchar(20) not null;
3 默认约束(default)
//创建时添加默认约束
create table student
(
sid int default 0,
sname varchar default 'sb',
score int default 0
);
//创建表后,字段本身无默认值
alter table student modify sname varchar(20) default 'sb';
//创建表后,字段本身有默认值
alter table student drop sname//删除字段
alter table student add sname varchar(20) default 'sb';
注意:删除字段后,原先数据全为新建后的默认值
4 自增约束(auto_increment)
//创建时添加自增约束
create table student
(
sid int auto_increment,
sname varchar,
score
);
//创建时添加自增约束,并设置自增初始值为100
create table student
(
sid int auto_increment,
sname varchar,
score
)auto_increment=100;
六 数据操纵
1 修改表名
//将表名student改为stu
alter table student rename to stu;
2 添加/删除字段
//添加字段sex,数据类型为枚举型
alter table student add sex enum('f','m');
//删除字段sex
alter table student drop sex;
3 修改字段名/字段数据类型
//将sname字段改名为new_sname
alter table student change sname new_sname varchar(20);
//将score字段数据类型由INT改为VARCHAR
alter table student modify score varchar(20);
3 修改字段位置
//将sex字段放在最上面
alter table student modify sex enum('f','m') first;
//将sid字段放在score后
alter table student modify sid int after score;
欢迎补充,未完待续。。。。。。
欢迎补充,未完待续。。。。。。
欢迎补充,未完待续。。。。。。
欢迎补充,未完待续。。。。。。
欢迎补充,未完待续。。。。。。
欢迎补充,未完待续。。。。。。