October 10, 2012 at 10:40 am
i would need to change the service code in the service table to be 2characters followed by 2 digits. For existing services,Add an 'X' to the beginning of each serviceCode. Any affected tables(s) should also changed accordingly.
this would be my table:
CREATE TABLE ClientTable
(
clientID char(5) NOT NULL,
clientName Char(30) NOT NULL,
clientAddress char(50) NOT NULL,
clientContact Numeric(8) NOT NULL,
CONSTRAINT ClientTablePK PRIMARY KEY(clientID),
CHECK(clientID like '[A-Z][0-9][0-9][0-9][A-Z]'),
CHECK (clientContact like '[6][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' OR
clientContact like'[8][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' OR
clientContact like'[9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
);
CREATE TABLE CaseTable
(
referenceNum int NOT NULL IDENTITY(100000,1),
startDate DATETIME NOT NULL,
endDate DATETIME NULL,
caseDetail Char(255) NOT NULL,
caseType Char(255) NOT NULL DEFAULT'copyright and trademark',
clientID Char(5) NOT NULL,
CONSTRAINT CaseTablePK PRIMARY KEY(referenceNum),
CONSTRAINT ClientTableFK FOREIGN KEY(clientID)
REFERENCES ClientTable(clientID),
CONSTRAINT caseTypeValues CHECK(caseType LIKE 'intellectual property enforcement' OR
caseType LIKE'copyright and trademark' OR caseType LIKE'patent and industrial design' OR
caseType LIKE'trade secret' OR caseType LIKE 'risk management' OR
caseType LIKE'litigation'),
CONSTRAINT clientIDValues CHECK(clientID LIKE '[A-Z][0-9][0-9][0-9][A-Z]')
);
CREATE TABLE ServiceTable
(
serviceCode Char(3) NOT NULL,
serviceDescription Char(255) NOT NULL,
minCharge Money NOT NULL,
CONSTRAINT ServiceTablePK PRIMARY KEY(serviceCode),
CONSTRAINT serviceCodeValues CHECK(serviceCode like '[C][0-9][0-9]' OR
serviceCode like'[E][0-9][0-9]' OR
serviceCode like'[O][0-9][0-9]'),
CONSTRAINT minChargeValues CHECK(minCharge BETWEEN '500' AND '1000000')
);
CREATE TABLE ServiceRenderedTable
(
referenceNum int NOT NULL IDENTITY(100000,1),
serviceDate DATETIME NOT NULL,
serviceCode Char(3) NOT NULL,
actualServiceCost Money NOT NULL ,
CONSTRAINT ServiceRenderedTablePK PRIMARY KEY(referenceNum,serviceDate),
CONSTRAINT CaseTableFK FOREIGN KEY(referenceNum)
REFERENCES CaseTable(referenceNum),
CONSTRAINT ServiceTableFK FOREIGN KEY(serviceCode)
REFERENCES ServiceTable(serviceCode),
CONSTRAINT actualServiceCost_positive CHECK (actualServiceCost>0.00)
);
appriciate if anyone could help me. Thanks
October 10, 2012 at 10:45 am
This looks a LOT like homework. What have you tried? What do you think you need to change?
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 10, 2012 at 10:49 am
Hi Sean Lange,
I am a new to SQL. And i try doing looking at a book but gives me error and lots of it. i am not sure how to start
October 10, 2012 at 12:17 pm
hillerz19 (10/10/2012)
Hi Sean Lange,I am a new to SQL. And i try doing looking at a book but gives me error and lots of it. i am not sure how to start
OK so what do you think needs to be changed? What have you tried so far that didn't work? I could provide the script for this fairly easily but then you don't learn how to solve it.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 10, 2012 at 8:28 pm
Hi Sean Lange,
This is my coding so far but allot of errors:
ALTER TABLE ServiceTable
ALTER COLUMN serviceCode char(4) NOT NULL,
ALTER TABLE CaseTable
DROP CONSTRAINT serviceCodeValues,
ALTER TABLE CaseTable
ALTER serviceCode DROP DEFAULT,
October 11, 2012 at 2:28 am
hillerz19 (10/10/2012)
Hi Sean Lange,This is my coding so far but allot of errors:
ALTER TABLE ServiceTable
ALTER COLUMN serviceCode char(4) NOT NULL,
ALTER TABLE CaseTable
DROP CONSTRAINT serviceCodeValues,
ALTER TABLE CaseTable
ALTER serviceCode DROP DEFAULT,
Firstly on the CaseTable I dont see a column called servicecode, or a constraint called servicecodevalues so the code will fail.
They are however there is a Constraint called ServiceCodeValues on the ServiceTable, but no Default value, You also have an FK reference on the serviceRenderedTable which uses the Servicecode on the Servicetable.
1) Drop FK Constraint on ServiceRenderedTable
2) Drop servicecovevalues Constraint on ServiceTable
3) Alter Both Servicecode Column in Servicetable AND ServiceRenderedTable
4) Update data in BOTH tables for new pattern, adding an X to the beginning of the servicecode column
5) Reapply the Check Constraint on the Service table.
6) Reapply FK Constraint on the ServiceRenderedTable
One thing you an do is change the check to be : Check(serviceCode like '[X][C|E|O][0-9][0-9]') which is bit more readable.
Make sure you test this in a dev environment.
I dont think I've missed any steps.
_________________________________________________________________________
SSC Guide to Posting and Best Practices
October 11, 2012 at 3:14 am
hi,
Thanks Allot. I would follow your steps and wish i could make it out.
for to your question,I declare a CONSTRAINT ServiceCodeValues in my serviceTAble. and not sure if i drop me contraint by the Constraint name or by the column.
October 11, 2012 at 3:37 am
I'm happy to have a look at the code you create and give it a sanity check.
In response to your question, you need to drop it by the constraint name, If you goto ssms and locate the constraint on the table that you want to drop, right click and script as drop you'll see how it should be dropped.
I would also consider using a naming standard for the constraints, I personally use the following prefixes
DF : Default
CK : Check
FK : ForeignKey
PK : PrimaryKey
UQ : Unique
Such that the name of the Check constraint on the Servicetable for serviceCode would read
CK_ServiceTable_ServiceCode
The FK name would be
FK_ServiceRenderedTable_ServiceTable_ServiceCode
That way I can tell what a constraint is doing and which column/object is affected without having to script the constraint.
_________________________________________________________________________
SSC Guide to Posting and Best Practices
October 11, 2012 at 4:08 am
gud one Jason.....
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
October 12, 2012 at 3:15 am
Thanks Allot Old Hand,
I am just following a book for the coding. Would follow the what you have suggested. And thanks for the steps. It does helps me on my way but still having error. will try on my own with the steps till i drop.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply