SQL数据库中email的@约束怎么写

SQL数据库中email的@约束怎么写,第1张

SQL数据库中email的@约束可用check语句。

工具:sqlserver 2008 R2

步骤:

1、打开sqlserver 2008 R2中的SSMS,连接到指定数据库。

2、创建员工表,其中包含email字段,并且要对email字段加以@的约束,语句如下:

create table 员工

(工号 varchar(10),

姓名 varchar(20),

性别 varchar(2),

email varchar(50) check (email like '%@%'))

3、测试email字段包含@的数据,可正常插入。

insert into 员工 values ('001','东东','男','[email protected]')

4、如果email输入不含@的内容,则报错:

insert into 员工 values ('002','腹胀','男','fuzhanghaha.com')

创建Oracle数据库的字段约束:

非空约束唯一约束对字段的取值的约束默认值外键约束create table tab_class(class_id number primary key,class_name varchar2(10) not null unique )create table tab_stu( stu_id number,--学生姓名,不能为空,不能重复 stu_name varchar2(20) not null unique,--学生姓名只能是male或female stu_gender varchar2(6) not null check(stu_gender='male' or stu_gender='female'),--学生年龄只能在18到60之间 stu_age number check(stu_age >18 and stu_age <60),--邮箱可以不填写,填写的话不能相同 stu_email varchar2(30) unique, stu_address varchar2(30), --外键约束 class_id number not null references tab_class(class_id))

维护已经创建好的约束:

可添加或删除约束,但不能直接修改。可使约束启用和禁用。非空约束必须使用MODIFY子句增加。为表增加主键约束:--维护约束 --创建约束 create table tab_check(che_id number,che_name varchar2(20) ) --为表增加主键约束 alter table tab_check add constraints tab_check primary key(che_id)

添加唯一约束

--添加唯一约束,tab_check_unique表示约束的名称 alter table tab_check add constraints tab_check_unique unique(che_name)

添加检查约束:

--添加一个字段 alter table tab_check add che_age number --添加检查约束 alter table tab_check add constraints tab_check_age check(che_age>18 and che_age<60)

删除约束:

--删除主键约束 alter table tab_check drop constraints tab_check

禁用约束:

--禁用约束 alter table tab_check disable constraints tab_check

启用约束

--启用约束 alter table tab_check enable constraints tab_check

复合约束,联合主键,也就是两个字段的组合成一个主键

--联合主键 create table tab_person(tab_firstname varchar2(10),tab_lastname varchar2(10),tab_gender varchar2(5),primary key(tab_firstname,tab_lastname) )

为表添加外键约束:

alter table tab_stu add constraints tab_stu foreign key(class_id) references tab_class(class_id)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

一、约束的分类

在SQLServer中,有3种不同类型的约束。

1、实体约束

实体约束是关于行的,比如某一行出现的值就不允许出现在其他行,例如主键。

2、域约束

域约束是关于列的,对于所有行,某一列有那些约束,例如CHECK约束。

3、参照完整性约束

如果某列的值必须与其他列的值匹配,那就意味着需要一个参照完整性约束,例如外键。

二、约束命名搜索

在学习约束之前,首先来了解下为约束命名需要注意哪些地方。

SQLServer在我们不提供名称时,会自动创建名称,但是由系统自动创建的名称并不是特别有用。

例如,系统生成的主键名称可能是这样的:PK_Employees_145C0A3F。

PK代表主键(primary key),Employees代表在Employees表中,而剩下的“145C0A3F”部分是为了保证唯一性而随机生成的值。只有通过脚本创建才会得到这种值,如果是通过Managerment Studio创建表,那么就直接是PK_Employees。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9412900.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存