How to select entries from one table where a certain field is not existing in the other table - sqlite

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()

Related

Select row owner in oracle table

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;

How to find 2 matching records comparing two fields in one table to two fields in another table?

Using MS Access 2010, I have two tables that I need to compare and only retrieve the matches.
Table [EE] has 5 fields:
Field4, SSN, Birthdate, Address1, Address2
Table [UPDATED] has fields:
Field4, DOB
and several others not relevant to thus question.
I need to find all records in [EE] from fields Field4 AND Birthdate that have a matching values in BOTH Field4 and DOB in [UPDATED]. I have tried INNER JOIN and it is returning me several duplicates. I have tried:
SELECT EE.Birthdate, EE.Field4
FROM EE, UPDATED
WHERE (EE.Birthdate = UPDATED.DOB)
AND (EE.Field4 = UPDATED.FIELD4)
And
SELECT EE.Birthdate, EE.Field4
FROM INNER JOIN UPDATED ON EE.Birthdate = UPDATED.DOB)
AND (EE.Field4 = UPDATED.FIELD4)
I am getting a lot of duplicate records and only want the records that appear in BOTH tables.
There are probably actually duplicates. Add the DISTINCT keyword to eliminate duplicates:
SELECT DISTINCT EE.Birthdate, EE.Field4
FROM EE
JOIN UPDATED ON EE.Birthdate = UPDATED.DOB
AND EE.Field4 = UPDATED.FIELD4
I also fixed a few syntax errors.
Also, always prefer using proper join syntax.

Same attributes, different tables in one database in SQLite

I am just new in SQLite and I have this assignment in creating database schema using the said program.
I just want to ask if it is allowed to give a same attributes present in different tables inside a single database. Let me give the SQLite statements of two tables in one database.
BEGIN TRANSACTION;
/* Create a table called Employee */
CREATE TABLE Employee(Name text, Phone_number varchar);
/* Create a table called Company */
CREATE TABLE Company(Name text, Country text);
I am referring to attribute Name which appears twice in two tables. Will it affect or distorts some of the SQL queries execution in the database?
If you had tried it (in the command-line shell or on SQLFiddle), you would have found out that it is allowed.
However, it can affect your queries.
When you join these two tables, you have to explicitly specifiy which table's column you want:
CREATE TABLE Employee(ID, CompanyID, Name, Phone_number);
CREATE TABLE Company(ID, Name, Country);
SELECT Company.Name,
Employee.Name,
Phone_number
FROM Company
JOIN Employee ON Company.ID = Employee.CompanyID;
On the other hand, if you have a foreign key relationship, using the same name in both tables allows you to simplify the join condition:
CREATE TABLE Employee(EmployeeID, CompanyID, Name, Phone_number);
CREATE TABLE Company(CompanyID, Name, Country);
SELECT Company.Name,
Employee.Name,
Phone_number
FROM Company
JOIN Employee USING (CompanyID);

SQLite regular table and table for fts

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 '😸')

How can I move several columns from one table to a new one to-many table?

I'd like to refactor my database by splitting a single table into two. I'd like to moving a few existing columns to a new table. For instance, suppose I want to move the home_address and work_address fields in the below Employee table to a new Address table.
How can I accomplish this using sqlite?
Before:
Employee Table
employee_id (Primary)
name
home_address
home_city
work_address
work_city
After:
Employee Table
employee_id (Primary)
name
home_address_id
work_address_id
Address Table
address_id (Primary)
address
city
I prefer migrations to be simple and straightforward, without extra logic, etc. At least when you run them only once or so.
So, first check what is max(employee_id), below assumes that it less than 10000 (and it is integer).
create table employee_new(employee_id,name,home_address_id,work_address_id);
insert into employee_new select employee_id,name,employee_id,employee_id+10000 from employee;
create table address(address_id,address,city);
insert into address select employee_id,home_address,home_city from employee;
insert into address select employee_id+10000,work_address,work_city from employee;
alter table employee rename to employee_old;
alter table employee_new rename to employee;

Resources