Can anyone help me with this SQL code error? - sql

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

Related

SQL table definition for 1-N-M situation

My situation is very complex, but I can simplify my problem to the following situation:
Assume a Kindergarden with 3 tables: "kids", "corner" and "day".
Each "day", any number of "kids" can play in one of the "corner"s.
The next day, the kids can chose another corner to play.
So for each day, the kids and the corners must be uniqe.
Here's an example DB to copy when fiddling:
CREATE TABLE kids (
id INT,
name TEXT
);
INSERT INTO kids VALUES (1, "Paul");
INSERT INTO kids VALUES (2, "Mary");
INSERT INTO kids VALUES (3, "Tom");
INSERT INTO kids VALUES (2, "Gina");
CREATE TABLE corner (
id INT,
name TEXT
);
INSERT INTO corner VALUES (1, "Kitchen");
INSERT INTO corner VALUES (2, "Cars");
CREATE TABLE days (
id INT,
day DATE
);
Basically, you would need another table to represent the relationship between the three entities:
create table plays (
kid_id int references kids(id),
corner_id int references corner(id),
day_id int references days(id),
primary key (kid_id, day_id)
);
Each column is a foreign key to the corresponding referential table. The primary key ensures that a given kid plays in only one corner per day.

oracle apex giving me an error when using INSERT INTO to add data to tables

I have an assignment due in a few days but cannot remember coming across this error before, im sure its something tiny that ive skimmed over but could someone take a look?
error is :
ORA-00984: column not allowed here ORA-06512: at
"SYS.WWV_DBMS_SQL_APEX_190100", line 590 ORA-06512: at
"SYS.DBMS_SYS_SQL", line 1658 ORA-06512: at
"SYS.WWV_DBMS_SQL_APEX_190100", line 576 ORA-06512: at
"APEX_190100.WWV_FLOW_DYNAMIC_EXEC", line 2033
ive tried rewriting single lines but it will not take any code that i put into it. The code i have written is in asterisks (bottom 14 lines) it works fine without these lines so i cannot figure out why. Im a newbie to this kind of stuff (first year uni) so any help would be appreciated!
CREATE TABLE vet(
vetno NUMBER(6) PRIMARY KEY,
vetname VARCHAR2(16) NOT NULL,
vetaddressln1 VARCHAR2(20)NOT NULL,
vetaddressln2 VARCHAR2(20),
vetpostcode VARCHAR2(8)NOT NULL);
DROP TABLE client cascade constraints;
CREATE TABLE client(
clientno NUMBER(6)PRIMARY KEY,
surname VARCHAR2(15)NOT NULL,
firstnames VARCHAR2(15)NOT NULL,
clientaddressln1 VARCHAR2(20)NOT NULL,
clientaddressln2 VARCHAR2(20),
clientpostcode VARCHAR2(8)NOT NULL);
DROP TABLE animal cascade constraints;
CREATE TABLE animal(
animalno NUMBER(6) PRIMARY KEY,
name VARCHAR2(15)NOT NULL ,
sex VARCHAR2(6),
dateofbirth DATE,
species VARCHAR2(30)NOT NULL ,
clientno NUMBER(6) REFERENCES client(clientno));
DROP TABLE consultation cascade constraints;
CREATE TABLE consultation(
consultationno NUMBER(6),
consultationdate DATE NOT NULL,
animalno NUMBER(6) REFERENCES animal(animalno),
vetno NUMBER(6) REFERENCES vet(vetno),
Outcomenote VARCHAR2(25) NOT NULL );
INSERT INTO vet VALUES (001,'James Herriot','Skeldale, High St', 'Yarm', 'YM3 6WP');
INSERT INTO vet VALUES (002,'Siegfried Farnon','61 Farnby Mount', 'Yarm', 'YM3 6WD');
INSERT INTO vet VALUES (003,'Danielle Chang','North St', 'Leeds', 'LS6 3NG');
INSERT INTO vet VALUES (004,'Emma Milne','87 Arncliffe Road', 'Harrogate', 'HG5 5HY');
INSERT INTO vet VALUES (005,'Harry Cooper','15, Coniston Avenue', 'Leeds', 'LS11 4PE');
INSERT INTO client VALUES (0034,'Wong','Judy','Flat 5, Victoria Av', 'Leeds', 'LS5 9PL');
INSERT INTO client VALUES (0035,'Spencer','Tom','4, Broad Lane', 'Harrogate', 'HG4 9DL');
INSERT INTO client VALUES (0036,'Hamza','Farhan','35A, Waterloo Cres', 'Harrogate', 'HG3 3FD');
INSERT INTO client VALUES (0037,'Cummins','Dominic','184, Queenswood Rd', 'Bradford', 'BR3 2GD');
INSERT INTO client VALUES (0038,'Stuart','Moira','77, Westgate', 'Leeds', 'LS1 4KL');
** INSERT INTO animal VALUES (00100,'Elsie','M', 01-FEB-13, 'PERSIAN CAT', '0034');
INSERT INTO animal VALUES (00101,'Thurston','M', 15-MAY-11, 'MAINE COON CAT', '0034');
INSERT INTO animal VALUES (00102,'Jeff','F', 11-SEP-19, 'NORWEIGAN FOREST CAT', '0035');
INSERT INTO animal VALUES (00103,'Monkey','M', 16-SEP-99, 'BOMBAY CAT', '0036');
INSERT INTO animal VALUES (00104,'Terry','M', 11-MAY-14, 'RED-FOOTED TORTOISE', '0037');
INSERT INTO animal VALUES (00105,'Emilia','F', 29-OCT-19, 'RED-KNEE TARANTULA', '0038');
INSERT INTO consultation VALUES (001001,14-jan-20,'00101','001','Given antibiotics');
INSERT INTO consultation VALUES (001002,11-JUL-20,'00101','002','Given respiratory tablets');
INSERT INTO consultation VALUES (001003,08-JUN-20,'00102','003','Given antibiotics');
INSERT INTO consultation VALUES (001004,11-AUG-20,'00103','005','Saline Drip');
INSERT INTO consultation VALUES (001005,11-SEP-20,'00104','001','Laid 3 eggs');
INSERT INTO consultation VALUES (001006,17-NOV-20,'00102','004','Given antibiotics');
INSERT INTO consultation VALUES (001007,11-DEC-20,'00105','004','Moult Assistance');
INSERT INTO consultation VALUES (001008,04-OCT-20,'00101','005','Given antibiotics');** ```
It is about invalid dates. The way you tried to do it is wrong - not just because it didn't work, but because you wanted to insert them as strings (only if they were enclosed into single quotes). You should insert dates.
One option is to use date literal which is always in format date 'yyyy-mm-dd':
SQL> INSERT INTO animal VALUES (00100,'Elsie','M', date '2013-02-01', 'PERSIAN CAT', '0034');
1 row created.
SQL>
Another is to use TO_DATE function with appropriate format mask:
SQL> INSERT INTO animal VALUES (00101,'Thurston','M', to_date('15-MAY-11', 'dd-mon-yy', 'nls_date_language = english'), 'MAINE COON CAT', '0034');
1 row created.
SQL>
The same goes for the consultation table.

Why do I receive error 1241 when trying to Create and Insert data into a new table (on phpMyAdmin)?

I am trying to create a new table and insert values into it however I keep receiving "#1241 - Operand should contain 1 column(s)".
Please could someone help to identify what is wrong with my code as I am unsure what this error is referencing?
The code I am inserting into the phpMyAdmin database under the SQL tab. I have tried to remove the auto increment attributes and have tried looking at other examples to check my syntax, but I can't spot the issue. Some guidance on this would be greatly appreciated.
The code I entered begins like this:
# AppSoft Project - Greg Roberts
DROP table if exists Department;
DROP table if exists Role;
DROP table if exists User;
DROP table if exists Appraisal;
DROP table if exists Question;
DROP table if exists Answer;
CREATE table Department(
DeptID int NOT NULL AUTO_INCREMENT,
DeptName varchar(30) NOT NULL,
primary key (DeptID));
INSERT into Department values(
(00, 'SuperAdmin'),
(01, 'Support Staff'),
(02, 'Teaching Staff'),
(03, 'SLT'));
CREATE table Role(
RoleID int NOT NULL AUTO_INCREMENT,
RoleTitle varchar(30) NOT NULL,
primary key (RoleID));
INSERT into Role values(
(00, 'SuperAdmin'),
(01, 'Office Administrator'),
(02, 'Teaching Partner'),
(03, 'Mid Day Supervisor'),
(04, 'Cooks'),
(05, 'Cleaners'),
(06, 'Maintenance'),
(07, 'Teacher'),
(08, 'Department Head'),
(09, 'SENCO'),
(10, 'Head Teacher'),
(11, 'Executive Head'));
Error Code that Occurs
You dont need to insert primary keys, if they are set to auto_increment.
DeptID int NOT NULL AUTO_INCREMENT
Just insert the department name, there are no additional braces required for Values.
INSERT into Department( DeptName) values
('SuperAdmin'),
('Support Staff'),
('Teaching Staff'),
('SLT');
You would need to do the same for Role table as well.
Also if you try to insert 0 into the primary key, it will actually insert 1 you can read about it in the Standard Docs
you seem to be getting the error because your first record inserts 1 into the table and then your second record tries to insert 1 again in the primary key column

Creating a Random Number in a Local Temporary Table

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.

SQL datable query to insert multiple column values

How to add multiple values in a single column of table in SQL? My table looks like this:
Create table emp
(
id number(5),
name varchar(25),
phone varchar(25)
);
Now I want to add values and multiple phones in the phone column. How to do that? I tried using
insert into emp values (id, name, phone)
values (1, lee, (23455, 67543));
but this is not working
Use two insert statements instead
insert into emp values (id, name,phone) values (1,'lee','23455');
insert into emp values (id, name,phone) values (1,'lee','67543');
or If you want to store both the values in single row
insert into emp values (id, name,phone) values (1,'lee','23455,67543');
Here table is not normalised. You either need to store Phone Number info in separate table or use two different column in same table.
Try changing you table design like this.
EMP table
CREATE TABLE emp
(
emp_id INT IDENTITY(1, 1) PRIMARY KEY,
name VARCHAR(25)
);
PhoneNumber Table
CREATE TABLE PhoneNumber
(
phoneno_id INT IDENTITY(1, 1),
emp_id INT,
Phone_Number int,
Cell_Number Int,
FOREIGN KEY (emp_id) REFERENCES emp(emp_id)
)
Note : Auto increment syntax may differ based on the database you are using.
The proper and only real well-designed way to do this in a relational setting is to use a separate table for your phones (this is in SQL Server specific syntax - it might be slightly different, depending on which concrete database system you're using):
Create table emp
(
id INT PRIMARY KEY,
name varchar(25)
)
create table phone
(
phoneId INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,
empid INT NOT NULL,
phone varchar(25) NOT NULL,
CONSTRAINT FK_Phone_Emp
FOREIGN KEY(empid) REFERENCES dbo.emp(id)
);
and then you insert the employee data into emp :
insert into emp(id, name)
values (1, lee);
and the phones into phone:
insert into phone(empid, phone) values(1, 23455);
insert into phone(empid, phone) values(1, 67543);
With this setup, you have proper normalization for the database, and you can store as many phones as you like, for each employee.