I have a table like this:
ID Name Email Referred_by
-------------------------------------------
1 John john#foo.com NULL
2 Sam sam#foo.com john#foo.com
3 Sally sally#foo.com sam#foo.com
..... more rows .....
And I would like to change it to:
ID Name Email Referred_by
-------------------------------------------
1 John john#foo.com NULL
2 Sam sam#foo.com 1
3 Sally sally#foo.com 2
..... more rows .....
Can I make this change using SQL?
This should do the trick:
UPDATE my_table a
set a.referred_by = (
SELECT b.id
FROM my_table b
WHERE b.email = a.referred_by
);
Many DBMS' will allow this by the use of DDL (definition) statements rather than DML (manipulation). Assuming that id is an integral type and referred_by is (currently) a textual column, you can do something like:
alter the table to add a new column ref2 (nullable) of the same type as id.
set up a foreign key constraint between that ref2 and id.
populate the ref2 column for all the rows.
drop the original constraint.
alter the table to remove column referred_by.
alter the table to rename column ref2 to referred_by.
Related
lets say i have table like this (new_table) (all field is not-null constraints):
id name km_unit
1 honda 1000
2 toyota 2000
3 bmw 1000
4 wuling 1500
i want to update the table with insert with this query:
insert into new_table(id,km_unit) values
(1,20000),
(2,20000),
(3,200000),
(4,200000)
ON CONFLICT (id)
DO
update SET km_unit = EXCLUDED.km_unit
but it return error like this :
null value in column "name" violates not-null constraint
the question is how to update the existing km_unit field if the id is same with values that i inserted?
can i update the table without writing name field in the values?
It seems you don't actually want to insert anything, so use an UPDATE statement:
update new_table dl
set km_unit = v.km_unit
from (
values
(1,20000),
(2,20000),
(3,200000),
(4,200000)
) as v(id, km_unit)
where v.id = dl.id
NOT NULL constraint
When the NOT NULL constraint is defined for a column, a row containing the null value in that column cannot be added, nor can a row be updated so as to set the null value in that column. A column for which the NOT NULL constraint is defined must have a definite value in every row. An attempt to set the null value in such a column results in a constraint violation.
In your case it means the column NAME should have a value.
In DB2 on Linux v11.1 I have a table:
COL1 COL2 "COLn 50 more columns"
A A
A A
B A
B B
etc 3 million rows
There can be multiple rows with the same rows, like first two rows in my sample (so obvious there is no primary key on table).
Now I have to add new column ID and set for every row unique sequential number.
The result should be:
COL1 COL2 "COLn 50 more columns" ID
A A 1
A A 2
B A 3
B B 4
etc 3 million rows
How to write such an update statement to update ID column?
Regards
Here is one way to do it, using an identity column , and it assumes that there is not an existing Primary Key or identity column.
alter table myschema.mytab add column id integer not null default 0 ;
alter table myschema.mytab alter column id drop default ;
alter table myschema.mytab alter column id set generated always as identity ;
update myschema.mytab set id = default ;
-- optional, if you want the new ID column to be a surrogate primary key
alter table myschema.mytab add constraint pkey primary key(id) ;
reorg table myschema.mytab ;
runstats on table myschema.mytab with distribution and detailed indexes all;
Try this:
alter table myschema.mytab add column id integer not null default 0 ;
UPDATE (SELECT ID, ROWNUMBER() OVER() RN FROM myschema.mytab) SET ID = RN;
-- Or even simplier:
-- UPDATE myschema.mytab SET ID = ROWNUMBER() OVER();
SQL table:
id | name
----+--------
1 | apple
2 | orange
3 | apricot
The id is primary key, unique, could be SERIES. The goal is to insert new row where id equals 2 and shift existing row numbers below, that is 2 and 3, to 3 and 4 position.
I have tried shift rows before inserting new row:
"UPDATE some_table SET id = id + 1 WHERE id >= id"
but an error occurred:
org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "some_table_pkey"
Detail: Key (id)=(3) already exists.
Is there some effective way to do such an operation?
The table should look like this after update:
id | name
----+--------
1 | apple
2 | cherry
3 | orange
4 | apricot
While I think the attempt is futile, you can achieve that by marking the primary key constraint as deferrable:
CREATE TABLE some_table
(
id int,
name text
);
alter table some_table
add constraint pk_some_table
primary key (id)
deferrable initially immediate; --<< HERE
In that case the PK constraint is evaluated at per statement, not per row.
Online example: https://rextester.com/JSIV60771
update Names
set id=id+1
where id in
(select id from Names where id>=2 order by id desc);
Here first you can update the id's and then you can insert
insert into Names (id,name) values(2,'cheery')
I want to add a column named 'index' in my existing SQL table, my_table.
How to do it?
Original my_table:
name sex age
Ben M 23
May F 20
Sam M 22
Desired my_table:
name sex age id
Ben M 23 1
May F 20 2
Sam M 22 3
INDEX is a resrve word so it is not best practice to use that to create index but you use that in naming below way
create table mytable( id int,name varchar(100) );
ALTER TABLE [mytable] DROP COLUMN ID
ALTER TABLE [mytable] ADD ID INT IDENTITY(1,1)
It is possible to create like that:
ALTER TABLE dbo.my_table
ADD [index] INT
However, as said #Zaynul Abadin Tuhin, it is not best practice.
If you want to create an identification column then just use the following code to create unique column for each row:
ALTER TABLE dbo.my_table
ADD ID INT IDENTITY
CONSTRAINT PK_my_table PRIMARY KEY CLUSTERED
OUTPUT:
Name Age ID
Ben M 23 1
May F 20 2
Sam M 22 3
I have an auto-increment primary key in a SQL table lets say table look like this:
CREATE TABLE [Table] (--Identifier contains a space and uses a reserved keyword.
[ID] [int] IDENTITY(1,1) NOT NULL ,
[Name] [varchar](50) NULL,
CONSTRAINT [PK__Table] PRIMARY KEY CLUSTERED ([ID] ASC)
);
ID | Name|
1 John
2 Jack
3 Bill
4 Joe
Then I delete row 2 Jack:
ID | Name|
1 John
3 Bill
4 Joe
And what I want to achieve is to change id column so the table will look like this
ID | Name|
1 John
2 Bill
3 Joe
Is there a way to do it?
I will never do that but you can:
create a new autoincrement primary key named ID2
delete ID column
rename ID2 column as ID
Quick and dirty way to do it is, (my way) -->
select * into yournewtable from youroldtable order by yourIdentityColumn;
Then, open up yournewtable's design, make sure yourIdentityColumn is Identity(1,1).
Then, drop youroldtable.
Then, rename yournewtable to youroldtable! ta-da!
Set identity_insert Table off;
Update Table set ID = 3 where ID = 4;
...
Set identity_insert Table on;
Where Table name is Table