首页 小组 文章 相册 留言本 用户 搜索 我的社区 在线学堂 商城 购物车 支付钱包

[打卡]念燊e

2024-05-21 10:25:49
0
170


#not null 只能用列约束,不能用表级约束

create DATABASE db;

use db;

create table test (
id int not null,
last_name VARCHAR(10)not null,
email VARCHAR(20),
salary int
);

desc test;

insert into test values(1,'tom','tom1@163.com',6000);

show create table test;

select * from test;

insert into test(id,email)
values(4,'abc@163.com');
desc test;

update test set email=null where id=4;

alter table test modify email VARCHAR(25)not null;

alter table test modify email VARCHAR(25)null;

create table test3 (
id int unique,#列级约束
last_name VARCHAR(15),
email VARCHAR(25),
salary int,
constraint uk_test3_email unique(email)
);

desc test3;

insert into test3 (id,last_name,email,salary)
values (1,'tom','tom@163.com',8000);

select * from test3;

insert into test3 (id,last_name,email,salary)
values (2,'sam','tom@163.com',7000);

insert into test3 (id,last_name,email,salary)
values (2,'lilil','lili@163.com',9000);

alter table test3 add constraint uk_test3_sal unique
(salary);

desc test3;

alter table test3 
modify last_name VARCHAR(15) unique;
评论
意见反馈