Creating a Random Number in a Local Temporary Table - sql

today I have a bit of a dilemma. I was tasked with creating a local temporary table that would contain faculty members first name, last name, campus, and new id number. The ID number would be a randomly generated 5 digit number. (I am using Microsoft SQL Server Management Studio)
My problem is I am new to random number generation and local temp tables. I believe most of my code is correct expect for the "random id number" I need to make. I have googled my problem only thing is there seem to be many ways to create "random" numbers and I don't understand the method behind it.
I've included my code and the database below.
My Code:
SELECT FirstName, LastName, Campus, LEFT(CAST(CAST(CEILING(RAND() *100000000) AS bigint) AS varchar), 5) AS IDnumber
INTO #LocalTemp1
FROM Faculty;
SELECT * FROM #LocalTemp1
Database:
CREATE TABLE Faculty
(Faculty_ID INT PRIMARY KEY IDENTITY,
LastName VARCHAR (20) NOT NULL,
FirstName VARCHAR (20) NOT NULL,
Department VARCHAR (10) SPARSE NULL,
Campus VARCHAR (10) SPARSE NULL);
INSERT INTO Faculty VALUES ('Brown', 'Joe', 'Business', 'Kent');
INSERT INTO Faculty VALUES ('Smith', 'John', 'Economics', 'Kent');
INSERT INTO Faculty VALUES ('Jones', 'Sally', 'English', 'South');
INSERT INTO Faculty VALUES ('Black', 'Bill', 'Economics', 'Kent');
INSERT INTO Faculty VALUES ('Green', 'Gene', 'Business', 'South');
CREATE TABLE Course
(Course_ID INT PRIMARY KEY IDENTITY,
Ref_Number CHAR (5) CHECK (Ref_Number LIKE '[0-9][0-9][0-9][0-9][0-9]'),
Faculty_ID INT NOT NULL REFERENCES Faculty (Faculty_ID),
Term CHAR (1) CHECK (Term LIKE '[A-C]'),
Enrollment INT NULL DEFAULT 0 CHECK (Enrollment < 40))
INSERT INTO Course VALUES ('12345', 3, 'A', 24);
INSERT INTO Course VALUES ('54321', 3, 'B', 18);
INSERT INTO Course VALUES ('13524', 1, 'B', 7);
INSERT INTO Course VALUES ('24653', 1, 'C', 29);
INSERT INTO Course VALUES ('98765', 5, 'A', 35);
INSERT INTO Course VALUES ('14862', 2, 'B', 14);
INSERT INTO Course VALUES ('96032', 1, 'C', 8);
INSERT INTO Course VALUES ('81256', 5, 'A', 5);
INSERT INTO Course VALUES ('64321', 2, 'C', 23);
INSERT INTO Course VALUES ('90908', 3, 'A', 38);
A source i was looking at, still need a better understanding: Generating a random & unique 8 character string using MySQL
EDIT: Running the query actually doesn't display any information, just the column names.
EDIT: Still would need help, I don't know if editing this post will bump it
EDIT: So I decided to try and just use "RAND()" on its own. I can now see the results being displayed, however, the number sets are not all random.
EDIT: Updated the formula for the RANDOM ID, it works in a way, just not making every row a unique random number.

Related

Trying to find entries where multiple keywords are present in column (AND not OR)

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 SQL code error?

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

T-SQL constraint to check if value is either 0 OR unique

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.

Is there a way to use a table value attribute in where clause in SQL

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

How Do I Update My SQL(Postgresql) Database With New Information From An Array?

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!