MariaDB(数据库)查删改

MariaDB 数据类型
MariaDB数据类型可以分为数字,日期和时间以及字符串值。
使用数据类型的原则:够用就行, 尽量使用范围小的,而不用大的
• 常用的数据类型

  1. 整数:int, bit
  2. 小数:decimal    #decimal(5,2)
  3. 字符串:varchar, char
  4. 日期时间:date, time, datetime
  5. 枚举类型(enum) 明确给出选项让你选择的
    • 约束
  6. 主键primary key:物理上存储的顺序(默认是索引:相当于是目录)
  7. 非空not null:此字段不能为空
  8. 唯一unique:此字段不允许重复
  9. 默认default:当不填写此值时会使用默认值,如果填写则已填写为准
  10. 外键foreign key:对关系字段进行约束,当为关系字段填写值时,会到关联的表中查询此值是否存在,如果存在则填写成功,如果不存在则填写失败并抛出异常

–显示当前时间
select now();

–创建classes表(id, name)
create table xxxx(id int, name varchar(20));
create table yyyy(id int primary key not null auto_increment, name varchar(30));
create table zzzz(
id int primary key not null auto_increment,
name varchar(20)
);

–查看表结构 表的列和约束
desc table_name

–创建students表(id, name, age, high, gender, cls_id)
create table students (
id int unsigned not null auto_increment primary key,
name varchar(20),
age tinyint unsigned default 0,
high decimal(5,2),
gender enum(‘男’, ‘女’, ‘中性’, ‘保密’) default ‘保密’,
cls_id int unsigned
);

insert into students values(0,‘金星’,18, 188.88, ‘中性’, 0);

查询students表数据
select * from students;

–创建classes表(id, name)
create table classes(
id int unsigned not null auto_increment primary key,
name varchar(20)
);
–查看表的创建
show create table table_name

–修改表-添加字段
–alter table 表名 add 列名 类型;
alter table students add birthday datetime;

– 修改表-修改字段:不重命名版,修改类型
– alter table 表名 modify 列名 类型及约束;
alter table students modify birthday date;

– 修改表-修改字段:重命名版
– alter table 表名 change 原名 新名 类型及约束;
alter table students change birthday birth date default ‘2000-01-01’;

– 修改表-删除字段
– alter table 表名 drop 列名;
alter table students drop xxx;

– 删除表
– drop table 表名;

–MyISAM与InnoDB区别
–两种类型最主要的区别就是InnDB支持事物处理与外键和行级锁
–增删改查
–增加
–全列插入
–insert into 表名 values(…) 括号内是列对应的数据
–主键字段 可以用0 null default 来站位

insert into classes values (0, ‘大神班’);

– 向students表里插入 一个学生信息
insert into students values (0, ‘鹿鼎记’, 20, ‘女’, 1, ‘1990-01-01’)
insert into students values (null, ‘鹿鼎记’, 20, ‘女’, 1, ‘1990-01-01’)
insert into students values (default, ‘鹿鼎记’, 20, ‘女’, 1, ‘1990-01-01’)
insert into students values (default, ‘鹿鼎记’, 20, ‘1’, 1, ‘1990-01-01’)
insert into students values (default, ‘鹿鼎记’, 20, ‘2’, 1, ‘1990-01-01’)
insert into students values (default, ‘鹿鼎记’, 20, ‘3’, 1, ‘1990-01-01’)
insert into students values (default, ‘鹿鼎记’, 20, ‘4’, 1, ‘1990-01-01’)

–失败insert into students values (default, ‘鹿鼎记’, 20, ‘第四性别’, 1, ‘1990-01-01’)

–部分插入
insert into students(name, gender) values (‘小乔’, 2);
insert into students(name, gender) values (‘大乔’, 2),(‘貂蝉’, 2);

–修改
–update 表名 set 列1=值1, 列2=值2… where 条件;

–删除
– 物理删除
– delete from 表名 where 条件
delete from students;
delete from students where xx=xx

– 逻辑删除
– 用一条字段来表示 这条信息是否已经不能在使用了
– 给students表添加一个is_delete字段 bit 类型
– alter table students add is_delete bit default 0;
– update students set is_delete=1 where id=6;
alter table students add is_delete bit default 0;

–查询基本使用
– 查询所有列
–select * from 表名
select * from students;

–一定条件查询
select * from students where name=‘小李飞刀’;
select * from students where id>3;

– 查询制定列
select name, gender from students;

– 可以使用as制定列或表制定别名;
select name as 姓名, gender as 性别 from students;

–字段的顺序
select id as 序号, gender as 性别, name as 姓名 from students;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值