Increment column id based on another id - sql

My table :
project_user_id refers to user's login id.
name refers to project name.
id is an incremented column based on a new name entry and refers to the project id.
Example table. How to make id column increment from 1 based on a different project_user_id and therefore name input? Desired table.
I am using SQLite and db.execute in my Python Flask application.

I would leave id alone, id should be a unique identifier for your row that has no meaning whatsoever, other than used to uniquely identify your row. if you want to display something else use a view. something like
create or replace view myview as
select project_user_id, name,
row_number() over (partition by project_user_id order by null) as id
from mytable

Related

Copying dates to a column from another table with criteria

I'm trying to do the following:
One table in the database is called service_state_history, that table has three columns:
service_id - which refers to the id of the service that the service_state is related to. (it's nor foreign key because the relation is done in the JPA code in this case).
state_started column which has DATETIME values, that indicate whenever the state on that row has started.
state column which describes the state of that history entry and has following VARCHAR-values: (STANDBY, IN PROGRESS, DONE)
The other table in the database is called services, that table has two relevant columns:
id BIGINT
done_date DATETIME
Now, what I'm supposed to do is update the done_date column in services from the service_state_history table's column state_started, when the state column's value in the row is DONE and the service_id value in matches id in services.
How does that translate into SQL?
So to get all the records that will be updating the destination table, you will use the results generated from:
SELECT service_id, state_started, state
FROM service_state_history
WHERE state = 'DONE'
To perform the UPDATE you will need to JOIN this result set to the destination table on the identity column and use the state_started in the update. Something like:
UPDATE s
SET s.done_date = ssh.state_started
FROM services s
INNER JOIN service_state_history ssh ON ssh.service_id = s.id
WHERE ssh.state = 'DONE'

Forcing data entry in order

I have a sql server database with ms access frontend.
I want to force data to be entered in a particular table in order.
eg I can only enter id = 2 after id = 1 has been added and I can only add id = 3 after 2 and 1 have been added.
What is the best way to enforce this? Do I need a restriction added to the access frontend or do I need to add a trigger or similar to the sql table?
i agree with #Vamsi Pamula, You can do that. But, however if you want to do only what your are looking for then :
First of all, When user tries to enter an new record, track the New ID Given Suppose 10 for now. And, first Query in the database for the Max id before like :
Select isnull(Max(id),0) from YourTable
Suppose the above query returned 8. So, 10 should not be allowed. Now now check :
if (ReturnedValuefromAboveQry + 1 = NEWID) then
msgbox "Allowed."
else
msgbox "Not Allowed."
End If
If you want to do so, keep that id as primary key and set it property as identity (auto increment). You no need to insert that value. Insert the remaining columns and that id will be automatically saved as 1,2 and so on

Get values based on newly inserted value using SQL

I want to make filtration on a column after selecting a specific value of another column in the same table, I tried to use #... special character followed by the column's name to get the address of this value.
My SQL statement is like the following :
SELECT ATTRIBUTE FROM TABLE WHERE FIELD = '#FIELDNAME';
If I used a specific value instead of #FIELDNAME, it will work properly but it will be static but I need it to be dynamic based on the selected value.
Create another table which will have the list of values that are in the FIELDNAME and give each record a unique id ,then retrieve the value depending on what you have selected by the name of the new table's field preceded by '#...'
I don't know if that what are you looking for, please let me know.
If no triggers are allowed, do you have any date/time column in the table? Is it possible to have that extra column anyway to see the time of a newly inserted row?
You may have to check the lastest row entered, save its field value into a variable. Then do the select based on the variable value.
Based on the vague last row id you could try the following (it's not pretty). But again, if you have date/time that's more accurate.
select attribute from table
where field = (select field from table
where rowid =(select max(rowid) from table))
;
upate
Do you have the priviledge to set up your insert command as below:
insert into table (id, col1, col2,...) values (1,'something', 'something',...)
returning id into variable; -- you may either save field or id depending on your table
Then you may use this variable to select the records you want.

how to return last inserted (auto incremented) row id in HSQL?

i am working with HSQL database for testing purpose. i want standalone db file.
but now i am in trouble to get last inserted row id (auto-incremental - identity) in HSQL. how can i get id??
http://www.hsqldb.org/doc/guide/ch09.html
the last inserted value into an identity column for a connection is
available using the function IDENTITY(), for example (where Id is
the identity column):
INSERT INTO Test (Id, Name) VALUES (NULL,'Test');
CALL IDENTITY();
It's pretty hard to write a query to perform this when you haven't given your table schema, but something like the following:
SELECT TOP 1 Id FROM [TABLENAME] ORDER BY Id DESC

How to set primary key on a field in fusion table

I have a table named "Testing". fields in this table are Id,Name and Description.
I want to make Id field as a primary key i.e. only unique values should be entered in this field.
Thanks,
Himanshu
When you do CREATE TABLE
In addition to the column names you specify in the CREATE TABLE statement, Google Fusion Tables will create a column named ROWID that contains an automatically-assigned, unique ID number for each row.
I don't think you can do this. It appears to be a much requested feature: https://code.google.com/p/fusion-tables/issues/detail?id=112
It's not available. I use a workaround that performs a COUNT(ROWID) query for the unqiue ID I'm about to insert and then take steps depending on whether or not that unique ID is already in the table.
The basic algo is something like this (using Google Apps Script):
var check = FusionTables.Query.sql("SELECT COUNT(ROWID) FROM <table ID> WHERE 'Trip ID' = '" + rowID + "'");
if (check.rows[0][0] > 0) {
// ID already exists
} else {
// ID is new, insert the record.
}