Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How can we have otional fields in a table?

Author  Topic 

g_p
Starting Member

48 Posts

Posted - 2008-01-29 : 09:11:24
Hello to all,

i want to make a table in order to keep the data of a hard disk.
e.g. create table disk{
capacity integer,
name varchar(50),
ports integer,
buffer char(1));

For example i want this hard disk to have these fields and i would like from the user to type me only the capacity because it is going to be a required field and the other fields(name,ports,buffer) to be optional!

How do i declare optional fields in a create table command?
and then how do i declare/define these fields in insert too?

Thanks, in advance!

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2008-01-29 : 10:09:58
what you need to do is either dynamic sql or normal t-sql with case.

if you want in create table function a field to be mandatory then add to capacity (leave other just the way they are):

create table my_disk
(
id int not null primary key identity,
capacity int not null,
name varchar(50),
buffer char(1),
ports int
)

when you will insert data to these table, only mandatory field will be capacity and primary key, which is auto incremented.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-29 : 12:49:46
declare all optional fields to be of type NULL. Then you dont require to pass value into them during inserts.
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2008-01-29 : 17:15:50
quote:
Originally posted by visakh16

declare all optional fields to be of type NULL. Then you dont require to pass value into them during inserts.

Define them to ALLOW nulls. Null is not a datatype.

e4 d5 xd5 Nf6
Go to Top of Page
   

- Advertisement -