简体   繁体   中英

How do I validate these columns?

I'm working on a database and I need to validate some columns under the Payment schema.

Like if a credit card is not used for payments, CreditCardNumber, CardHoldersName, and CreditCardExpDate should be made NULL. If a credit card is used, the CreditCardExpDate value should be greater than the PaymentDate PaymentDue can allow NULL but should not be greater than PaymentAmount

I've searched online but what I get are complex triggers and procedures which are not really helpful.

  create table Payment.Payments(
  Payment_ID int identity (200, 21),
  Payment_Amount money constraint chk_Payment_Amount check (Payment_Amount > 
  '0'),
  Payment_Date date, -- is to be greater than the end date which is on another table
  Credit_Card_Number int,
  Card_Holders_Name char (50),
  Credit_Card_Expiry_Date date, 
  Project_ID int Foreign Key references ProjectDetails.Projects(Project_ID),
  Payment_Due money -- should not be greater than Payment Amount but 
  can still accept null*
   );

The notes show the current validation problem i'm having.

I created a trigger for the payment_date but i can only get it to fire when the inserted date is greater than the current date, i need it to fire if it is less than the end date(end date is on another table)

CREATE TRIGGER paymentdate ON Payment.Payments FOR INSERT AS DECLARE @ModifiedDate date SELECT @ModifiedDate = Payment_Date FROM Inserted IF (@ModifiedDate > getdate()) BEGIN PRINT 'The modified date should be the current date. Hence, cannot insert.' ROLLBACK TRANSACTION END

I'm reading a lot between the lines here, but I think this is what you're after (Note I have used the dbo schema though):

USE Sandbox;
GO

CREATE TABLE dbo.Payments (
    Payment_ID int identity(200, 21),
    Payment_Amount money CONSTRAINT chk_Payment_Amount CHECK (Payment_Amount > '0'),
    Payment_Date date,
    Credit_Card_Number char(19), --note datatype change from int to char. See my comment below (copied from my comment)
    Card_Holders_Name varchar (50), --note I've used varchar instead. Names aren't all 50 characters long
    Credit_Card_Expiry_Date date,
    --Project_ID int FOREIGN KEY REFERENCES ProjectDetails.Projects(Project_ID) --Commented out as I don't have this table
    Payment_Due money CONSTRAINT chk_Payment_Due CHECK (Payment_Due > '0' OR Payment_Due IS NULL)
    );
GO

--Credit Card format validation
ALTER TABLE dbo.Payments ADD CONSTRAINT ck_Credit_Card CHECK (Credit_Card_Number LIKE '[0-9][0-9][0-9][0-9] [0-9][0-9][0-9][0-9] [0-9][0-9][0-9][0-9] [0-9][0-9][0-9][0-9]' OR Credit_Card_Number IS NULL);

--Add card details must be there, or none.
ALTER TABLE dbo.Payments ADD CONSTRAINT ck_Card_Details CHECK ((Credit_Card_Number IS NULL AND Card_Holders_Name IS NULL AND Credit_Card_Expiry_Date IS NULL)
                                                           OR  (Credit_Card_Number IS NOT NULL AND Card_Holders_Name IS NOT NULL AND Credit_Card_Expiry_Date IS NOT NULL))

GO
DROP TABLE dbo.Payments;

Comment made on the Card Number's datatype:

The datatype int for a credit card number is a bit of an oxymoron. The maximum value for an int is 2,147,483,647 and a card number is made up of 4 sets of 4 digit numbers (ie 9999 9999 9999 9999 ). Even as a number, that's far higher than the max value of an int . I'd suggest using a char(19) and making a constraint on the format as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM