Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 19th, 2006, 01:23 AM
Registered User
 
Join Date: Oct 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Can anyone point out my error please....

CREATE TABLE tblCustomers
(
CustomerID Char(5) IDENTITY,
CompanyName Char(40) NOT NULL,
ContactName Char(30) NOT NULL,
ContactTitle Char(30) ,
Address Char(60) NOT NULL,
City Char(15) NOT NULL,
Province Char(2) NOT NULL,
PostalCode Char(10) NOT NULL,
Country Char(15) NOT NULL,
Phone Char(24) NOT NULL,
Fax Char(24) NOT NULL,
PRIMARY KEY (CustomerID),
CONSTRAINT Province CHECK (Type in('AB','BC','MB','NB','NL','NT','NS','NU','ON','P E','QC','SK','YT')),
CONSTRAINT default_country DEFAULT 'Canada' FOR Country,
)

And my error message is:
Server: Msg 170, Level 15, State 1, Line 16
Line 16: Incorrect syntax near 'for'.

And when I take that line out I get this error message:
Server: Msg 1759, Level 16, State 1, Line 1
Invalid column 'Type' is specified in a constraint or computed-column definition.
Server: Msg 1750, Level 16, State 1, Line 1
Could not create constraint. See previous errors.

It looks correct to me so I can't figure out what is wrong.

 
Old November 19th, 2006, 11:47 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Here's a couple of tips about the code you posted... PLEASE don't take it personally... just thinking that we should get started with some fairly normalized code...

1. You cannot use CHAR as the datatype for an IDENTITY column. It must be BigInt, Int, SmallInt, TinyInt or Decimal with zero decimal places defined.

2. You should always name your keys and constraints on permanent tables. They mean something and so should their names. You really will appreciate the names down the road when you're trying to debug something on a more complicated table.

3. "Type" is not a column name. Province is.

4. "FOR" is part of trigger code, not table creation code.

5. Although I've included the check for Province names as requested, it is very wrong to do that here. You should have a table of Provinces to check against so that when you add another country, you can include some of their geographical boundaries without having to recreate the constraint (and many other reasons, as well. Google "3rd normal form").

6. Although I've included the phone numbers in the table creation as requested, these really belong in a phone number table using the CustomerID as an FK to the Customer table. A secondary lookup table for phone number type is also in order.

7. Although I've included the address info in the table creation as requested, these really belong in an address table using the CustomerID as an FK to the Customer table. A secondary lookup table for address type is also in order.

8. Although I've included the contact info in the table creation as requested, these really belong in an contact table using the CustomerID as an FK to the Customer table. In addition, contacts should also have an address column pointing to the address table because a contact could have a different address than the company. Not sure whether you'll need a contact type table for this but I'd plan on it anyway.

9. Although I've included the Country table as a 10 character column in the table creation as requested, it really belongs in an Country table with the various different ways a country can be named including 2 and 3 character ISO names as well as the "long" name. The Customer table should point to one of those countries.

10. There is absolutely no good reason to prefix a table name with "tbl" although I'm certain some will argue the point to death.

11. Some folks will also take exception to the table being called Customers instead of just Customer... guess it's whichever camp you're from on that one.

And, here's the code you requested... it works, but, as you can see from above, should never be allowed to exist in a production environment.

Code:
 CREATE TABLE dbo.tblCustomers 
        (
        CustomerID   INT IDENTITY(1,1) NOT NULL ,
        CompanyName  VARCHAR(40)       NOT NULL ,
        ContactName  VARCHAR(30)       NOT NULL ,
        ContactTitle VARCHAR(30)       NOT NULL ,
        Address      VARCHAR(60)       NOT NULL ,
        City         VARCHAR(15)       NOT NULL ,
        Province     CHAR(2)           NOT NULL ,
        PostalCode   VARCHAR(10)       NOT NULL ,
        Country      VARCHAR(15)       NOT NULL CONSTRAINT DF_tblCustomers_Country DEFAULT ('Canada'),
        Phone        VARCHAR(24)       NOT NULL 
        Fax          VARCHAR(24)       NOT NULL ,
        CONSTRAINT PK_tblCustomers_CustomerID 
            PRIMARY KEY CLUSTERED (CustomerID),
        CONSTRAINT CK_tblCustomers_Province 
            CHECK (Province IN ('AB','BC','MB','NB','NL','NT','NS','NU','ON','PE','QC','SK','YT'))
        )
GO



--Jeff Moden





Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there any point RobReid Flash (all versions) 0 September 26th, 2008 09:07 AM
Point Function varun Pro VB 6 5 November 18th, 2005 11:15 PM
point in the right direction, please chriswinn ASP.NET 1.0 and 1.1 Basics 2 February 10th, 2005 07:40 AM
Page 23, Point 2, Error 1046 carlitio BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 9 July 9th, 2004 01:52 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.