How to remove auto increment of table column in sqlite? - iphone

i had create my table using below code:
create table tbl_questionnair(qid integer primary key autoincrement, title varchar, uid integer, superadminid integer,TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
Now i want to remove auto increment of qid. Anyone can help me?

sqLite doesnot support alter table so drop the older table and create it once again
Simply remove the qid as primary key.
create table tbl_questionnair(qid integer, title varchar, uid integer, superadminid integer,TIMESTAMP DEFAULT CURRENT_TIMESTAMP);

Related

How do I write this so that the Error: relation "photos" does not exist and same error for "users" does not occur

Im trying to make these two tables however the users and photo table both have a foreign key that refers to the other table and they come up with errors. How would I change this code so that there's no error.
create table people (
id serial,
family_names text,
given_names text,
displayed_names text,
email_address text,
primary key (id)
);
create table users (
userid integer,
website text,
date_registered date,
gender GenderValue,
birthday date,
password text,
portrait integer,
primary key (userid),
foreign key (userid) references people(id),
foreign key (portrait) references photos(photoid)
);
create table photos (
photoid serial,
title TitleValue,
date_uploaded date,
date_taken date,
description text,
technical_details text,
safety_level safetyLevel,
visibility visibilityLevel,
owns integer,
primary key (photoid),
foreign key (owns) references users(userid)
);
ER Diagram
Remove the "forward" reference from the table definition and add it as an alter table statement after all tables have been created:
alter table users add constraint fk_users_photos
foreign key (portrait) references photos(photoid);
In fact, one style of table creation is simply to create all tables with no foreign key references, and then to use alter table for all of them after the table definitions. This prevents any errors and allows circular references.
Here is a db<>fiddle. Note that your code has custom types which I changed to base types in the fiddle.

Are 'link tables' in postgresql supposed to automatically update when records are added to primary tables?

Noob question - Basically I'm trying to get a link table for a many-to-many relationship to auto update. I don't really know if this is what is supposed to happen, but I don't think I'm supposed to manually copy data into it either.
Below is my DDL - if glo.link_table is made this way, and I add data to both glo.collectors and glo.natural_hazards, shouldn't glo.link_table auto-update? Right now glo.link_table is not doing anything when I copy data into to glo.collectors and glo.natural_hazards. I'm guess I'm expecting glo.link_table to like add records when I add data to those two tables.
--Drop if exsits & create
DROP TABLE IF EXISTS glo.collectors CASCADE;
-- Make collectors table
CREATE TABLE glo.collectors (
u_id serial,
user_name text NOT NULL,
password text NOT NULL,
email text NOT NULL,
agency text NOT NULL,
PRIMARY KEY (u_id)
);
--Drop if exsits & create
DROP TABLE IF EXISTS glo.natural_hazards;
-- Create natural hazards table
CREATE TABLE IF NOT EXISTS glo.natural_hazards(
nathaz_id SERIAL,
data_version text,
data_class text, -- class = foundational
agency text NOT NULL,
data_owner text,
data_url text NOT NULL,
keywords text,
spatial_ref text,
creation_date text,
data_description text,
data_purpose text,
fema_sfha text,
event_inundation text,
video_catalog text,
hazard_scale text,
PRIMARY KEY (nathaz_id)
);
--Drop if exsits & create
DROP TABLE IF EXISTS glo.link_table;
-- Create link table
CREATE TABLE IF NOT EXISTS glo.link_table(
u_id int REFERENCES glo.collectors (u_id) ON UPDATE CASCADE ON DELETE CASCADE,
nathaz_id int REFERENCES glo.natural_hazards (nathaz_id) ON UPDATE CASCADE,
CONSTRAINT link_table_pkey PRIMARY KEY (u_id, nathaz_id)
);
No the link_table will not auto populate. Those columns will act in a similar way to a 'foreign key' and ensure that there is some form of constraint on what can be inserted into these columns.
Data that already exists in the link_table will be affected by changes to data in the tables that these columns reference. But this data will have to be inserted separately.
column references documentation
For automatically inserting data into the link_table I would suggest possibly using a trigger.
Trigger documentation

sqlite3 - copy autoincrement field to other column on INSERT

Is there a more performant way to copy the autoincrement field value to another field after row insert?
I'd like to make it automatic with triggers, but I haven't enough knowledge with them to be sure of my code.
this answer (for mysql) doesn't count for it is in the query, and i don't know if it would work with multiple rows
this is the table:
CREATE TABLE IF NOT EXISTS commenti (
id INTEGER PRIMARY KEY AUTOINCREMENT,
content TEXT COLLATE NOCASE,
idcommento INTEGER REFERENCES commenti (id)
ON DELETE CASCADE ON UPDATE CASCADE);
and this is the trigger i'm using: it copies id (the autoincrement field) into idcommento only if the value is null in the query.
CREATE TRIGGER set_comment_to_self AFTER INSERT ON commenti
WHEN NEW.idcommento IS NULL
BEGIN
UPDATE commenti SET idcommento = NEW.id WHERE id = NEW.id;
END;
I don't know if I can use some sort of easy NEW.idcommento = NEW.id istead of searching every time in all the table...

Timetravel in postgres - violating PRIMARY KEY constraint

I wanted to use timetravel function (F.39. spi, PostgreSQL 9.1 Documentation) in my application, however it doesn't seem to work properly for me. With inserting rows into table everything works just fine, I get start and stop date properly, but when I'm trying to update those rows postgres gives me error about violating of PRIMARY KEY constraint. He's trying to insert a tuple with the same primary id as previous tuple...
It's insane to remove primary key constraints from all tables in the database but it's the functionality I need. So maybe you have some expierience with timetravel?
Any sort of help will be appreciated. Thanks in advance.
DDL:
CREATE TABLE cities
(
city_id serial NOT NULL,
state_id integer,
name character varying(80) NOT NULL,
start_date abstime,
stop_date abstime,
CONSTRAINT pk_cities PRIMARY KEY (city_id ),
CONSTRAINT fk_cities_states FOREIGN KEY (state_id)
REFERENCES states (state_id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE NO ACTION
)
WITH (
OIDS=FALSE
);
-- Trigger: time_travel on cities
-- DROP TRIGGER time_travel ON cities;
CREATE TRIGGER time_travel
BEFORE INSERT OR UPDATE OR DELETE
ON cities
FOR EACH ROW
EXECUTE PROCEDURE timetravel('start_date', 'stop_date');
STATEMENT GIVEN:
INSERT INTO cities(
state_id, name)
VALUES (20,'Paris');
and that's ok. I get start_date and stop_date.
But by:
UPDATE cities SET name='Rome' WHERE name='Paris'
I get error- described earlier.
Schema of states
-- Table: states
-- DROP TABLE states;
CREATE TABLE states
(
state_id serial NOT NULL, -- unikatowy numer wojewodztwa
country_id integer, -- identyfikator panstwa, w ktorym znajduje sie wojewodztwo
name character varying(50), -- nazwa wojewodztwa
CONSTRAINT pk_states PRIMARY KEY (state_id ),
CONSTRAINT uq_states_state_id UNIQUE (state_id )
)
WITH (
OIDS=FALSE
);
Unfortunately,as a new user I'm not allowed to post images here.
You can see them there:
Sample data from table cities: korpusvictifrew.cba.pl/postgres_cities.png
Sample data from table states: korpusvictifrew.cba.pl/states_data.png
Time travel converts an UPDATE into an UPDATE of the old record's stop_date and an INSERT of a new one with the changed data plus an infinity stop_date. You can't have more than one record for city_id due to pk_cities. The time travel triggers do not allow you to break that requirement.
You cannot use this:
CONSTRAINT pk_cities PRIMARY KEY (city_id )
You must use this
CONSTRAINT pk_cities PRIMARY KEY (city_id, stop_date)

PostgreSQL: after import some data, if insert there is error - IntegrityError duplicate key value violates unique constraint "place_country_pkey"

When I import some data to PostgreSQL through PhpPgAdmin there is all fine.
But when I try later to insert some data to populated before tables I get an error:
IntegrityError: duplicate key value violates unique constraint "place_country_pkey"
And this is happens only with prepopulated tables.
Here is my SQL:
DROP TABLE IF EXISTS place_country CASCADE;
CREATE TABLE place_country (
id SERIAL PRIMARY KEY,
country_en VARCHAR(100) NOT NULL,
country_ru VARCHAR(100) NOT NULL,
country_ua VARCHAR(100) NOT NULL
);
INSERT INTO place_country VALUES(1,'Ukraine','Украина','Україна');
How to avoid this?
Thanks!
Try not inserting the "1". IIRC, in Postgres, when you define a column as SERIAL, it means that it will auto-generate an ID with a counter to automatically populate that column. So use:
INSERT INTO place_country (country_en, country_ru, country_ua) VALUES (Ukraine','Украина','Україна');
Which is a good practice anyway, BTW (explicitly naming the columns in an INSERT, I mean).