By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,184 Members | 1,500 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,184 IT Pros & Developers. It's quick & easy.

Nulls being allowed when they shouldnt be?

P: n/a
I have a simple table, for some reason, certain columns seem to accept
Nulls even though they shouldn't, for example the I can set the 'Name'
field to Null using my web application or directly in Enterprise
Manager. field How do I prevent this? However the 'RecCreated' doess
not permit nulls.
CREATE TABLE [dbo].[Group] (
[GroupID] [int] IDENTITY (1000, 1) NOT NULL ,
[Name] [nvarchar] (50) NOT NULL ,
[Description] [nvarchar] (750) NULL ,
[RecCreated] [datetime] NOT NULL ,
[RecUpdated] [datetime] NOT NULL ,
[RecCreatedBy] [int] NOT NULL ,
[RecUpdatedBy] [int] NOT NULL ,
[RecActive] [int] NOT NULL
) ON [PRIMARY]
GO
thanks for any help you can give on this
Jul 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
If your adding data to varchar field using EM are actually entering an empty
string? and most likely this is what your web application is doing as well.

1) You really shouldn't be using EM to enter data into your tables, or
rather you shouldn't be relying on it as a primary means to enter data.
It's great for testing and the like but it should be limitted to that.
2) It sounds like your web application needs some additional middle tier
logic to make sure that empty strings aren't passed into the database.

Hopefully these ideas help,
Muhd.

"grist2mill" <gr********@excite.com> wrote in message
news:46************************@posting.google.com ...
I have a simple table, for some reason, certain columns seem to accept
Nulls even though they shouldn't, for example the I can set the 'Name'
field to Null using my web application or directly in Enterprise
Manager. field How do I prevent this? However the 'RecCreated' doess
not permit nulls.
CREATE TABLE [dbo].[Group] (
[GroupID] [int] IDENTITY (1000, 1) NOT NULL ,
[Name] [nvarchar] (50) NOT NULL ,
[Description] [nvarchar] (750) NULL ,
[RecCreated] [datetime] NOT NULL ,
[RecUpdated] [datetime] NOT NULL ,
[RecCreatedBy] [int] NOT NULL ,
[RecUpdatedBy] [int] NOT NULL ,
[RecActive] [int] NOT NULL
) ON [PRIMARY]
GO
thanks for any help you can give on this

Jul 20 '05 #2

P: n/a
Can you post some code that will reproduce the problem. I assume you are
aware that NULL is different to the empty string? How did you check that
NULLs were present in the table? Can you actually run the following queries
in Query Analzer and see what they return:

SELECT [name]
FROM dbo.[Group]
WHERE [name] IS NULL

SELECT COLUMNPROPERTY(OBJECT_ID('dbo.[Group]'),'name','AllowsNull')

You can't always believe what you see in Enterprise Manager because the
display isn't always refreshed when you would expect it to be. Also, I
wouldn't trust EM as a method for entering data into tables.

P.S. "GROUP" is a reserved word. It's not a good idea to use reserved words
for table names. Anyway "Group" is too meaningless to make a good table
name. Group of what?

--
David Portas
SQL Server MVP
--
Jul 20 '05 #3

P: n/a

"grist2mill" <gr********@excite.com> wrote in message
news:46************************@posting.google.com ...
I have a simple table, for some reason, certain columns seem to accept
Nulls even though they shouldn't, for example the I can set the 'Name'
field to Null using my web application or directly in Enterprise
Manager. field How do I prevent this? However the 'RecCreated' doess
not permit nulls.
CREATE TABLE [dbo].[Group] (
[GroupID] [int] IDENTITY (1000, 1) NOT NULL ,
[Name] [nvarchar] (50) NOT NULL ,
[Description] [nvarchar] (750) NULL ,
[RecCreated] [datetime] NOT NULL ,
[RecUpdated] [datetime] NOT NULL ,
[RecCreatedBy] [int] NOT NULL ,
[RecUpdatedBy] [int] NOT NULL ,
[RecActive] [int] NOT NULL
) ON [PRIMARY]
GO
thanks for any help you can give on this


In Enterprise Manager, if you type NULL in the table data screen, it will
put the literal string 'NULL' into the table, which is not the same as a
real NULL; you need Ctrl+0 to get a real NULL. I suspect your application is
doing the same thing, or perhaps displaying an empty string as NULL, which
it isn't.

EM isn't a good application for modifying data, because it's not always
completely clear what it's doing. You should use Query Analyzer instead,
because then you can control every detail of the SQL you execute, and you
can also save scripts for future use more easily.

Simon
Jul 20 '05 #4

P: n/a
Thanks to all for your help and tips.

So if I understand things correctly, (having run David's suggested
queries),

if you set a column to be nvarchar to not accept nulls, the column
will still accept empty strings. So to make a field mandatory, I must
enforce this at the application level (which is not such good
practice, because if another application should access the table I
won't know if they correctly enforce that certain fields should be
mandatory).

Have I understod things correctly (or should a be using a different
data type from nvarchar)?
Jul 20 '05 #5

P: n/a
Thanks to all for your help and tips.

So if I understand things correctly, (having run David's suggested
queries),

if you set a column to be nvarchar to not accept nulls, the column
will still accept empty strings. So to make a field mandatory, I must
enforce this at the application level (which is not such good
practice, because if another application should access the table I
won't know if they correctly enforce that certain fields should be
mandatory).

Have I understod things correctly (or should a be using a different
data type from nvarchar)?
Jul 20 '05 #6

P: n/a
On 11 Oct 2004 03:05:00 -0700, grist2mill wrote:
if you set a column to be nvarchar to not accept nulls, the column
will still accept empty strings. So to make a field mandatory, I must
enforce this at the application level


Hi grist2mill,

You can use a CHECK constraint:

CREATE TABLE xxxx (....,
NonEmptyCol nvarchar(78) NOT NULL
CHECK (NonEmptyCol <> ''),
....)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #7

P: n/a
Thanks Hugo,
thats the problem solved
regards
GM

Hugo Kornelis <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message news:<ou********************************@4ax.com>. ..
On 11 Oct 2004 03:05:00 -0700, grist2mill wrote:
if you set a column to be nvarchar to not accept nulls, the column
will still accept empty strings. So to make a field mandatory, I must
enforce this at the application level


Hi grist2mill,

You can use a CHECK constraint:

CREATE TABLE xxxx (....,
NonEmptyCol nvarchar(78) NOT NULL
CHECK (NonEmptyCol <> ''),
....)

Best, Hugo

Jul 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.