SELECT COMMAND SQL more than 1 value - sql

Im trying to create a select command to query the database I have and find different values from different tables in the database.

Try the following, I change all ID columns from CHAR to NUMBER
CREATE TABLE Actor
(actorID NUMBER,
lastName CHAR(24),
firstName CHAR(24),
middleName CHAR(24),
suffix CHAR(6),
gender CHAR(1),
birthDate DATE,
deathDate DATE)
/
CREATE TABLE Movie
(movieID NUMBER,
title CHAR(36),
year NUMBER,
company CHAR(50),
totalNoms NUMBER,
awardsWon NUMBER,
DVDPrice NUMBER(5,2),
discountPrice NUMBER(5,2))
/
CREATE TABLE Role
(roleID NUMER,
roleName CHAR(36),
gender CHAR(1),
actorID NUMBER,
movieID NUMBER)
/
CREATE TABLE Quote
(quoteID NUMBER,
quoteCHAR CHAR(255))
/
CREATE TABLE RoleQuote
(roleID NUMBER,
quoteID NUMBER)
/
and the select will be:
SELECT Movie.Title , Movie.Year , Actor.Firstname, Actor.lastname, Role.roleName, Quote.quoteCHAR
FROM Movie, Actor, Role, Quote, RoleQuote
WHERE Movie.movieID = Role.movieId AND
Actor.actorID = Role.ActorId AND
Role.roleID = RoleQuote.roleID AND
Quote.quoteID = RoleQuote.quoteID AND
Quote.quoteCHAR LIKE '%Houston, we have a problem.%'

Related

Returning query/table into a variable

Following the instructions from here, I am trying to return the results of a query into a variable like so
DROP DATABASE IF EXISTS test;
CREATE DATABASE test;
\c test
CREATE TABLE staff(
id BIGSERIAL PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
age SMALLINT
);
CREATE FUNCTION get_contacts(
p_id_min BIGINT,
p_id_max BIGINT,
p_age_min BIGINT,
p_age_max BIGINT
)
RETURNS #contacts TABLE (
first_name VARCHAR(50),
last_name VARCHAR(50)
)
AS
BEGIN
INSERT INTO #contacts
SELECT
first_name,
last_name
FROM
staff
WHERE
id >= p_id_min
AND
id <= p_id_max;
INSERT INTO #contacts
SELECT
first_name,
last_name
FROM
staff
WHERE
age >= p_age_min
AND
age <= p_age_max;
RETURN;
END;
But I am getting the following error
DROP DATABASE
CREATE DATABASE
You are now connected to database "test" as user "postgres".
CREATE TABLE
psql:test.sql:31: ERROR: syntax error at or near "#"
LINE 5: RETURNS #contacts TABLE (
^
psql:test.sql:33: ERROR: syntax error at or near "RETURN"
LINE 1: RETURN;
^
psql:test.sql:34: WARNING: there is no transaction in progress
COMMIT
I have 2 questions:
Why am I getting the syntax error at or near "#" and how do I fix thix?
What is the significance of # at the beginning of a variable name?
Turns out, in Postgres, the RETURN command can be used multiple times, so I just did the following
CREATE FUNCTION get_contacts(
p_id_min BIGINT,
p_id_max BIGINT,
p_age_min BIGINT,
p_age_max BIGINT
)
RETURNS TABLE(
id BIGINT,
first_name VARCHAR(50),
last_name VARCHAR(50),
age SMALLINT
)
AS $$
BEGIN
--RETURN FIRST QUERY
RETURN QUERY
SELECT * FROM staff
WHERE id >=p_id_min AND id<=p_id_max;
--DO OTHER WORK
...
--RETURN SECOND QUERY
RETURN QUERY
SELECT * FROM staff
WHERE age >=p_age_min AND age<=p_age_max;
END $$ LAnguage 'plpgsql';
Author was initially using # for parameters. For code you might also try this
CREATE FUNCTION get_contacts(
p_id_min BIGINT,
p_id_max BIGINT,
p_age_min BIGINT,
p_age_max BIGINT
)
RETURNS TABLE (
first_name VARCHAR(50),
last_name VARCHAR(50)
)
AS $$
BEGIN
INSERT INTO #contacts
SELECT
first_name,
last_name
FROM
staff
WHERE
id >= p_id_min
AND
id <= p_id_max;
INSERT INTO #contacts
SELECT
first_name,
last_name
FROM
staff
WHERE
age >= p_age_min
AND
age <= p_age_max;
RETURN;
END;

Oracle Database - REF unsupported data type

So I have three tables, Staff, Book, and Sale.
For Staff:
CREATE TYPE StaffType AS OBJECT (
ID VARCHAR2(5),
Name VARCHAR2(30)
);
/
CREATE TABLE Staff of StaffType;
For Book:
CREATE TYPE BookType AS OBJECT (
B_ID VARCHAR2(8));
/
CREATE TABLE Book of TicketType;
For Sale:
CREATE TYPE SaleType AS OBJECT (
S_ID VARCHAR2(8),
S_Date VARCHAR2(10),
Price NUMBER(10),
With REF StaffType,
For REF BookType );
/
CREATE TABLE Sale of SaleType (
Scope for (With) is Staff,
Scope for (For) is Book);
So I inserted a row in Sale table like this
insert into Sale VALUES(SaleType('S12','33th',22,(SELECT REF(c) FROM Staff c WHERE c.ID='C13'),(SELECT REF(t) FROM Book t WHERE t.B_ID='T18'));
however, I get the following error:
ORA-00917: missing comma
I don't understand why I am getting this error.. What comma did I miss?
EDIT: I followed the solution of #thatjeffsmith and although the insert statement works, when I try to view my table, I get the following error:
ORA-00932: inconsistent datatypes: expected CHAR got REF SQL_VTJOLYEBVKAOJEHORTCXVPTBP.STAFFTYPE
here is the code.. I removed HR. before Book and Staff from sale table declaration because that was causing the worksheet to show an error stating that Staff and Book are out of scope. Here is the code:
CREATE TYPE STAFFTYPE AS OBJECT (
ID VARCHAR2(5),
NAME VARCHAR2(30)
);
/
CREATE TABLE STAFF OF STAFFTYPE;
/
INSERT INTO STAFF VALUES('C13','James');
/
CREATE TYPE BOOKTYPE AS OBJECT (
B_ID VARCHAR2(8)
);
/
CREATE TABLE BOOK OF BOOKTYPE;
/
INSERT INTO BOOK VALUES('T18');
/
CREATE TYPE SALETYPE AS OBJECT (
S_ID VARCHAR2(8),
S_DATE VARCHAR2(10),
PRICE NUMBER(10),
WITHS REF STAFFTYPE,
FORS REF BOOKTYPE
);
/
CREATE TABLE SALE OF SALETYPE (
SCOPE FOR ( WITHS ) IS STAFF,
SCOPE FOR ( FORS ) IS BOOK
);
INSERT INTO SALE VALUES ( SALETYPE(
'S12', '33th',
22,
(
SELECT REF(C)
FROM STAFF C
WHERE C.ID = 'C13'
),
(
SELECT REF(T)
FROM BOOK T
WHERE T.B_ID = 'T18'
)
) );
SELECT * FROM SALE;
WITH and FOR are keywords, not available as a table or column name. You could "QUOTE" these, but DO NOT DO THAT - it will make your life much harder going forward.
Your BOOK table is wrong, you're using a TYPE that doesn't exist per your example.
And your INSERT is missing the last ')'
CREATE TYPE STAFFTYPE AS OBJECT (
ID VARCHAR2(5),
NAME VARCHAR2(30)
);
/
CREATE TABLE STAFF OF STAFFTYPE;
CREATE TYPE BOOKTYPE AS OBJECT (
B_ID VARCHAR2(8)
);
/
CREATE TABLE BOOK OF BOOKTYPE;
CREATE TYPE SALETYPE AS OBJECT (
S_ID VARCHAR2(8),
S_DATE VARCHAR2(10),
PRICE NUMBER(10),
WITHS REF STAFFTYPE,
FORS REF BOOKTYPE
);
/
CREATE TABLE SALE OF SALETYPE (
SCOPE FOR ( WITHS ) IS HR.STAFF,
SCOPE FOR ( FORS ) IS HR.BOOK
);
INSERT INTO SALE VALUES ( SALETYPE(
'S12', '33th',
22,
(
SELECT REF(C)
FROM STAFF C
WHERE C.ID = 'C13'
),
(
SELECT REF(T)
FROM BOOK T
WHERE T.B_ID = 'T18'
)
) );

Teradata error SPL 1076 - The right parenthesis in parameter declaration is missing

I have created a blank table in Teradata called EMPLOYEE
CREATE MULTISET VOLATILE TABLE EMPLOYEE (
EmployeeNo INTEGER,
FirstName VARCHAR(30),
LastName VARCHAR(30),
DOB DATE FORMAT 'YYYY-MM-DD',
JoinedDate DATE FORMAT 'YYYY-MM-DD',
DepartmentNo BYTEINT
)
PRIMARY INDEX ( EmployeeNo ) On COMMIT PRESERVE ROWS;
Table is created.
Now i am trying to define a procedure to enter data into the table.
CREATE PROCEDURE InsertEmployee (
IN in_EmployeeNo INTEGER, IN in_FirstName VARCHAR(30),
IN in_LastName VARCHAR(30), IN in_DOB DATE FORMAT 'YYYY-MM-DD',
IN in_JoinedDate DATE FORMAT 'YYYY-MM-DD', IN in_DepartmentNo BYTEINT
)
BEGIN
INSERT INTO EMPLOYEE(
EmployeeNo,
FirstName,
LastName,
DOB,
JoinedDate,
DepartmentNo
)
VALUES (
:in_EmployeeNo,
:in_LastName,
:in_FirstName,
:in_DOB,
:in_JoinedDate,
:in_DepartmentNo
);
END;
This is where I repeatedly get 2 errors:
SPL1076:E(L3), The right parenthesis in parameter declaration is missing.
SPL1048:E(L3), Unexpected text ';' in place of SPL statement.

Displaying Multiple SQL Fields as a Single Field

I have the following tables:
CREATE TABLE title (
booktitle VARCHAR( 60 ),
title_id CHAR( 6 ),
au_id CHAR( 11 ),
PRIMARY KEY (title_id)
)
create table authors (
au_id char(11),
au_lname varchar(20),
au_fname varchar(20),
phone varchar(10),
address varchar(30),
city varchar(20),
state char(2),
zip char(5));
I need to somehow show the booktitle, au_lname and au_fname in one field.
I want to order it by CITY, but that should be simple enough.
So far I have made a view of this data, but I am not sure how to make it into a single field? I tried using the "AS" command, but I keep getting errors.
Here is my working view:
CREATE VIEW authorname AS
SELECT
title.booktitle, authors.au_fname, authors.au_lname
FROM
title
INNER JOIN authors ON title.au_id = authors.au_id
ORDER BY authors.city
You can use CONCAT() to concatenate string values:
CREATE VIEW authorname AS
SELECT
CONCAT(title.booktitle, authors.au_fname, authors.au_lname) as name
FROM title
INNER JOIN authors ON title.au_id = authors.au_id
ORDER BY authors.city
or if you want to separate the values by a certain character, use CONCAT_WS():
CONCAT_WS(' ', title.booktitle, authors.au_fname, authors.au_lname) as name

Moving away from STI - SQL to break single table into new multi-table structure

I am moving old project that used single table inheritance in to a new database, which is more structured. How would I write a SQL script to port this?
Old structure
I've simplified the SQL for legibility.
CREATE TABLE customers (
id int(11),
...
firstName varchar(50),
surname varchar(50),
address1 varchar(50),
address2 varchar(50),
town varchar(50),
county varchar(50),
postcode varchar(50),
country varchar(50),
delAddress1 varchar(50),
delAddress2 varchar(50),
delTown varchar(50),
delCounty varchar(50),
delPostcode varchar(50),
delCountry varchar(50),
tel varchar(50),
mobile varchar(50),
workTel varchar(50),
);
New structure
CREATE TABLE users (
id int(11),
firstName varchar(50),
surname varchar(50),
...
);
CREATE TABLE addresses (
id int(11),
ForeignKey(user),
street1 varchar(50),
street2 varchar(50),
town varchar(50),
county varchar(50),
postcode varchar(50),
country varchar(50),
type ...,
);
CREATE TABLE phone_numbers (
id int(11),
ForeignKey(user),
number varchar(50),
type ...,
);
With appropriate cross-database notations for table references if appropriate:
INSERT INTO Users(id, firstname, surname, ...)
SELECT id, firstname, surname, ...
FROM Customers;
INSERT INTO Addresses(id, street1, street2, ...)
SELECT id, street1, street2, ...
FROM Customers;
INSERT INTO Phone_Numbers(id, number, type, ...)
SELECT id, phone, type, ...
FROM Customers;
If you want both the new and the old address (del* version), then repeat the address operation on the two sets of source columns with appropriate tagging. Similarly, for the three phone numbers, repeat the phone number operation. Or use a UNION in each case.
First make sure to backup your existing data!
The process is differnt if you are going to use the original id field or generate a new one.
Assuming you are going to use the orginal, make sure that you have the ability to insert id fields into the table before you start (the SQL Server equivalent if you are autogenrating the number is Set identity Insert on, not sure what mysql would use). Wirte an insert from the old table to the parent table:
insert newparenttable (idfield, field1, field2)
select idfield, field1, field2 from old parent table
then write similar inserts for all the child tables depending on what fields you need. Where you have multiple phone numbers in differnt fields, for instance, you would use a union all stament as your insert select.
Insert newphone (phonenumber, userid, phonetype)
select home_phone, id, 100 from oldparenttable
union all
select work_phone, id, 101 from oldparenttable
Union all
select cell_phone, id, 102 from oldparenttable
If you are going to have a new id generated, then create the table with a field for the old id. You can drop this at the end (although I'd keep it for about six months). Then you can join from the new parent table to the old parent table on the oldid and grab the new id from the new parent table when you do you inserts to child tables. Something like:
Insert newphone (phonenumber, userid, phonetype)
select home_phone, n.id, 100 from oldparenttable o
join newparenttable n on n.oldid = o.id
union all
select work_phone, n.id, 101 fromoldparenttable o
join newparenttable n on n.oldid = o.id
Union all
select cell_phone, n.id, 102 from oldparenttable o
join newparenttable n on n.oldid = o.id