如何让一个字段的默认值等于另一个字段的值?

chc124 2010-05-08 10:30:34
一个表有两个字段,想让一个字段的默认值等于两一个非空字段的值,请问该如何做?
...全文
1105 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
chc124 2010-05-08
  • 打赏
  • 举报
回复
1.顺便请问下密码字段应该用什么数据类型?
2.密码长度8~20位如何限制密码长度?
htl258_Tony 2010-05-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 chc124 的回复:]
引用 2 楼 fredrickhu 的回复:
直接用计算列


因为我有一张学生信息表,包含学号、姓名等。还有一张登录信息表。想让学生信息表插入记录时,引发触发器,登录信息表也插入一条数据,帐号等于学号,密码初始值也为学号。以后可以修改的。
[/Quote]
你这个需求不是默认值的问题
xman_78tom 2010-05-08
  • 打赏
  • 举报
回复

if OBJECT_ID('students') is not null
drop table students;
go
create table students (student_id char(13) primary key,name varchar(20) not null);
go
if OBJECT_ID('accounts') is not null
drop table accounts;
go
create table accounts (account_id char(13) primary key,passwd binary(20) not null);
go

create trigger trg_students_ins on students
for insert
as
set nocount on;
insert into accounts (account_id, passwd)
select student_id,hashbytes('sha1',student_id) from inserted;
go

insert into students values('S2010F0402001','Wang Fang');

select * from students;
select * from accounts;
chc124 2010-05-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 htl258 的回复:]
如果另一个字段空呢
[/Quote]
另一个字段设置的非空的。
you_tube 2010-05-08
  • 打赏
  • 举报
回复
嗯,可以用触发器
[Quote=引用 3 楼 chc124 的回复:]

引用 2 楼 fredrickhu 的回复:
直接用计算列


因为我有一张学生信息表,包含学号、姓名等。还有一张登录信息表。想让学生信息表插入记录时,引发触发器,登录信息表也插入一条数据,帐号等于学号,密码初始值也为学号。以后可以修改的。
[/Quote]
htl258_Tony 2010-05-08
  • 打赏
  • 举报
回复
如果另一个字段空呢
chc124 2010-05-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 fredrickhu 的回复:]
直接用计算列
[/Quote]

因为我有一张学生信息表,包含学号、姓名等。还有一张登录信息表。想让学生信息表插入记录时,引发触发器,登录信息表也插入一条数据,帐号等于学号,密码初始值也为学号。以后可以修改的。
--小F-- 2010-05-08
  • 打赏
  • 举报
回复
直接用计算列
无语孩童 2010-05-08
  • 打赏
  • 举报
回复
写个公式
chc124 2010-05-08
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 xman_78tom 的回复:]
SQL code

if OBJECT_ID('students') is not null
drop table students;
go
create table students (student_id char(13) primary key,name varchar(20) not null);
go
if OBJECT_ID('accounts') is not nul……
[/Quote]

非常感谢!
xman_78tom 2010-05-08
  • 打赏
  • 举报
回复

if OBJECT_ID('students') is not null
drop table students;
go
create table students (student_id char(13) primary key,name varchar(20) not null);
go
if OBJECT_ID('accounts') is not null
drop table accounts;
go
create table accounts (account_id char(13) primary key,passwd varbinary(20) not null);
go

create trigger trg_students_ins on students
for insert
as
set nocount on;
insert into accounts (account_id, passwd)
select student_id,cast(student_id as varbinary(20)) from inserted;
go

create trigger trg_accounts_pwd on accounts
for insert, update
as
set nocount on;
-- 判断密码的长度
if exists (select * from inserted where datalength(passwd) not between 8 and 20)
begin
raiserror('Invalid password.',15,0);
rollback tran;
end
-- 基于安全考虑,存储密码的 hash 值,而不直接存储密码原文。
update a set a.passwd=hashbytes('sha1',a.passwd)
from accounts a,inserted i where a.account_id=i.account_id;
go

insert into students values('S2010F0402001','Wang Fang');
-- 允许
update accounts set passwd=cast('p@ssw0rd' as varbinary(20));
-- 违反
update accounts set passwd=CAST('123456' as varbinary(20));

select * from students;
-- 通过将密码原文转为 hash 值验证密码
select * from accounts where passwd=hashbytes('sha1','p@ssw0rd');

htl258_Tony 2010-05-08
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 chc124 的回复:]
1.顺便请问下密码字段应该用什么数据类型?
2.密码长度8~20位如何限制密码长度?
[/Quote]
1.建议varchar或nvarchar
2.可以在程序语言上限制,也可以在SQL用check约束或触发器限制。

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧