Linux-MySQL增删改查

数据表

创建
create table 数据表的名字(字段 类型 约束[,字段 类型 约束]);
  • unsigned无符号 整形
  • auto_increment 自动增长
  • primary key 主键 唯一标识这条数据
  • not null 不能为空
create table student(
    id int unsigned auto_increment primary key not null,
	name varchar(30) not null,
    age tinyint unsigned default 0,
    weight decimal(4,2),
    gender enum("男","女","保密","中") default "保密"
  );
查看数据库字段
  • desc student

修改字段

alter table 表名 add 列名 类型;

添加birthday

alter table student add birthday datetime;
change修改字段 (可以给字段改名字)
alter table 表名 change 原名 新名 类型及约束;

birthday改为birth 添加约束not null

alter table students change birthday birth datetime not null;
modify 修改字段 (只改类型和约束)
alter table 表名 modify 列名 类型及约束;
例:
alter table student modify birth date not null;
删除字段
alter table 表名 drop 列名;
例:
alter table student drop birth;
删除表

drop 表名

增删改查(curd)

curd的解释: 代表创建(Create)、更新(Update)、读取(Retrieve)和删除(Delete)

添加insert
  • 注意 遇到添加枚举类型的数据时,数据必须在枚举给里的范围内,否则报错
insert into 表名 values(...)
例:
insert into student values(0,’郭靖‘,18,'88.88','男');
部分列添加
  • 注意不能为空的数据 并且没有默认值的 必须设置
insert into 表名(列1,...) values(值1,...)
例:
insert into student(name,gender) values('黄蓉','女');
同时添加多条
insert into student(name,gender) values('张飞','男'),("赵云","男"),("貂蝉","nv");
修改
update 表名 set 列1=值1,列2=值2... where 条件

update students set name='小龙女',age=10 where id=9;

删除
delete from 表名 where 条件
delete from student where id>6
逻辑删除,本质就是修改操作
  • 给表添加一个字段表示是否删除 isdelete 1表示删除 0表示未删除 默认值0

    alter table student add isdelete  tinyint default 0;
    
  • 删除(实际是修改isdelete)

    update student set isdelete=1 where id=15;
    
  • 所有的查询过滤isdelete=0

    select * from student where isdelete=0;
    
查询
select id,name,age from student where id>2 and isdelete=0;
备份
  • 运行mysqldump命令
mysqldump –uroot –p 数据库名 > python.sql;

# 按提示输入mysql的密码
恢复
  • 连接mysql,创建新的数据库
  • 退出连接,执行如下命令
mysql -uroot –p 新数据库名 < python.sql

# 根据提示输入mysql密码
在数据库中使用source 执行.sql文件
  • 注意:在进入数据库前,当前目录要处于和sql同一目录下,否则找不到sql文件

你可能感兴趣的:(mysql,linux,数据库)