Alter sequence to be linked to another table - sql

So I'm uploading some 150,000 rows of data into a database over HTTP via Python backend, and the upload takes a while, thus I'm inserting it into a new table which I then swap with (by renaming) the old table:
create table tmp (like main);
alter sequence main_id_seq restart;
alter table tmp alter column id set default nextval('main_id_seq');
drop table main cascade; -- THIS REMOVES THE SEQUENCE ^^^^^^^
alter table tmp rename to main;
How can I alter the sequence to not be linked to the main table, so that when I drop the main table, the sequence would stay linked to the current tmp table (new main)?

You can do that by making the column "owning" the sequence
alter sequence main_id_seq
owned by main.id;

use alter sequence:
t=# create table s120(i bigserial);
CREATE TABLE
t=# \d+ s120;
Table "public.s120"
Column | Type | Modifiers | Storage | Stats target | Description
--------+--------+--------------------------------------------------+---------+--------------+-------------
i | bigint | not null default nextval('s120_i_seq'::regclass) | plain | |
t=# create table s121(i bigint);
CREATE TABLE
t=# alter sequence s120_i_seq owned by s121.i;
ALTER SEQUENCE
t=# drop table s120;
DROP TABLE
t=# alter table s121 alter COLUMN i set default nextval('s120_i_seq'::regclass);
ALTER TABLE
t=# \d+ s121
Table "public.s121"
Column | Type | Modifiers | Storage | Stats target | Description
--------+--------+-----------------------------------------+---------+--------------+-------------
i | bigint | default nextval('s120_i_seq'::regclass) | plain | |

Related

Why DROP INDEX IF EXISTS generates a MySQL note

I'm using MariaDB and I have a sql file which looks like this:
create table table1(
id int auto_increment primary key,
name varchar(50))
);
drop index if exists my_index on table1;
create fulltext index my_index on table1(name);
When i run this file with source file.sql it generates this warning:
+-------+------+--------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+--------------------------------------------------------------------------+
| Note | 1091 | Can't DROP 'my_index'; check that column/key exists |
+-------+------+--------------------------------------------------------------------------+
Is there a way to take this warning away?
This is what I have founded here: https://mariadb.com/kb/en/drop-index/
If the IF EXISTS clause is used, then MariaDB will return a warning
instead of an error if the index does not exist.
Hope this helps.

Kudu table comments not showing up. What should I do?

This is my create statement for impala-shell:
CREATE TABLE IF NOT EXISTS tmp.demo0011(
uid Bigint, comment'用户uid'
nick String, comment'昵称'
primary key(uid)
)
partition by hash(uid) partitions 128
stored as kudu
tblproperties (
'kudu.master_addresses'='10.10.10.1'
);
When I enter desc tmp.demo0011; I get:
+------+--------+---------+
| name | type | comment |
+------+--------+---------+
| uid | bigint | |
| nick | string | |
+------+--------+---------+
The comment section is nothing. Why?
Thanks.
Okay, I know why.
When I entered:
alter table demo0011 change uid uid bigint comment 'test comment';
I get:
ERROR: AnalysisException: Kudu does not support column comments.
CREATE TABLE test_db.babylist
(
id INT COMMENT 'Comment of ID Column',
patient_name STRING,
mother_name STRING,
Primary key (id)
)
PARTITION BY HASH(id) PARTITIONS 3
COMMENT 'Comment of Table'
STORED AS KUDU;
ALTER TABLE test_db.babylist CHANGE patient_name patient_name string COMMENT 'Comment of Column-patient_name';
DESCRIBE test_db.babylist;
ALTER TABLE test_db.babylist ALTER COLUMN id SET COMMENT 'Comment of Column-id';
DESCRIBE test_db.babylist
ALTER TABLE stest_db.babylist ADD COLUMN IF NOT EXISTS father_name string;
DESCRIBE test_db.babylist;
ALTER TABLE test_db.babylist DROP COLUMN mother_name;
DESCRIBE test_db.babylist;
ALTER TABLE test_db.babylist ALTER COLUMN father_name SET COMMENT 'Comment of Column-father_name';

Reset INCREMENT VALUE For Identity Column using T-SQL Script

I Need Change Increment Value for Identity Column
For Example
I have create table Test Table with Identity Column
Create Table test
(
Id Int Identity(1,1)
,Name Varchar(200)
)
Now it is easy to Reseed the start value of Identity Column using
DBCC CheckIdent('TEST',Reseed,100)
But I want to change the Increment value 1 to 10
is there any sql command that will work ..
While changing from SSMS 2016 I get this error
To change the increment you need to drop the existing identity() column and add a new column.
alter table test drop column Id;
alter table test add Id int identity(100,10);
If you want to keep existing values, then you will need to create a new table, insert the existing rows with identity_insert on, drop the old table, and rename the new table.
For example:
create table test (id int identity(1,1), name varchar(200) default '')
insert into test default values
insert into test default values
create table new_test (id int identity(100,10), name varchar(200) default '');
set identity_insert new_test on;
insert into new_test (id,name)
select id,name from test
set identity_insert new_test off;
drop table test;
exec sp_rename 'new_test','test';
insert into test default values;
insert into test default values;
select * from test;
rextester demo: http://rextester.com/XDE9355
returns:
+-----+------+
| id | name |
+-----+------+
| 1 | |
| 2 | |
| 100 | |
| 110 | |
+-----+------+

copy table (create table like) - not keeping auto incrementing primary key

I'm new to postgres (on 9.5) and I can't find this in the docs anywhere.
Basically create a table like this:
CREATE TABLE test (
id serial primary key,
field1 CHARACTER VARYING(50)
);
Then copy it:
create table test_copy (like test);
The table test has these columns:
COLUMN_NAME id field1
DATA_TYPE 4 12
TYPE_NAME serial varchar
COLUMN_SIZE 10 50
IS_NULLABLE NO YES
IS_AUTOINCREMENT YES NO
But test_copy has these:
COLUMN_NAME id field1
DATA_TYPE 4 12
TYPE_NAME int4 varchar
COLUMN_SIZE 10 50
IS_NULLABLE NO YES
IS_AUTOINCREMENT NO NO
Why am I losing serial and autoincrement? How can I make a copy of a table that preserves these?
This is because serial isn't really a datatype. It gets "expanded" to an integer + a sequence + a default value.
See the manual for details
To get the default definition you need to use create table test_copy (like test INCLUDING DEFAULTS).
However, that will then use the same sequence as the original table.
You can see the difference when you display the table definition in psql:
psql (9.5.3)
Type "help" for help.
postgres=> CREATE TABLE test (
postgres(> id serial primary key,
postgres(> field1 CHARACTER VARYING(50)
postgres(> );
CREATE TABLE
postgres=> create table test_copy_no_defaults (like test);
CREATE TABLE
postgres=> create table test_copy (like test including defaults);
CREATE TABLE
postgres=> \d test
Table "public.test"
Column | Type | Modifiers
--------+-----------------------+---------------------------------------------------
id | integer | not null default nextval('test_id_seq'::regclass)
field1 | character varying(50) |
Indexes:
"test_pkey" PRIMARY KEY, btree (id)
postgres=> \d test_copy
Table "public.test_copy"
Column | Type | Modifiers
--------+-----------------------+---------------------------------------------------
id | integer | not null default nextval('test_id_seq'::regclass)
field1 | character varying(50) |
postgres=> \d test_copy_no_defaults
Table "public.test_copy_no_defaults"
Column | Type | Modifiers
--------+-----------------------+-----------
id | integer | not null
field1 | character varying(50) |
you can try:
create table test_inh () inherits (test);
and then
alter table test_inh no inherit test;
should leave same sequence default value for you

Postgres detach a primary key from a sequence

how can i detach a primary key of table from a sequence with out having to drop the table
With "detach" you mean probably, removing the default for the column to the next value of the sequence.
For example, say you have a table definition like this:
Column | Type | Modifiers
------------+---------+----------------------------------------------------------------
yourcolumn | integer | not null default nextval('yourtable_yourcolumn_seq'::regclass)
you want to remove this part: default nextval('yourtable_yourcolumn_seq'::regclass)
If so, you can do it with this statement:
ALTER TABLE yourtable ALTER COLUMN yourcolumn DROP DEFAULT;