mysql怎样添加唯一标识_MySql设置唯一标识,主键等设置

本文介绍了如何在MySQL中创建和管理唯一标识和主键。包括如何添加、删除和修改主键约束,设置唯一键,调整自增字段,以及默认值的操作。详细讲解了ALTER TABLE语句的使用,如CHANGE和MODIFY的区别。
摘要由CSDN通过智能技术生成

create table user_score(

name char(10) not null default “”,

score smallint not null default 0

);

增加一列

alter table user_score add column id smallint after name; ( 不要引号)

alter table user_score add column idea smallint;

主键&唯一键

主键不能为空 唯一键可以为空

主键only能有一个 唯一键可以有多个

主键可以多个列和并起来作为一个 唯一键不行

//在数据库表中 只有设置一个int类型的字段为主键,才可以设这个字段为标识列,标识列的作用就是一个可以自动增长的数字,如消息id,唯一并且不重复,是消息唯一标识

设置主键

alter table user_score add primary key(id);

//删除主键需要两步.

1、如果有auto_increment,先删除之; alter table user_score change id id int;

2、删除主键约束 primary key. alter table user_score drop primary key;

设置唯一约束

alter table user_score add column idea smallint auto_increment;// error一个表只能有一个自增列.

alter table user_score add column idea smallint default 0 unique; // 不好,这样往里面加数据的时候࿰

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值