深入浅出谈MYSQL增删改查

创建表

-- 创建一个学生表,id为主键,姓名不能为空,地址默认北京,外键使用学院的id

create table student(

  id int primary key auto_increment,

  name varchar(20) not null,

  college_id int not null,

  address varchar(20) default "北京",

  foreign key(college_id) references college(id)

);

-- 创建一个学院表,id为主键

create table college(

  id int primary key auto_increment,

  name varchar(20)

);

-- 新增数据

insert into 表名(字段名) values(数据);

-- 删除数据

delete from 表名 where 条件

-- 修改数据

update 表名 set 字段名 = 修改的数据 where 条件

-- 查询数据

select 查询列表  from 表名

  where 条件

  group by 分组字段名 having 筛选条件

  order by 字段  asc 升序/desc 降序   默认升序

  limit 查询的条数

-- 聚合函数

sum()  count()  min()  max()  avg()

-- 修改字段

-- 删除,添加或修改表字段

alter table 表名 drop 要删除的字段名

alter table 表名 add 增加的字段 类型等

-- 修改字段类型及名称

alter table 表名 modify 字段名 类型

-- 使用change 紧跟着要修改的字段名,然后指定新字段名及类型

alter table 表名 change 原来字段名 新字段名 类型等约束

-- 修改表名

alter table 表名 rename to 新表名

猜你喜欢

转载自blog.csdn.net/a159357445566/article/details/109188940