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';
Related
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
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 | |
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 | |
+-----+------+
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
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