I have a table named Table1, and I have five users with SELECT and INSERT privileges, each one of these users had populated data to the table.
How to display each row is owner ? for example row 1 inserted by user2, row 2 inserted by user4 and so on?
It's too late to find out which user added existing rows. To know for future inserted rows, add this column to the table:
alter table table1 add created_by varchar2(30) default user;
Another commonly added column is:
alter table table1 add created_date date default sysdate;
Related
How to select entries in peewee ORM from one table where a certain field is not in the other table?
E.g.
How to count the employees which is there in tblemployees and not in tblcards?
select count(*) from tblEmployees
where name not in
(select employname from tblCards);
Note: Both fields, "name" and "employname" are not the respective primary/foreign keys).
You should provide your model definitions. But did you try something like:
Employee.select().where(Employee.name.not_in(Card.select(Card.employname))).count()
I need to insert extra records in one table for all entries in another table where a certain condition is not met, namely they do not exist yet in the table.
I have 3 tables, one is called ARTICLES, and has columns (ArtNr, ArtName), the second table is called CUSTOMERS with columns (CustNr, CustName, CustAddress)
The third table CUSTARTLINK is a link between the Customer and Article tables and has a record for each Customer and all Article is has. So it has columns (CustNt, ArtNr).
This way, in the application, when a customer is selected, only the articles will be shown that have a Link with the customer in the table .
All this functionality is working, including adding and deleting articles and customers in the resp. tables, and adding articles to customers (creating entries in the CUSTARTLINK table.
I want to add a functionality that I can select one Article from the Articles table and add this to ALL the customers existing in the CUSTOMERS table (so creating new entries in the CUSTARTLINK table, as long as they not already exist.
I tried to create this with the INSERT into command combined with a WHERE condition, but I am not getting close to what I want to reach.
Can anyone list the SQL commands to reach this?
Suppose that you want to insert the artnr = 11 for all customers:
insert into custartlink (custnr, artnr)
select c.custnr, 11
from customers c
where not exists (
select 1 from custartlink
where custnr = c.custnr and artnr = 11
)
The condition where not exists... makes sure that there will be no duplicates after the statement is executed, or if there is already a constraint in the table custartlink that there will be no error trying to violate the constraint.
The same can be achieved with the use of except:
insert into custartlink (custnr, artnr)
select custnr, 11
from customers
except
select custnr, 11
from custartlink
where artnr = 11
If you have the article's name like 'art11' and you want to use that and not the artnr, then you can do this:
insert into custartlink (custnr, artnr)
select
c.custnr,
(select artnr from articles where artname = 'art11')
from customers c
where not exists (
select 1 from custartlink
where
custnr = c.custnr
and
artnr = (select artnr from articles where artname = 'art11')
)
INSERT INTO CUSTARTLINK (CustNt, ArtNr)
(SELECT CustNr, X from CUSTOMERS)
Replace X with article ID
i would like to have a situation whereby i have 2 tables, table 1 is customers table with salary column and table 2 is a tax table.
When i insert a record in the tax table with tax amount, i would like the amount to subtract the salary and the customer table (salary column) is update with the net salary.
CREATE OR REPLACE TRIGGER trig_update
AFTER INSERT ON tax
FOR EACH ROW
DECLARE
net_sal;
BEGIN
net_sal := :customers.salary - :tax.amount;
UPDATE customers (salary) VALUES (net_sal)
WHERE (tax.cust_id == customers.id);
END;
I am getting a compilation error
First, there is no "==" operator in SQL. Only "=".
Second, you need a SET clause in your update statement. It should be
UPDATE customers
SET salary = net_sal
WHERE tax.cust_id = customers.id;
Third, this is bad table design. Instead of updating the salary column, make a new column called "net_salary" and update that column instead. What if you had to delete a tax record and insert a new one? Your salary value would still have the old taxes taken from it, being incorrectly low.
I have table news (id, news_id, news_title) and I creat FTS table:
CREATE VIRTUAL TABLE news_search USING fts4 (news_title, tokenize=porter);
I use trigger to keep table NEWS and news_search in sync:
CREATE TRIGGER IF NOT EXISTS insert_news_trigger
AFTER INSERT ON news
BEGIN
INSERT OR IGNORE INTO news_search (news_title) VALUES (NEW.news_title);
END;
Question: how to use search? When I do MATCH in news_search table it returns me only records from this table, but I need *news_id* from news table. May be I should add *news_id* column to news_search table?
What is the proper way to use fts in sqlite?
Read the documentation; FTS tables also have a rowid column (also called docid) that you can set explicitly to the same value as the corresponding key of the original table.
Assuming that news.id is the rowid (i.e., INTEGER PRIMARY KEY), you should change your trigger to also copy that ID value into the news_search table.
You can the use that to look up the original record:
SELECT *
FROM news
WHERE id IN (SELECT docid
FROM news_search
WHERE news_title MATCH '😸')
I am trying to create a trigger such that if i am inserting some values in 1st table than the two field of the 2nd table automatically updated.
case is that i have a table in which i'm storing user details
First Table
1st name | last name | userId | password | adress | email
and 2nd login table which have two field
Second Table
userId | password
now i want if i change value of password in 1st table is automatically reflect in the 2nd table what is the query for that.
as long as the userid and password for the second table are mapped as foreign keys to the first table, i think you should be able to set "on update cascade" on the rows
you can do like this....
CREATE TRIGGER trgTest ON Test
FOR INSERT
AS
INSERT Test2
(Id, value)
SELECT Id, Value
FROM Inserted
or if you're using stored procedures you can easily manage this
CREATE PROCEDURE sp_Insert
#Value varchar(10)
AS
insert into table1 (...,...) values (#value,...)
insert into table2 (...,...) values (#value,...)
You've mostly answered your own question, Prakash. Check out the MSDN documentation on triggers for the specific syntax you need. Jon's comment in regard to the duplication of data points out that your schema is somewhat denormalized so you may want to take a look at changing that unless it's required for your situation.
From MSDN
INSERT INTO auditEmployeeData
(audit_log_type,
audit_emp_id,
audit_emp_bankAccountNumber,
audit_emp_salary,
audit_emp_SSN)
SELECT 'NEW',
ins.emp_id,
ins.emp_bankAccountNumber,
ins.emp_salary,
ins.emp_SSN
FROM inserted ins