How to to insert a foreign table name field instead the id in sqlitebrowser GUI - sqlite

How can I select the company name (Acme,Foo) instead it's company_id when inserting a new person?
Here I can select 1 or 2 but want to see Acme or Foo instead. Alas picture doesn't show the dropdown displaying the company ids:
CREATE TABLE "company" (
"company_id" INTEGER,
"name" TEXT,
PRIMARY KEY("company_id" AUTOINCREMENT)
)
CREATE TABLE "person" (
"last_name" TEXT NOT NULL,
"firstname" TEXT,
"salutation" BLOB,
"company_id" INTEGER,
"person_id" INTEGER,
FOREIGN KEY("company_id") REFERENCES "company"("company_id"),
PRIMARY KEY("person_id" AUTOINCREMENT)
)
I'm attempting to use the db browser as crm gui.
Closing this question as duplicate without context gives me no direction at all. How does that question help in regards of inserting a new row using the sqlitebrowser?

Related

Trigger is not created in sqlite

I'm building a database for a library management system, so I have created three tables: books, members and borrow(describe the relation between the two tables).
for each book I store in the books table, I store it's quantity.
for each member that want to borrow a book, I store his id and the book id in the borrow table.
for every time a member want to borrow a book, I want to check that borrowed quantity is equal or less than the quantity stored for that book in the books table(in case it was more it will rise an error and will not accept the new data) , so I tried to achieve this using a trigger
the problem is that when I try to run the trigger it's not created, and it does not even give an error message, when I try even to see the trigger that is created in the database using the command:(select name from sqlite_master where type = 'trigger';) it does not show any thing
here is the code:
CREATE TABLE books(
book_id INTEGER CHECK (book_id>0999) PRIMARY KEY AUTOINCREMENT,
book_title VARCHAR(20) NOT NULL,
author_name VARCHAR(20),
quantity INT NOT NULL,
genre VARCHAR(20) NOT NULL,
book_place VARCHAR(20) NOT NULL,
UNIQUE(book_title,author_name)
);
CREATE TABLE members(
member_id INTEGER PRIMARY KEY AUTOINCREMENT CHECK(member_id<1000) ,
member_name VARCHAR(20) NOT NULL,
member_phone TEXT NOT NULL UNIQUE
CHECK (LENGTH(member_phone)==11 AND member_phone NOT GLOB '*[^0-9]*'
AND (SUBSTR(member_phone,1,3)=='010' OR SUBSTR(member_phone,1,3)=='011'
OR SUBSTR(member_phone,1,3)=='012' OR SUBSTR(member_phone,1,3)=='015' )),
sub_startDate TEXT NOT NULL CHECK(sub_startDate IS DATE(sub_startDate)),
sub_endDate TEXT NOT NULL CHECK(sub_endDate IS DATE(sub_endDate))
);
CREATE TABLE borrow(
member_id INTEGER NOT NULL,
book_id INTEGER NOT NULL,
borrowed_date TEXT NOT NULL CHECK(borrowed_date IS DATE(borrowed_date)),
return_date TEXT NOT NULL CHECK (return_date IS DATE(return_date)),
FOREIGN KEY(member_id) REFERENCES members(member_id) ON DELETE CASCADE ,
FOREIGN KEY(book_id) REFERENCES books(book_id) ON DELETE CASCADE,
PRIMARY KEY(member_id,book_id)
);
CREATE TRIGGER not_enough_copies
BEFORE INSERT, UPDATE
ON borrow
WHEN
(SELECT((SELECT
COUNT(*)
FROM borrow
WHERE book_id=NEW.book_id)
NOT BETWEEN 1 AND
(SELECT quantity FROM books WHERE books.book_id==NEW.book_id)))
BEGIN
RAISE(ABORT,'ERROR!..This book is not available in the library right now')
END;

Cascade delete not working SQLite with 1:1 relationship

I'm trying to set up a 1:1 relationship between two tables Places and People. A person has a home, and when that person is deleted the home should also be deleted. Other tables also use the Places table, so there is no column in the Places table that refers to the People table.
To try and achieve this, I've set the People table up so that when a row is deleted, there is a cascade delete on the foreign key pointing at the Places table row is also deleted.
CREATE TABLE IF NOT EXISTS "People" (
"Id" TEXT NOT NULL CONSTRAINT "PK_People" PRIMARY KEY,
"Name" TEXT NOT NULL,
"HomeId" TEXT NOT NULL,
CONSTRAINT "FK_People_Places_HomeId" FOREIGN KEY ("HomeId") REFERENCES "Places" ("Id") ON DELETE CASCADE
);
However, when I actually tried this, the row in the Places table still existed. Is there any way to fix this?
Fully runnable example
PRAGMA foreign_keys = ON;
CREATE TABLE IF NOT EXISTS "Places" (
"Id" TEXT NOT NULL CONSTRAINT "PK_Places" PRIMARY KEY,
"Name" TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS "People" (
"Id" TEXT NOT NULL CONSTRAINT "PK_People" PRIMARY KEY,
"Name" TEXT NOT NULL,
"HomeId" TEXT NOT NULL,
CONSTRAINT "FK_People_Places_HomeId" FOREIGN KEY ("HomeId") REFERENCES "Places" ("Id") ON DELETE CASCADE
);
DELETE FROM Places;
DELETE FROM People;
INSERT INTO "Places" ("Id", "Name") VALUES ("6f81fa78-2820-48e1-a0a7-b0b71aa38262", "Castle");
INSERT INTO "People" ("Id", "HomeId", "Name") VALUES ("ccb079ce-b477-47cf-adba-9fdac6a41718", "6f81fa78-2820-48e1-a0a7-b0b71aa38262", "Fiona");
-- Should delete both the person and the place, but does not
DELETE FROM "People" WHERE "Id" = "ccb079ce-b477-47cf-adba-9fdac6a41718";
SELECT pl.Name "Place Name",
po.Name "Person Name"
FROM Places pl
LEFT JOIN People po USING(Name)
UNION ALL
SELECT pl.Name,
po.Name
FROM People po
LEFT JOIN Places pl USING(Name)
WHERE pl.Name IS NULL;
The "ON DELETE CASCADE" action for the foreign key that you defined in the table People for the column HomeId which references the column Id of the table Places means that:
whenever you delete a row in the table Places (which is the parent
table in this relationship) all rows in the table People that hold a
reference to the deleted row will also be deleted.
See the demo.
In your case you are deleting a row in the table People and this does not affect at all the table Places.

Adding IDs to join table for many-to-many in SQLite

I am using SQLite Studio 3.3 to work with a simple video database. These are three of the tables I am working with.
-- Table: Videos
DROP TABLE IF EXISTS Videos;
CREATE TABLE Videos (
id INTEGER PRIMARY KEY ASC AUTOINCREMENT
UNIQUE
NOT NULL,
vid_title TEXT NOT NULL
UNIQUE,
vid_rel_date DATE
);
-- Table: People
DROP TABLE IF EXISTS People;
CREATE TABLE People (
id INTEGER PRIMARY KEY AUTOINCREMENT
NOT NULL
UNIQUE,
person_name_first TEXT NOT NULL,
person_name_last TEXT
);
-- Table: _Videos_People
DROP TABLE IF EXISTS _Videos_People;
CREATE TABLE _Videos_People (
id INTEGER PRIMARY KEY AUTOINCREMENT
UNIQUE
NOT NULL,
vid_id INTEGER REFERENCES Videos (id),
person_id INTEGER REFERENCES People (id)
);
I have these statements to add a video and a person to the database and to get both IDs.
INSERT OR IGNORE INTO Videos (
vid_title,
vid_rel_date
)
VALUES (
'The Call Of The Wild',
'2020'
);
INSERT OR IGNORE INTO People (
person_name_first,
person_name_last
)
VALUES (
'Harrison',
'Ford'
);
SELECT id
FROM Videos
WHERE vid_title = 'The Call Of The Wild'
AND vid_rel_date = '2020';
SELECT id
FROM People
WHERE person_name_first = 'Harrison'
AND person_name_last = 'Ford';
This is where I am stuck. Once I have these two IDs, how do I add them to the _Videos_People join table so that the person is associated with the video?
I found this but I don't know how to use it for my situation (or if I should).
If I am going about this completely wrong, I am open to suggestions. I am new at this so I appreciate any help.
You can use this INSERT statement:
INSERT INTO _Videos_People (vid_id, person_id) VALUES (
(SELECT id FROM Videos WHERE vid_title = 'The Call Of The Wild' AND vid_rel_date = '2020'),
(SELECT id FROM People WHERE person_name_first = 'Harrison' AND person_name_last = 'Ford')
);
See the demo.

How to create a required many-to-many relationship in SQLite?

Suppose I have a schema like this:
CREATE TABLE Artist (
ArtistID INTEGER NOT NULL,
ArtistName TEXT NOT NULL,
PRIMARY KEY (ArtistID)
);
CREATE TABLE Song (
SongID INTEGER NOT NULL,
SongTitle TEXT NOT NULL,
PRIMARY KEY (SongID)
);
CREATE TABLE SongArtist (
SongID INTEGER NOT NULL,
ArtistID INTEGER NOT NULL,
PRIMARY KEY (SongID, ArtistID),
FOREIGN KEY (SongID) REFERENCES Song(SongID),
FOREIGN KEY (ArtistID) REFERENCES Artist(ArtistID)
);
By defining a column as NOT NULL I can semantically say that having a value in it is required. How would I make a many-to-many relationship required, but only in one direction?
In this situation, what I mean is this: How can I say that a Song row must have at least one Artist row associated with it through the SongArtist join table? If I were to represent a song as the JSON object below, this would be equivalent to saying that the songArtistIds array must have a length of 1 or higher.
{
songId: 745194,
songTitle: "Title",
songArtistIds: [523214]
}
However, an Artist row need not be associated with any Song row necessarily. An artist can have 0 or more songs, but a song must have 1 or more artists. How can I enforce this in SQLite? Also, if the answer is that I cannot do this in SQLite, then what alternative do I have for an embedded application?

Oracle SQLFiddle - Building schema Error

First time using SQLFiddle. I want to test Oracle sql. So on left panel, I paste this code which I got here
create table Employee(
name varchar2(100),
id integer,
salary integer,
PRIMARY KEY(id)
);
This successfully executed. But when I change 1 of the column, I got script error:
create table Employee(
sssssss varchar2(100),
id integer,
salary integer,
PRIMARY KEY(id)
);
Why? Can't I create any column name?

Resources