How to add Primary Key from Existing table in SQLite? - iphone

Now i am working in SQLite database. I want to add primary key from my existing table. But i couldn't add the primary key in existing table and i have deleted all the records in the old table. So how can i add the primary key from existing table in SQLite?
Here my query is,
alter table studetails add constraint pk primary key (rollno).
I have used that above query, but it shown error. Is it possible?, if yes, please guide me.
Thanks!

you cannot according to SQLite's syntax perform this operation. In general it seems strange to add a primary key to an existing table. This should be done using the create table statement (you can also add an index by create index).
Br
Anders
Alter table in SQLite
Create table in SQLite

Related

Query which will delete other table reference as well as from main table

I have a requirement of deleting records from the Postgres database tables.
We have a Customer table which is the main table, this table contains a primary key which is used in so many other tables as a FOREIGN KEY, I want to delete one of the customers as well as its reference used in other tables. Is there any way to delete the customer from main table as well as from other tables which contains foreign key.
Thanks in Advance.
In the other tables, you want a cascading delete foreign key reference. You can create one in the database using:
alter table othertable add constraint fk_othertable_customerid
foreign key (customerid) references customers(customerid)
on delete cascade;
Note: This assumes that customerid is the name of the column in both tables and that it is already defined in the other tables.
A cascading foreign key constraint does exactly what you specify. When a row is deleted in the reference table, then all related rows are deleted.
If you already have foreign key constraints on customerid, then drop the existing constraint and add the cascading version.

Do I have to insert the foreign key values manually or PostgreSQL will automatically copy the value of the primary key into the second table?

do i have to manually insert the FK to match the PK or this process can be automated? i'm using PostgreSQL.
if anyone can direct to me to a reference to this issue or a video tutorial explaining this, i would be much appreciated.

What is the use for Auto FK index in pgAdmin?

When creating a foreign key constraint in PostgreSQL from pgAdmin (1.12.2 in my case), the following option is checked:
Auto FK index
I would like to know if it's right to leave it checked all the time, and also understand how that overhead actually works.
For instance, the following constraint:
ALTER TABLE "user"
ADD CONSTRAINT fk_user_region FOREIGN KEY (intregionid)
REFERENCES region (intid) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION;
Creates the following index:
CREATE INDEX fki_user_region
ON "user"
USING btree
(intregionid);
Note that it creates an index only when creating the constraint from pgAdmin.
There is not much documentation about pgAdmin, and nothing specifically about that option.
Thank you.
This creates an Index for the Foreign Key column. By Default, the SGBD´s not created an index for FKs Columns (Index are created by default to Primary Keys and Unique Constraints).
This is a good practice to tuning your database.
Att,
set to No during creation process...
and then seems to be enabled by default:
so everything seems to be fine!

CREATE TABLE AS with PRIMARY KEY in one statement (PostgreSQL)

Is there a way to set the PRIMARY KEY in a single "CREATE TABLE AS" statement?
Example - I would like the following to be written in 1 statement rather than 2:
CREATE TABLE "new_table_name" AS SELECT a.uniquekey, a.some_value + b.some_value FROM "table_a" AS a, "table_b" AS b WHERE a.uniquekey=b.uniquekey;
ALTER TABLE "new_table_name" ADD PRIMARY KEY (uniquekey);
Is there a better way of doing this in general (assume there are more than 2 tables, e.g. 10)?
According to the manual: create table and create table as you can either:
create table with primary key first, and use select into later
create table as first, and use add primary key later
But not both create table as with primary key - what you wanted.
If you want to create a new table with the same table structure of another table, you can do this in one statement (both creating a new table and setting the primary key) like this:
CREATE TABLE mytable_clone (
LIKE mytable
INCLUDING defaults
INCLUDING constraints
INCLUDING indexes
);
No, there is no shorter way to create the table and the primary key.
See the command below, it will create a new table with all the constraints and with no data. Worked in postgres 9.5
CREATE TABLE IF NOT EXISTS <ClonedTableName>(like <OriginalTableName> including all)
well in mysql ,both is possible in one command
the command is
create table new_tbl (PRIMARY KEY(`id`)) as select * from old_tbl;
where id is column with primary key of old_tbl
done...
You may do this way
CREATE TABLE IOT (EMPID,ID,Name, CONSTRAINT PK PRIMARY KEY( ID,EMPID))
ORGANIZATION INDEX NOLOGGING COMPRESS 1 PARALLEL 4
AS SELECT 1 as empid,2 id,'XYZ' Name FROM dual;

django: manually adding a foreign key column (newcolumn_id_refs_id_4bfb2ece ?)

I need to add a foreign key field to an existing django model/postgres table. As per the django documentation, I ran the 'sqlall myapp' command to 'work out the difference'.
The obvious difference is that the table in question now has an extra column with a new contraint, which looks like this:
ALTER TABLE "myapp_mytable" ADD CONSTRAINT newcolumn_id_refs_id_4bfb2ece
FOREIGN KEY ("newcolumn_id") REFERENCES "myapp_theothertable" ("id")
DEFERRABLE INITIALLY DEFERRED;
Before messing with my database, I'd like to understand that statement, in particular, what does the last part of newcolumn_id_refs_id_4bfb2ece refer to?
Thanks,
Martin
It will make PostgreSQL understand and enforce your foreign key to the other table and guarantee there won't be anything in myapp_table.newcolumn that can't be found in myapp_theothertable.id
Actually, your django app will work just fine even without that constraint. However, it's a good idea to have one in place, and if you do afterwards a dumpdata - loaddata -cycle, it will be created.