简体   繁体   English

如何验证这些列?

[英]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. 就像不使用信用卡付款一样,应将CreditCardNumber,CardHoldersName和CreditCardExpDate设置为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 如果使用信用卡,则CreditCardExpDate值应大于PaymentDate PaymentDue可以允许为NULL,但不应大于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) 我为payment_date创建了一个触发器,但是我只能在插入的日期大于当前日期时触发它,如果它小于结束日期(结束日期在另一个表上),则需要它触发

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. 在Payment.Payments上创建触发器付款日期,以声明形式发送@@@@@ ModifiedDate日期SELECT @@@@ ModifiedDate = Payment_Date from插入的IF(@@@ ModifiedDate> getdate())BEGIN PRINT'修改后的日期应为当前日期。 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): 我在这里读到很多东西,但是我这就是您想要的(注意,尽管我使用了dbo模式):

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. 信用卡号的数据类型int有点矛盾。 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 ). 一个int的最大值为2,147,483,647,并且卡号由4组4位数字组成(即9999 9999 9999 9999 )。 Even as a number, that's far higher than the max value of an int . 即使是数字,也远远高于int的最大值。 I'd suggest using a char(19) and making a constraint on the format as well. 我建议使用char(19)并对格式进行限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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