Home > database >  Add constraints to a database table
Add constraints to a database table

Time:12-27

If the field=NaN, insert statement cannot insert invalid, constraints, how to write

CodePudding user response:

The ALTER TABLE TABLE name ADD the CONSTRAINT CHECK CONSTRAINT name (A<> 'NaN')

CodePudding user response:

NaN is the inside of the js, not Numbers,
The simplest is to put the field type is set to int, bigint, etc., don't need to use constraints, this is the most reasonable approach,

If you must get into a string, you can refer to the following:
 USE tempdb for 
GO
IF OBJECT_ID (' t ') IS NOT NULL
DROP TABLE t
GO
CREATE TABLE (t
Id INT IDENTITY (1, 1) PRIMARY KEY,
N VARCHAR (50)
)
GO
The ALTER TABLE t ADD CONSTRAINT CK_t_n CHECK (ISNUMERIC (n)=1);
GO
INSERT INTO t (n) VALUES (' 123 ');
- (1 line affected)
GO
INSERT INTO t (n) VALUES (' ABC ');
/*
INSERT statement and the CHECK constraint "CK_t_n" conflict, the conflict in the database "tempdb for," table "dbo. T", the column 'n',
The statement has been terminated,
*/

CodePudding user response:

For application, of course, should not by the database to the last,
But in the save before validation, if the user input is not digital, direct hint: you input is not a number, that's fine,
Don't put all things into the database, the database should not suffer unnecessary tasks,
  • Related