Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
SQL Server Newbies
»
would need some help
would need some help
Rate Topic
Display Mode
Topic Options
Author
Message
hillerz19
hillerz19
Posted Wednesday, October 10, 2012 10:40 AM
Forum Newbie
Group: General Forum Members
Last Login: Saturday, October 13, 2012 7:12 AM
Points: 6,
Visits: 19
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
Post #1371036
Sean Lange
Sean Lange
Posted Wednesday, October 10, 2012 10:45 AM
SSCrazy Eights
Group: General Forum Members
Last Login: Yesterday @ 1:17 PM
Points: 8,641,
Visits: 8,273
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 Moden's
splitter
.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1371039
hillerz19
hillerz19
Posted Wednesday, October 10, 2012 10:49 AM
Forum Newbie
Group: General Forum Members
Last Login: Saturday, October 13, 2012 7:12 AM
Points: 6,
Visits: 19
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
Post #1371040
Sean Lange
Sean Lange
Posted Wednesday, October 10, 2012 12:17 PM
SSCrazy Eights
Group: General Forum Members
Last Login: Yesterday @ 1:17 PM
Points: 8,641,
Visits: 8,273
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 Moden's
splitter
.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1371076
hillerz19
hillerz19
Posted Wednesday, October 10, 2012 8:28 PM
Forum Newbie
Group: General Forum Members
Last Login: Saturday, October 13, 2012 7:12 AM
Points: 6,
Visits: 19
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,
Post #1371176
Jason-299789
Jason-299789
Posted Thursday, October 11, 2012 2:28 AM
SSC Eights!
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 1:30 AM
Points: 803,
Visits: 2,124
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
Post #1371280
hillerz19
hillerz19
Posted Thursday, October 11, 2012 3:14 AM
Forum Newbie
Group: General Forum Members
Last Login: Saturday, October 13, 2012 7:12 AM
Points: 6,
Visits: 19
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.
Post #1371300
Jason-299789
Jason-299789
Posted Thursday, October 11, 2012 3:37 AM
SSC Eights!
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 1:30 AM
Points: 803,
Visits: 2,124
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
Post #1371309
kapil_kk
kapil_kk
Posted Thursday, October 11, 2012 4:08 AM
Ten Centuries
Group: General Forum Members
Last Login: Sunday, May 19, 2013 11:16 PM
Points: 1,061,
Visits: 1,151
gud one Jason.....
Post #1371329
hillerz19
hillerz19
Posted Friday, October 12, 2012 3:15 AM
Forum Newbie
Group: General Forum Members
Last Login: Saturday, October 13, 2012 7:12 AM
Points: 6,
Visits: 19
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.
Post #1371967
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.