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 last year.
Improve this question
I want to make auto increment here with the table creation but it gives an error. The database is an oracle database. The SQL is shown below.
CREATE TABLE Continents
(
ConId INT GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1) PRIMARY KEY,
Continent VARCHAR(25),
);
Just remove this part
(START WITH 1, INCREMENT BY 1)
Use this syntax.
CREATE TABLE Continents
(
ConId INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
Continent VARCHAR(25),
);
The value of ConId will start at 1 (one) and always increment by one.
Refer to this db<>fiddle
Also refer to Oracle documentation1
The following statement creates a table t1 with an identity column id. The sequence generator will always assign increasing integer values to id, starting with 1.
CREATE TABLE t1 (id NUMBER GENERATED AS IDENTITY);
1SQL Language Reference (Oracle 21c) - CREATE TABLE
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 details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How can an algorithm is written to find Primary Keys and Foreign Keys of a relation ?
Given parameters are name of relation, degree of relation and an array of the attributes(a primary key may include more than one attribute)
I think if an attribute is referenced than it is primary key. and the attirbute that references is FK
I would make a list of columns that contain no redundancies and no null cells as possible primary keys. I suppose one fast way to detect them would be to attempt to declare the column as a PK and see if there are errors. Another approach would be a group by:
select column_name, count(*) c from table_name
where column_name is not null
group by c
having c <> 1
If column_name of table_name is plausibly a primary key, the query above should produce no rows.
As for foreign keys, try this:
select column_name from table_name
except
select other_column from other_table
This should return an empty set if other_column of other_table has column_name of table_name as a foreign key.
As for automating the above tests over all the tables and each of their columns, I can't help there as my SQL vocabulary doesn't include Microsoft.
Note that passing the tests above is a necessary but not sufficient condition for a column to be a key. Determining which columns should be keys is as much a matter of intuition as algorithms, and if your starting point is taming a messy collection of raw data there may be columns that fail the tests but should nevertheless be keys.
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 ?
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.