如何在sql中设置列的dafult值 [英] How to set the dafult value of a column in sql
问题描述
我有一个表''users'',其中列出了所有用户信息,一个名为status的列,其数据类型为int。另一个表是状态,其中只有两列,即statusid和statusname,statusid是自动增量的,在users表中作为外键引用。 statusname包含待处理,已批准等的值。
现在我必须在用户表中设置status的默认值,具体取决于状态表状态ID的值为挂起状态用户表中的默认值
请帮助我
i have a tables ''users'' in which all user information is listed and a column named status whose data type is int.the other table is status in which only two columns i.e. statusid and statusname, the statusid is autoincremented and is referenced in the users table as foreign key. the statusname contains values as pending, approved etc.
Now i have to set the default value of status in users table depending on vlaue of Status table status id as pending as default value in user table
please help me
推荐答案
您好b $ b
转到特定表的设计视图(数据库 - >表 - >设计)然后在列属性中查看默认值或绑定。在这里你可以指定默认值。
问候
Willington
Hi
Goto Design view of the particular table (DATABASE-->Table-->Design) then look Default Valur or Binding in Column Properties. Here you can specify the default value.
Regards
Willington
你需要创建< b>该列的默认约束(假设待定的statusid为1):
You need to create Default Constraint for that column (assuming statusid for pending is 1):
ALTER TABLE dbo.users ADD CONSTRAINT
DF_users_statusid DEFAULT 1 FOR statusid
HI,
您可以尝试这样:< br $>
You can try like this:
ALTER TABLE {TABLENAME}
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL}
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}
添加默认值现有专栏
Add Default Value to Existing Column
-- Add default to existing column DateOfHire:
ALTER TABLE [dbo].[Employees] ADD DEFAULT (getdate()) FOR [DateOfHire]
-- Add default value to existing column IsTerminated
ALTER TABLE [dbo].[Employees] ADD DEFAULT ((0)) FOR [IsTerminated]
添加默认值创建表
Add Default Value with Create Table
CREATE TABLE [dbo].[Employees]
(
[EmployeeID] [INT] IDENTITY(1,1) NOT NULL,
[FirstName] [VARCHAR](50) NULL,
[LastName] [VARCHAR](50) NULL,
[SSN] [VARCHAR](9) NULL,
-- Add default of zero
[IsTerminated] [bit] NOT NULL DEFAULT ((0)) ,
-- Add default of getdate()
[DateAdded] [datetime] NULL DEFAULT (getdate()),
[Comments] [VARCHAR](255) NULL,
[DateOfHire] [datetime] NULL
)
希望这会给你一些想法...
谢谢
Hope this will give you some idea...
Thanks
这篇关于如何在sql中设置列的dafult值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!