I have to add a field1 in table1 as new column, while this field1 is already a primary key in table2. I have written below query:
alter table1 a
add field1 smallint,
default 5 not null
What's wrong in the above query? It's in sybase database.
Your syntax is a bit wrong.
It should be:
alter table table1
add field1 smallint
default 5 not null
Check out the docs here:http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.sqlug/html/sqlug/sqlug354.htm
Related
I have a table which already consists a number of records. The table has a column named 'id' of integer type and I have already inserted unique values in id column for all the records in the table.
Now I need to set IDENTITY constraint on the 'id' column so that in any new record added to the table, the value of id column gets inserted automatically incremented from the last value added to the column.
The create table query of the table is like follows:
create table Table1 (
Column1 varchar(255) not null,
Column2 varchar(254) not null,
Column3 int not null,
id int ,
PRIMARY KEY CLUSTERED ( id ) on 'default'
)
I insert data into the table using following query:
Insert into Table1 (Column1, Column2, Column3, id) values("abc","def",12,1)
But when I try to execute following Alter table query on my sybase database it returns an error "Incorrect Syntax near 'IDENTITY'"
ALTER TABLE Table1 MODIFY id int IDENTITY DEFAULT AUTOINCREMENT NOT NULL
Can anyone point me in the right direction about, how to apply IDENTITY constraint on a column which already has data?
There are 2 databases DB_Main and DB_Backup in SQL Server 2008.
I want to copy the data of Table1 from DB_Backup into Table1 of DB_Main.
The structure of all the tables in both the database is same. Both the tables in both the database have foreign key, primary key constraint.
When I try to copy data of Table1 from DB_Backup into Table1 of DB_Main with this query:
Insert into [DB_Main].[Table1]
Select *
from [DB_Backup].[Table1];
I get this foreign key error.
The INSERT statement conflicted with the FOREIGN KEY constraint
"FK_Table1_Table3". The conflict occurred in database "DB_Main",
table "Table3", column 'RequestID'.
Please let me know any simple way to copy all the records of Table1 from DB_Backup into Table1 of DB_Main, without violating any constraint?
Please reply
What this means is that for example you are trying to insert a record into Table1 which has for example RequestID = 75. The foreign key constraint means that there must be a record with RequestID = 75 in Table3.... and there currently isn't.
So this means you also need to load data into Table3
To find the records actually causing the issue run
Select DISTINCT RequestID from [DB_Backup].[Table1]
Some of these request ID's need a 'parent' record in Table3
To find the specific ones run
Select RequestID from [DB_Main].[Table3]
WHERE Request_ID NOT IN (
Select DISTINCT RequestID from [DB_Backup].[Table1]
)
You need to insert these into Table3:
insert into Table3(Request_ID, OtherColumn)
Select RequestID, OtherColumn from [DB_Backup].[Table3]
WHERE Request_ID NOT IN (
Select DISTINCT RequestID from [DB_Main].[Table3]
)
Then you can load your other records.
Drop or Disable constraint FK_Table1_Table3 before inserting and enable after it.
To Disable:
ALTER TABLE Table1 NOCHECK CONSTRAINT ALL
or
ALTER TABLE Table1 NOCHECK CONSTRAINT FK_Table1_Table3
Source
to Enable
ALTER TABLE Table1 CHECK CONSTRAINT ALL
or
ALTER TABLE Table1 CHECK CONSTRAINT FK_Table1_Table3
I've a table (SQL Server 2008) with a composite primary key. How can I prevent this behaviour?:
insert into myTable values ('A','B')
insert into myTable values ('B','A')
I tried with UNIQUE restriction and WITH_IGNORE_DUP_KEYS but I can't solve it.
Thanks in advance!
As SQL Server does not allow a function based index, one solution is to force inserting the values such that the "smaller" one is always stored in the first column:
alter table myTable add constraint chk_pk
check (col1_1 <= col_2);
If you don't like this (because you don't want to force a special "ordering" of the values during insert), you need to define two computed columns and create a unique index on them:
ALTER TABLE myTable ADD
min_pk AS case when col_1 < col_2 then col_1 else col_2 end,
max_pk AS case when col_1 > col_2 then col_1 else col_2 end;
CREATE UNIQUE INDEX idx_unique_pk ON myTable (min_pk, max_pk);
i think before insert you can check if combination 'B' and 'A' exists in both column
Declare #t1 table(col1 varchar(2),col2 varchar(2))
insert into #t1 values('A','B')
if not exists(select col1 from #t1 where ((col1='B' or Col1='A') and (col2='B' or Col2='A')))
insert into #t1 values('B','A')
select * from #t1
or you can define constraint.
Thanks for good question .
How to make a NULLABLE column to NOT NULL in SQL SERVER. I made a table with four columns:
My table:
id int null
fname varchar not null
last name not null
contact int null.
Now what i am required is to make id column to be not null.So please help me how we can make a null column to be a not null column.
Use the following statements to do it. Change the name of column id as required
UPDATE [Table] SET id=0 WHERE id IS NULL;
ALTER TABLE [Table] ALTER COLUMN id INTEGER NOT NULL;
Replace [Table] with the name of the table
first Update id Column '0'(Zero) where NULL then right Click on table then select Design and uncheck Allow Nulls checkbox of your Column from sql table design.
ALTER TABLE tableName
ALTER COLUMN columnName nvarchar(200) [NULL|NOT NULL]
How to Set a existing column of MS SQL table as NOT NULL?
ALTER TABLE tablename
ALTER COLUMN columnname datatype NOT NULL
You will obviously have to make sure that the column does not contain any NULL values before doing this.
E.g.
ALTER TABLE orders
ALTER COLUMN customer_id INT NOT NULL
Firstly ensure that the fields have non null values. In this case I'm working with a field that has a GUID nvarchar so I'll do
UPDATE tablename
SET fieldname = Newid()
WHERE fieldname IS NULL;
Then as Adam Ralph says
ALTER TABLE tablename ALTER COLUMN fieldname datatype NOT NULL