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.
Related
I am terribly sorry if this is a supremely easy question. It's just such a weird case I have trouble even figuring out how to write that in google. I just can't. I'll describe the situation and what I want to do - I don't know how to put that as a question...
The situation is this. I have a mySQL table: service_logs. There I have the log_id as the primary key with AUTO_INCREMENT set to it. So all the various logs there have log_id 1, 2, 3, 4, 5 and so on with various data. But across the history, many individual logs were deleted. So now I have:
log_id 1: content
log_id 2: content
log_id 10: content
log_id 11: content
log_id 40: content
and so on.
I want to fill the gaps in that. I want to have the entry nr. 10 reassigned the 3rd number, then entry nr. 11 assigned the 4th number, and so on. I don't want to have gaps in there.
And yes I know it's dumb and shouldn't be done. I just have a friend who needs these without gaps for some of his Excel stuff :/
Yes this is a bad idea and not necessary as you can always add a row_number to get your wanted result to export it.
That said.
You can remove auto_increment and primary key
Renumber the column
Add autoincrement and primary key
and set the "correct" number
it must be clear at the time no further actions should be run with the server
If you only want consquitove numbers you can also choose toe "make a seciond" log table with row_numbers in it, it is the sample CREATE TABLE log2
CREATE TABLe log(log_id int PRIMARY KEY AUTO_INCREMENT)
ALTER TABLE log MODIFY log_id INT ;
ALTER TABLE log DROP PRIMARY KEY;
INSERT INTO log VALUES (1),(2),(5),(10)
CREATE TABLE log2 SELECT ROW_NUMBER() OVER(ORDER BY log_id ASC) log_id,'abs' FROm log
UPDATE log
JOIN (SELECT #rank := 0) r
SET log_id=#rank:=#rank+1;
SELECT * FROM log
| log_id |
| -----: |
| 1 |
| 2 |
| 3 |
| 4 |
ALTER TABLE log MODIFY log_id INT PRIMARY KEY AUTO_INCREMENT;
✓
SELECT #max := MAX(log_id)+ 1 FROM log;
PREPARE stmt FROM 'ALTER TABLE log AUTO_INCREMENT = ?';
EXECUTE stmt USING #max;
DEALLOCATE PREPARE stmt;
| #max := MAX(log_id)+ 1 |
| ---------------------: |
| 5 |
SELECT * FROM log2
log_id | abs
-----: | :--
1 | abs
2 | abs
3 | abs
4 | abs
db<>fiddle here
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';
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 | |
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;
Before you mark this a duplicate. I found this answer on another thread and having difficulties making it work.
From psql I see my table:
\d people
Table:
Column | Type | Modifiers
---------------+----------------------------------+-----------------------------------------------------------------------
id | integer | not null default
nextval('people_id_seq'::regclass)
Code I tried which seems to do nothing...
ALTER SEQUENCE people_id_seq RESTART 1000
How do I make the primary key start from 1000?
The following query would set the sequence value to 999. The next time the sequence is accessed, you would get 1000.
SELECT setval('people_id_seq', 999);
Reference:
Sequence Manipulation Functions on PostgreSQL Manual
Why are you declaring your id like that ?
I mean, I would do the following :
create table people(
id serial,
constraint primaryKeyID primary key(id));
And now if you want to start your sequence from 1000, your alter query will work.
alter sequence people_id_seq restart 1000