Change a Column to Primary Key in sqlite [closed] - sqlite

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a table in my app
CREATE TABLE VisitDetails (VisitId INTEGER, UserId TEXT, VisitNumber
TEXT, JobId INTEGER, JobNumber TEXT, StatusId INTEGER, Status TEXT,
SignatureRequired BOOL, StatusDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP
,StartDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP, EndDate TIMESTAMP
DEFAULT CURRENT_TIMESTAMP, IsPPMJob BOOL)
and it was working fine but now I need to alter this table to make already existing column 'VisitId' to become the primary key. Can somebody help me please? It might work by adding Unique constraint to VisitId column of the table but I am trying to put some efficient solution !

You can't change SQLite tables field's primary key once the table is created,
Possible solution is,
Create new table with desired primary key
Copy all data
Drop old table
Here is the documentation link : https://www.sqlite.org/omitted.html

Related

How do you insert into an sqlite table with foreign key [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Hello I need help with an sqlite/Qt problem. The schema for the table is
CREATE TABLE PROJECT (
"pro_id" INTEGER NOT NULL DEFAULT 1,
"app_id" INTEGER NOT NULL DEFAULT 3,
"Start" TEXT NOT NULL,
"End" TEXT NOT NULL,
"Output" INTEGER NOT NULL,
"Log Type" INTEGER NOT NULL,
"Errors" INTEGER NOT NULL,
"Success" INTEGER NOT NULL,
"NOTES" VARCHAR NOT NULL DEFAULT '',
"Duration" TEXT NOT NULL DEFAULT '00.00.00.000',
PRIMARY KEY("app_ID", "pro_ID", "Start"),
FOREIGN KEY ("Output") REFERENCES "OUTPUTS" ("ID") ,
FOREIGN KEY ("app_ID", "pro_ID") REFERENCES "PROJECTS"("A_ID", "P_ID"),
FOREIGN KEY ("Log Type") REFERENCES "TYPE" ("ID"));
The INSERT query (inside Qt) is
temp = "INSERT INTO PROJECT (pro_id, app_ID,
Start, End, Output, Log Type, Errors, Success, NOTES, Duration)
VALUES(1,1, Date & time in quotes, Date & time in double quotes, '1, 1, 0, 0, '0.0.0.0.000')";
The error I get is:
QSqlError("1", "Unable to execute", "near \"Type \": syntax error")
I feel like the problem is on the sqlite side and I'm not familiar with it at all. I started it only this year. I feel like the foreign key is where the issue is
You should probably fire up sqlite and try some things out. Your error is just saying that it doesn't know what "Type" is most likely because of the space. If you wrap "Log Type" in quotes, you'll get something else.
But you have other errors in that statement from what I can tell. But I can't tell if you are abbreviating on purpose like "Date & time in quotes" or not. There's also a single quote missing potentially, etc.
So assuming the values are 100% correct, inserting into a table with a foreign key is just a matter of knowing for sure that the key exists in the foreign key table.

How to know last id of SQLite database [duplicate]

This question already has answers here:
Predict next auto-inserted row id (SQLite)
(11 answers)
Closed 6 years ago.
I have a database in SQLite that I'll be using to store restaurants information for a website.
The website will allow users to insert new restaurants in the dabase (through php).
My question is, given that each row in a table in the database has its id attribute (which is the primary key), how can I know what id should the next item I'm going to store in the database have?
If the last item added to the database has the id 50, then the next restaurant a user tries to add should have the id 51, but how can I have access to this?
Here's my SQLite Restaurant table:
CREATE TABLE Restaurant (
id NUMBER PRIMARY KEY NOT NULL,
name NVARCHAR2(20) NOT NULL,
description NVARCHAR2(20),
address NVARCHAR2(20) NOT NULL,
priceRange NUMBER NOT NULL REFERENCES PriceRange(id) ON DELETE SET NULL ON UPDATE CASCADE
);
Thanks in advance!
Since you've tagged the question with php, you should be able to use SQLite3::lastInsertRowID().
Alternatively, a pure SQLite solution is the last_insert_rowid() function.

How to avoid duplicate records insertion in database table using stored procedure? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a database table with these columns:
Banner_Title varchar(500)
Existing_Banner_Image varchar(500)
Banner_URL varchar(500)
Banner_ALT varchar(500)
CheckBoxText varchar(500)
[for these 3 checkboxes have taken chkArticles,chkFittness,chkHealthArticle
but I want to check checkboxes also while insertion of data.For that I added one extra column in datatable named as sectionId for chkArticle checkbox I will assign 10 as a sectionId ,for chkFittness sectionId 11 and for chkHealthArticle as 12 so when I insert banner_title='article1', click chkArticle checkbox and fill other field data as mentioned in table format from then data should insert but next time when I try banner_title='article1' and click same checkbox then data not allowed to insert.but when I give banner_name='article1' and click another checkbox chkFittness it should allowed though the name of banner is same but maintained sectionid for checkboxes is different then what changes need to do stored procedure?? Basically I want to maintained unique banner_title for each checkbox click and to differentiate I maintained sectionid.plz help me
Create Unique constraint:
USE YourDatebase;
GO
ALTER TABLE YourTable
ADD CONSTRAINT YourConstraintName UNIQUE (Banner_Title);
GO
Check if banner title exists before insert attempt:
IF NOT EXISTS(SELECT Banner_Title FROM YourTable WHERE Banner_Title =
'TitleYourAreAttemptingToInsert')
BEGIN
INSERT .....
END

I have create procedure code,what is the delete procedure code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have create procedure code, what is a delete procedure code.
Create procedure [dbo].[saveemp]
#Empname varchar(50),
#EmpAddress varchar(50),
#EmpDOB int,
#EmpGender varchar(50)
as
begin
insert employee(Empname, EmpAddress, EmpDOB, EmpGender)
values (#Empname, #EmpAddress, #EmpDOB, #EmpGender)
end
you are missing into in insert statement
insert into employee(Empname,EmpAddress,EmpDOB,EmpGender)
values
(#Empname,#EmpAddress,#EmpDOB,#EmpGender)
for deleting row using procedure
Create procedure [dbo].[deleteempname]
#Empname varchar(50)
as
BEGIN
DELETE FROM employee WHERE Empname= #Empname
END
Are you looking for "drop procedure"? http://technet.microsoft.com/en-us/library/ms174969.aspx
To delete the whole table, your query will be,
DELETE FROM employee
And to delete a single row (depending upon a parameter) it will be like,
DELETE FROM employee WHERE EmpName= #EmpName
It will be better if you delete a row depending upon the primary key (if you have any),
DELETE FROM employee WHERE EmpID = #EmpID

last record from database [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Get the last record from a Microsoft SQL database table using ASP.net (VB) onto a web form.
I'm assuming he's trying to retrieve the last inserted record. As Ariel pointed out, the question is rather ambiguous.
SELECT TOP 1 * FROM Table ORDER BY ID DESC
If you have an identity column called ID, this is easiest. If you don't have an identity PK column for example a GUID you wont be able to do this.
Here is a basic solution:
var order = (from i in db.orders
where i.costumer_id.ToString() == Session["costumer_id"]
orderby i.order_id descending
select i).Take(1).SingleOrDefault();
You need to be more specific with actually putting it onto a web form, but the SQL to get the last record is:
SELECT *
FROM TABLE_NAME
WHERE ID = (SELECT MAX(ID) FROM TABLE_NAME)
Where ID is your ID and TABLE_NAME is your table name.

Resources