数据库入门,一篇就够了,纯语句

准备工作

创建数据库

create database test;

使用数据库

use test;

创建表

create table student;

1

下面的数据库语句将以此表为例

一 添加数据

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);

注意:未列出字段将为默认值
2

//一条语句添加多条记录
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

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(12,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)

5

//查询学生的sid
select distinct sid from student;

1

//查询学生的成绩
select distinct score from student;

1

模糊查询(通配符%)

//查询score字段中,以5开头的记录
select * from student where score like "5%";

1

//查询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";

1

注意:“_” 只匹配单个字符

精准查询(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);

1

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;

1

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);

1

//将score字段数据类型由INT改为VARCHAR
alter table student modify score varchar(20);

1

3 修改字段位置

//将sex字段放在最上面
alter table student modify sex enum('f','m') first;

1

//将sid字段放在score后
alter table student modify sid int after score;

1

欢迎补充,未完待续。。。。。。
欢迎补充,未完待续。。。。。。
欢迎补充,未完待续。。。。。。
欢迎补充,未完待续。。。。。。
欢迎补充,未完待续。。。。。。
欢迎补充,未完待续。。。。。。
欢迎补充,未完待续。。。。。。

写了一万年,跪求三连🙌🙌🙌

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小橙菜鸡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值