MySQL数据库的基本操作1

MySQL是一种关系数据库管理系统,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。
show databases;(查看所有数据库)

use 数据库名称(进入某个指定的数据库中)

select database();(查看当前所在的数据库名称)

show tables;(查看当前数据库中所有数据表)

desc students;(查看数据表students的表结构)

alter table students add birthday datetime not null;(为数据表添加birthday字段类型为datetime,约束为非空)

alter table students change birthday birth datetime not null;(修改字段birthday为birth)
alter table students modify birth date not null;(修改字段的类型为date)

添加is_delete结构语句
alter table students add is_delete bit default 0;
将第六行设置为is_delete
update students set is_delete = 1 where id = 6;

当查看is_delete=0的时候只显示其他的,就会出现删除的假象
select * from students where is_delete = 0;


向表中批量插入数据
insert into students values(0,'韦少',18,180.00,2,1,0),(0,'小月月',18,180.00,2,2,1),(0,'彭于晏',29,185.00,1,1,0),(0,'刘德华',59,175.00,1,2,1),(0,'芙蓉',38,160.00,2,1,0),(0,'凤姐',28,150.00,4,2,1),(0,'王祖贤',18,172.00,2,1,1),(0,'周杰伦',36,NULL,1,1,0),(0,'程坤',27,181.00,1,2,0),(0,'刘亦菲',25,166.00,2,2,0),(0,'金星',33,162.00,3,3,1),(0,'静香',12,180.00,2,4,0),(0,'周杰',34,176.00,2,5,0);
查询数据表中指定的列
select name,age,gender from students;

select后面列前面使用distinct可以将重复的行去除
查询students数据表中有几种性别(distinct去重操作):
select distinct gender from students:


模糊查询 like 关键字
下划线_ 表示任意一个字符
百分号% 表示任意多个字符
查询表中名字姓为周,且名为一个字的人(关键字like)
select * from students where name like "周_"

查询表中名字姓为周,且名为两个字的人
select * from students where name like "周_ _";

查询表中所有姓周的人
select * from students where name like "周%";

查询表中姓周的人(正则匹配:关键字rlike)
select * from students where name rlike "^周";:

查询 id 为1,3,8的学生
select * from students where id in(1,3,8);

查询 id 为 1到8之前的学生(关键字:between)
select * from students where id between 1 and 8;

空判断(关键字:is null、、和 is not null)
select * from students where height is null;


  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值