Re: Use DEFAULT CONSTRAINTs or BOUND DEFAULTs?



"serge" <sergea@xxxxxxxxxxxxxxxxx> wrote in message
news:KQa9f.3990$u8.202495@xxxxxxxxxxxxxxxxxxxxxx
>I am doing a little research on Google about this topic and I ran into
> this thread:
>
> http://groups.google.com/group/microsoft.public.sqlserver.programming/browse_thread/thread/63cce060ff152dcc/1dc13d4ee6758966?lnk=st&q=difference+constraint+sql+defaults&rnum=14#1dc13d4ee6758966
>
> I read SQL Server MVP Louis Davidson's post saying:
>
> "Actually they are more likely to drop the concept of bound defaults.
> Constraints are the standard way to do this, and really should be the way
> you create defaults anyhow."
>
> Even I read in the Microsoft SQL Server Introduction (SQL 7 book
> page 244, however we're using SQL Server 2000):
>
> "Constraints define rules regarding the values allowed in columns and are
> the standard mechanism for enforcing integrity, preferred over triggers,
> rules, and defaults. They are also used by the query optimizer to improve
> performance in selectivity estimation, cost calculations, and query
> rewriting."
>
> Why constraint defaults are better? The second sentence about constraints
> having better optimization, I am guessing they don't mean this about
> Default Constraints, rather the other type of constraints?
> Because I don't see how a Default Constraint have anything to do with
> performance? Isn't default only to do with new records being created?
>
>
> At work we are setting all tables' columns to have constraint defaults
> of 0 or ' ' (space character) in order not to have any column with the
> NULL value. Therefore we have dozens of files containing statements like:
>
> alter table TABLE1 add constraint TABLE1_ID_DF
> DEFAULT(' ') FOR ID
> go
> alter table TABLE1 add constraint TABLE1_QUANTITY_DF
> DEFAULT(0) FOR QUANTITY
> go
>
> First I was thinking to create 3 SQL Defaults called:
> DefaultZero
> DefaultSpace
> DefaultDate
>
> and then bind these defaults to all the columns of all tables excluding
> primary keys. After creating the tables I would enumerate through
> all the columns and bind one of these three Defaults based on their
> datatype:
> number = DefaultZero
> text type = DefaultSpace
> date type = DefaultDate
>
> And then unbind the ones that we specifically need to specify other
> default values.
>
> So my question is should I do this by using sp_binddefault or stick
> with using Default Constraints inside a table/columns loop code?
>
>
> Thank you
>
>
>

Default constraints are closer to standard SQL and in my experience are the
more conventional solution and better understood by most people using SQL
Server today. Here's a quote from SQL Server 2005 Books Online about the
legacy CREATE DEFAULT / sp_binddefault syntax:

"CREATE DEFAULT will be removed in a future version of Microsoft SQL Server.
Avoid using CREATE DEFAULT in new development work, and plan to modify
applications that currently use it. Instead, use default definitions created
using the DEFAULT keyword of ALTER TABLE or CREATE TABLE."

--
David Portas
SQL Server MVP
--


.



Relevant Pages

  • Use DEFAULT CONSTRAINTs or BOUND DEFAULTs?
    ... Constraints are the standard way to do this, and really should be the way ... Even I read in the Microsoft SQL Server Introduction (SQL 7 book ... DefaultZero ... and then bind these defaults to all the columns of all tables excluding ...
    (comp.databases.ms-sqlserver)
  • Re: Case Statement Error When Creating Fucntion
    ... SQL Server MVP ... The optimizer can use the CHECK ... Your check constraints do not even match the OP's code. ...
    (microsoft.public.sqlserver.programming)
  • Re: Need a little help please
    ... >> Does not having indexes or PK hinder the insert? ... > A lack of indexes and constraints doesn't affect the insert per se. ... If there is a clustered index, SQL Server will immediately know where to put this row. ... Tibor Karaszi, SQL Server MVP ...
    (microsoft.public.sqlserver.server)
  • Re: Unique index not recognizing null
    ... if they're not subject to the constraints of the Index?" ... consistent with other products - including SQL Server, Oracle, and how MS ... value in one or more of the key fields will not be included in the ... Nulls setting made no difference. ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Question on Structuring Product Attributes
    ... constraints, I have found a combination of features that allow for ... materialized-views that bring the data together, ... I have no experience with Oracle, ... feature SQL Server has) that joins data from subtype and supertype ...
    (comp.databases.theory)