Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

would need some help Expand / Collapse
Author
Message
Posted Wednesday, October 10, 2012 10:40 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, October 20, 2014 8:45 AM
Points: 11, Visits: 34
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
Posted Wednesday, October 10, 2012 10:45 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:04 PM
Points: 13,086, Visits: 12,553
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
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1371039
Posted Wednesday, October 10, 2012 10:49 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, October 20, 2014 8:45 AM
Points: 11, Visits: 34
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
Posted Wednesday, October 10, 2012 12:17 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:04 PM
Points: 13,086, Visits: 12,553
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
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1371076
Posted Wednesday, October 10, 2012 8:28 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, October 20, 2014 8:45 AM
Points: 11, Visits: 34
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
Posted Thursday, October 11, 2012 2:28 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, October 3, 2014 9:25 AM
Points: 895, Visits: 2,432
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
Posted Thursday, October 11, 2012 3:14 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, October 20, 2014 8:45 AM
Points: 11, Visits: 34
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
Posted Thursday, October 11, 2012 3:37 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, October 3, 2014 9:25 AM
Points: 895, Visits: 2,432
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
Posted Thursday, October 11, 2012 4:08 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, October 20, 2014 4:54 AM
Points: 1,921, Visits: 2,345
gud one Jason.....


_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1371329
Posted Friday, October 12, 2012 3:15 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, October 20, 2014 8:45 AM
Points: 11, Visits: 34
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 »

Add to briefcase

Permissions Expand / Collapse