I'm trying to figure out a way to find an entry or the entries where my keywords (multiple) are present in the category table / category name column .
CREATE TABLE entry (
entryid INTEGER PRIMARY KEY,
title TEXT NOT NULL,
datecreated date NOT NULL
);
CREATE TABLE category (
entryid INTEGER,
categoryid INTEGER PRIMARY KEY,
categoryname TEXT NOT NULL
);
INSERT INTO entry VALUES (1, 'title1', '2022-01-15');
INSERT INTO entry VALUES (2, 'title2', '2022-01-16');
INSERT INTO entry VALUES (3, 'title3', '2022-01-17');
INSERT INTO category VALUES (1, 1, 'categorya');
INSERT INTO category VALUES (1, 2, 'categoryd');
INSERT INTO category VALUES (1, 3, 'categorye');
INSERT INTO category VALUES (2, 4, 'categoryd');
INSERT INTO category VALUES (2, 5, 'categorya');
INSERT INTO category VALUES (2, 6, 'categoryb');
INSERT INTO category VALUES (3, 7, 'categorye');
SELECT DISTINCT entry.* FROM entry,category
WHERE entry.entryid=category.entryid
AND (category.categoryname LIKE '%ye%' AND category.categoryname LIKE '%yd%')
I thought that this would give me entryid=1 but, if I unterstand it correctly, I already limit the query with comparing my first keyword? What would I have to do to search for multiple keywords in one column and get the correct entryid?
Edit: I am using HSQLDB within Libre Office Base. I used the data in this post for a more "standardized" approach and to then adjust the solution to my needs :)
Can anyone help me with this error?
Database info:
CREATE DATABASE School;
Use School to create first table:
CREATE TABLE Classes
(
class_id INT,
name Varchar(45),
dept Char(4),
number CHAR(4),
section Char(2),
location Varchar(45),
meeting_time varchar(45),
PRIMARY KEY(class_id)
);
INSERT INTO Classes
VALUES (000001, 'Intro. to databases', 'info', '1620', '1a', 'sarpy214', 'm/w 1-2:45 pm');
INSERT INTO Classes (class_id, name, dept, number, section, location)
VALUES (000002, 'intro. to sql', 'info', '2630', 'ww', 'online');
INSERT INTO Classes
VALUES (000003, 'Software Engineering I', 'info', '1325', '4c', 'socmahoney205', 't/h 10-11:45 pm');
INSERT INTO Classes (class_id, name, dept, number, section, location)
VALUES (000004, 'Software Engineering II', 'info', '1335', 'ww', 'online');
INSERT INTO Classes
VALUES (000005, 'How to leave the shire & live forever', 'ring', '1001', '1r', 'socmahoney214', 'f 10-11:45 am');
INSERT INTO Classes (class_id, name, dept, number, section, location)
VALUES (000006, 'Living with the demon inside', 'psyc', '1668', 'ww', 'online');
INSERT INTO Classes
VALUES (000007, 'Internet Scripting jedi mastery', 'info', '2430', '2b', 'socmahoney205', 'm/w 10-11:45 am');
UPDATE Classes
SET meeting_time = 't/h 10-11:45 am'
WHERE class_id = 000003;
SELECT * FROM Classes;
Now create 2nd table:
CREATE TABLE Enrol
(
stu_id int,
class_id int,
grade char(1),
PRIMARY KEY (stu_id),
FOREIGN KEY (stu_id) REFERENCES Students(Stu_id),
FOREIGN KEY (class_id) REFERENCES Classes(class_id)
);
INSERT INTO Enrol VALUES (0000001, 000002, 'A');
INSERT INTO Enrol (stu_id, class_id) VALUES (0000002, 000002);
INSERT INTO Enrol VALUES (0000005, 000001, 'D');
INSERT INTO Enrol (stu_id, class_id) VALUES (0000006, 000005);
INSERT INTO Enrol VALUES (0000003, 000006, 'C');
SELECT * FROM Enrol;
Create 3rd table:
CREATE TABLE Students
(
Stu_id INT PRIMARY KEY,
fname VARCHAR(45),
lname VARCHAR(45),
area_code CHAR(3),
phone VARCHAR(8)
);
INSERT INTO Students
VALUES (000001, 'patty', 'melt', '402', '234-9876');
INSERT INTO Students
VALUES (000002, 'bill', 'fold', '402', '531-6222');
INSERT INTO Students
VALUES (000003, 'sam', 'winchester', '402', '234-2346');
INSERT INTO Students
VALUES (000004, 'luke', 'skywalker', '402', '543-1234');
INSERT INTO Students
VALUES (000005, 'charlie', 'kelly', '402', '234-6859');
INSERT INTO Students
VALUES (000006, 'bilbo', 'baggins', '531', '646-3828');
SELECT * FROM Students
enter image description here
the issue that you used aggregated function and in the group by you need to put all non aggregated function
so add in the group by
GROUP BY S.STU_ID, S.FNAME, S.LNAME;
The "error" is a minor one and several RDBMS will even ignore it.
You see, if you have a tuple such as (ID, FNAME), it is obvious that every ID maps to its own FNAME. It can't be otherwise. So, GROUPing by ID, or grouping by ID and FNAME, is exactly the same thing.
While, on the other hand, in a complex JOIN, you might have different values of a third column from a different table for the same ID value; in that case, you cannot SELECT both ID and this third column when grouping by ID, because the system wouldn't have enough information to decide which, among the possible different values of the third column, should go with each ID. In this case, to solve the quandary you need to add this third column to the GROUP BY, thereby increasing the cardinality of ID in the response: you might now have 1, JOHN SMITH, YELLOW and 1, JOHN SMITH, RED.
Here, your SELECT is of the first kind, but the database engine does not realize or care for this, and insists that you specify a full GROUP with all the fields in the select, even if one is a primary key and the others are in the same table.
So,
GROUP BY S.STU_ID, S.FNAME, S.LNAME
I'm designing a DB where employees need to be activated by an Admin. When a new employee registers, their workstation (int) gets set to 0 by default. However, I also can't allow to have more than 1 employee per workstation. So I was thinking if there's a way to allow for duplicates of the value 0, but enforce that any other number be unique.
You can use a partial unique index. For example:
create unique index ix1 on employees (workstation) where workstation <> 0;
As in:
create table employees (
id int,
name varchar(10),
workstation int not null
);
create unique index ix1 on employees (workstation) where workstation <> 0;
insert into employees (id, name, workstation) values (1, 'Anne', 100);
insert into employees (id, name, workstation) values (2, 'Peter', 101);
insert into employees (id, name, workstation) values (3, 'Joel', 100); -- fails
insert into employees (id, name, workstation) values (4, 'Niko', 0);
insert into employees (id, name, workstation) values (5, 'Akina', 0); -- succeeds
This is one of the constraints that you cannot enforce using traditional constraints.
See running example at db<>fiddle. As you see only Joel, 100 is rejected. The other two cases with workstation = 0 are inserted.
I'm very new to SQL and i feel like this is a dumb question because I couldn't find an answer to it...
I have this table in a SQL query:
CREATE TABLE MOVIE
(
TITLE VARCHAR(255) NOT NULL,
YEAR INTEGER NOT NULL,
LENGTH INTEGER,
INCOLOR CHAR(1),
STUDIONAME CHAR(50),
PRODUCERC# INTEGER
);
INSERT INTO MOVIE
VALUES ('Pretty Woman', 1990, 119, 'Y', 'Disney', 199);
INSERT INTO MOVIE
VALUES ('The Man Who Wasn''t There', 2001, 116, 'N', 'USA Entertainm.', 555);
INSERT INTO MOVIE
VALUES ('Logan''s run', 1976, NULL, 'Y', 'Fox', 333);
INSERT INTO MOVIE
VALUES ('Star Wars', 1977, 124, 'Y', 'Fox', 555);
I want to write a query where I get only the movies with length bigger than that of "Star Wars". Is there another way to do it instead of "WHERE length > 124" where I put something like a variable after '>' rather than a number
Probably easiest with a subquery:
where length > (select length from movie where title = 'Star Wars')
I am trying to store the interests of a user. I was wondering what is the best way or an example in which I could do so. The interest could already be in the table. How would I be able to take out any interests that are not in the new interest array and add the interests that are not in the table for a particular user?
Example:
Interest Array: ['Dance', 'Art', 'Singing','Surfing'];
User Id: 4
SQL Table:
CREATE TABLE interests (
interests_id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(user_id),
passion TEXT NOT NULL
);
INSERT INTO interests (user_id, passion)
VALUES (1, 'Volleyball'),
(1, 'Football'),
(2, 'Football'),
(2, 'EDM'),
(2, 'Art'),
(3, 'EDM'),
(3, 'Surfing'),
(4, 'Volleyball'),
(4, 'Dance');
Thanks!