Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to create constraint orderDate must be smaller than deliveryDate?
Help me.
Supposing the table name is MyTable:
ALTER TABLE [dbo].[MyTable] WITH CHECK
ADD CONSTRAINT [CK_MyTable_date1] CHECK (orderDate <= deliveryDate)
ALTER TABLE [dbo].[MyTable] CHECK CONSTRAINT [CK_MyTable_date1]
GO
There are two ways to do it.
First While creating the table and after the creation:
While creating the table:
CREATE TABLE Price (
PriceID INT PRIMARY KEY IDENTITY (1,1),
OriginalPrice FLOAT NOT NULL,
CurrentPrice FLOAT NOT NULL,
Discount FLOAT,
ShippingCost FLOAT NOT NULL,
Tax FLOAT NOT NULL,
CHECK (CurrentPrice <= OriginalPrice));
After creation the table:
ALTER TABLE Price ADD CHECK (CurrentPrice <= OriginalPrice);
--or
ALTER TABLE Price ADD CONSTRAINT CK_Price_Current_vs_Original
CHECK (CurrentPrice <= OriginalPrice);
You can go for the date fields in the same sense.
For more info please Read this.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am trying to write some SQL code. I have a table and I want to add values to that table Tasks, but I am getting an error
Invalid Column Name "Manager"
over Task_Name. I identified Id as auto-incrementing with IDENTITY(1,1) and I have a BIT type value as default 0.
How should I write the expression of insert into?
Here is the code for creating:
CREATE TABLE Tasks
(
ID INTEGER IDENTITY(1,1) NOT NULL PRIMARY KEY,
Task_Name VARCHAR(100),
Situation BIT DEFAULT 0
);
Here is the code for Insert Into:
INSERT INTO (ID, Task_Name, Situation)
VALUES (1, "Manager");
INSERT INTO Tasks (Task_Name) values ('Manager')
Don’t specify ID unless turning identity_insert on (column is marked as identity column)
Don’t include column names to insert default values
Use ' to quote strings
INSERT INTO Tasks (Task_Name, Situation) VALUES ('Task_NameValue', 1);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
A patient can select many tests, and a test can be selected by many patient.
Then what would be the structure of these tables and how the relationship can be established between them?
your table structure should be below
PatientTable
PatientId int Primary Key,
PatientName varchar(50),
EmailId varchar(50)
Password varchar(50)
TestTable
TestId int Primary key,
TestName varchar(50)
PatientTestTable
PatientId int FK(PatientTable)
TestId int FK (TestTable)
This way you can give relationship to two tables. you need to understand funamental of RDBMS.
You will probably need 3 tables, Patient table, Test Table and PatientTest Table
with PatientID as foreign key fro Patient table and TestId as foreign key from Test table and you can add any other column ( like TestDatetime, TestResult ...)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Is there any difference at all with the following three scripts, and which is best practice?
CREATE TABLE Test1 (
TestID INT IDENTITY(1,1) NOT NULL,
Description NVARCHAR(50) NOT NULL,
CONSTRAINT [PK_AnyNameIFancy] PRIMARY KEY CLUSTERED
(
TestID ASC
)
) ON PRIMARY;
CREATE TABLE Test1 (
TestID INT NOT NULL PRIMARY KEY IDENTITY,
Description NVARCHAR(50) NOT NULL) ON PRIMARY
The only difference is that you are naming the constraint in the first one - that is a very good practice to follow.
They both end up with a clustered unique index.
For single column constraints I tend to write it on the one line...
TestID INT NOT NULL IDENTITY CONSTRAINT PK_AnyNameIFancy PRIMARY KEY CLUSTERED
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I tried to create a table which has got 2 primary key and both of them supposed to be auto incremental by changing the Identity specification but in the property window the option (I just mentioned) is disable and I cannot change that.
The column I want to change to auto increment is the primary key of the table and type of it is INT.
What's wrong ? or What I'm wrong ?
As mentioned by TT, you can only have a single identity column and only a single primary key in a table.
For SQL Server 2012 and above, You can however use a SEQUENCE if you need 2 or more auto increment columns. You can then create a PRIMARY KEY on IDENTITY column and a UNIQUE constraint on the column with sequence as default
CREATE SEQUENCE mainseq START WITH 1 INCREMENT BY 1 ;
GO
create table table1 (
id1 int IDENTITY(1,1) NOT NULL PRIMARY KEY,
id2 bigint not null constraint DF_table1_id2 default next value for mainseq,
data varchar(20) not null
)
GO
INSERT INTO table1(data) VALUES('row 1');
INSERT INTO table1(data) VALUES('row 2');
SELECT * FROM table1;
Note that the column with sequence default behaves in differently than an identity. you cannot directly insert into an identity column (except when identity_insert is on) however you can manually insert / update the column being defaulted by a sequence.
From the definition of IDENTITY in SQL Server, you can read that a table can only have one IDENTITY column. See remarks, quote:
Only one identity column can be created per table.
I also wonder how you get two primary keys on a table, as that is not possible. See the following article on creating primary keys, quote from Limitations and Restrictions:
A table can contain only one PRIMARY KEY constraint.
If you need an additional auto incrementing column you could add a trigger to the table FOR INSERT. Something that would find the current maximum value and then add one to it.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I have a table in SQL Server 2008 - let's call the table MyTable. The table has a column named Status, which is not a computed column, and is defined as varchar(40) and it allows NULLs; however, there is also a DEFAULT CONSTRAINT on this column, with the default value = 'POOL'. I just added a computed column to the table, using the following:
ALTER TABLE MyTable
ADD PrimaryStatus AS
CASE
WHEN Status LIKE '%/%' THEN LEFT(Status,CHARINDEX('/', Status) - 1)
ELSE Status
END PERSISTED
If I insert records into the table one by one (and let Status default to 'POOL' using the constraint) it works just fine; for instance, this SQL statement has no problem:
INSERT INTO MyTable (Name) VALUES ('Foo')
With the above SQL, I end up with a new record in the table with Name = 'Foo' and Status = 'POOL' and PrimaryStatus = 'POOL'
But if I execute a multi-row INSERT like the following:
INSERT INTO MyTable (Name) VALUES ('Foo'),('Bar')
then it throws an error:
Msg 537, Level 16, State 2, Line 1
Invalid length parameter passed to the LEFT or SUBSTRING function.
If I drop either the default constraint or the computed column (or both), the multi-row INSERT works fine; but for some reason having both the constraint and the computed column are causing the multi-row INSERT to fail. I have tried tweaking the computed column in a variety of ways to account for NULLs (even though I don't think it should matter given the order of evaluation), but nothing seems to remedy the problem.
Anybody ever seen something like this before?
I tried to replicate the error. But, I don't get any error. I could have added this as a comment, but don't have enough points yet. Anyway, this is what i did -
CREATE TABLE [dbo].[MyTable](
[Name] [varchar](50) NULL,
[Status] [varchar](50) NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[MyTable] ADD CONSTRAINT [DF_MyTable_Status]
DEFAULT ('POOL') FOR [Status]
GO
Then i removed an extra ) in your code and did -
ALTER TABLE MyTable ADD PrimaryStatus AS
CASE WHEN Status LIKE '%/%' THEN LEFT(Status,CHARINDEX('/',Status)-1)
ELSE Status END PERSISTED
Followed by -
INSERT INTO MyTable (Name) VALUES ('Foo')
INSERT INTO MyTable (Name) VALUES ('Foo'),('Bar')
It works. Am I missing something ?