Function .JSON_OBJECT does not exist - mysql

I have 2 tables and I try to get statuses from table status and add to each status info about an author from a user table. I created the same database on my laptop and everything works fine. But I've got an error when published the database on Heroku using ClearDB:
[42000][1305] FUNCTION heroku_1ecaeb2cbfaf113.JSON_OBJECT does not exist
Without JSON_OBJECT query works fine. What's going on? Where I made a mistake?
My tables:
CREATE TABLE IF NOT EXISTS user (
login VARCHAR(15) NOT NULL PRIMARY KEY,
name VARCHAR(15),
avatar VARCHAR(150),
ts INT,
hash BINARY(60)
);
CREATE TABLE IF NOT EXISTS status (
id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
note VARCHAR(300),
author VARCHAR(15),
ts INT,
replyTo INT UNSIGNED
);
My query:
SELECT
status.id,
status.note,
status.ts,
JSON_OBJECT(
'login', statusAuthor.login,
'name', statusAuthor.name
)
FROM
user,
user statusAuthor
INNER JOIN status ON statusAuthor.login = status.author
WHERE
user.login = 'some_user' AND status.replyTo IS NULL
ORDER BY status.id
DESC
LIMIT 11;

Related

how can I create second table in mysql? 1050 error

i'm creating second table in HEIDI SQL, but it doesn't work.
it shows 1050 error: user1 aleady exists.
I aleady created user1 table and I try to create user2 table. However it doesn't
#2. TABLE
CREATE TABLE user1(
user_id INT,
name VARCHAR(20),
email VARCHAR(30),
age INT(3),
rdata DATE
);
# TABLE 2
CREATE TABLE user2(
user_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
email VARCHAR(30) UNIQUE NOT NULL,
age INT(3) DEFAULT 30,
rdate TIMESTAMP
);
You can run this script, which will avoid this error "1050 error: user1 aleady exists.":
CREATE TABLE IF NOT EXISTS `user1 ` (
user_id INT,
name VARCHAR(20),
email VARCHAR(30),
age INT(3),
rdata DATE
);
NB: your user1 table doesn't have a primary key and for the age column, it's advisable to store the date of birth because the age can be calculated.
I would advise you to write the script using drop table if exists:
DROP TABLE IF EXISTS user1;
CREATE TABLE user1 (
user_id INT,
name VARCHAR(20),
email VARCHAR(30),
age INT(3),
rdata DATE
);
# TABLE 2
DROP TABLE IF EXISTS;
CREATE TABLE user2(
user_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
email VARCHAR(30) UNIQUE NOT NULL,
age INT(3) DEFAULT 30,
rdate TIMESTAMP
);
CREATE TABLE IF NOT EXISTS will leave the previous version of the table -- which is troublesome if you make changes to the definitions. Using such constructs, I have spent a fair amount of time debugging code when the structure of a table didn't change.

advice on database design

I am building a database for my website. I have a schema here : enrolin.in/download.png
Introduction to website:
It collects data from students on behalf of colleges.
Now what I have done in database is that I have created a UDT-where data that is fixed to a student is stored. and CST-where data that can change with every new course is stored.
I am very new to database design, so please have a look at the database design and advice me where i can improve. I want it to be perfect so that there are no complications/limitations with database in future
NOTES:
There is not just for one college. It is like a platform where different colleges can add their courses. Every college has different courses with different subjects etc. There can be many CST's, may be a separate CST for every new course. Plus I am not sure about the way I am storing course data , I mean CST and UDT. Every college will have access to the data of students which have applied for a course under that college and every student will have access to data to his previously filled form data,status etc.
Thanks in advance. If anything is unclear, please ask in comments.
Update : -- tables
-- Table CST
CREATE TABLE CST (
cst_id int NOT NULL ,
rollno int NOT NULL ,
semester int NOT NULL ,
CONSTRAINT CST_pk PRIMARY KEY (cst_id)
);
-- Table UDT
CREATE TABLE UDT (
udt_id int NOT NULL ,
id int NOT NULL ,
name varchar(255) NOT NULL ,
gender varchar(255) NOT NULL ,
fatherame varchar(255) NOT NULL ,
mothername varchar(255) NOT NULL ,
dob date NOT NULL ,
signature binary(255) NOT NULL ,
CONSTRAINT UDT_pk PRIMARY KEY (udt_id)
);
-- Table address
CREATE TABLE address (
address_id int NOT NULL ,
id int NOT NULL ,
add_name varchar(255) NOT NULL ,
add_street varchar(255) NOT NULL ,
city varchar(255) NOT NULL ,
state varchar(255) NOT NULL ,
country varchar(255) NOT NULL ,
pin int NOT NULL ,
CONSTRAINT address_pk PRIMARY KEY (address_id)
);
-- Table courses
CREATE TABLE courses (
course_id int NOT NULL ,
course_name varchar(255) NOT NULL ,
CONSTRAINT courses_pk PRIMARY KEY (course_id)
);
-- Table phones
CREATE TABLE phones (
phone_id int NOT NULL ,
id int NOT NULL ,
phone int NOT NULL ,
CONSTRAINT phones_pk PRIMARY KEY (phone_id)
);
-- Table photos
CREATE TABLE photos (
photo_id int NOT NULL ,
id int NOT NULL ,
photo binary(255) NOT NULL ,
CONSTRAINT photos_pk PRIMARY KEY (photo_id)
);
-- Table sub_trans
CREATE TABLE sub_trans (
sub_trans_id int NOT NULL ,
transactions_t_id int NOT NULL ,
subjects_sub_id int NOT NULL ,
CONSTRAINT sub_trans_pk PRIMARY KEY (sub_trans_id)
);
-- Table subjects
CREATE TABLE subjects (
sub_id int NOT NULL ,
course_id int NOT NULL ,
subjectname varchar(255) NOT NULL ,
CONSTRAINT subjects_pk PRIMARY KEY (sub_id)
);
-- Table transactions
CREATE TABLE transactions (
t_id int NOT NULL ,
id int NOT NULL ,
course_id int NOT NULL ,
p_status int NOT NULL DEFAULT 0 ,
phones_phone_id int NOT NULL ,
UDT_udt_id int NOT NULL ,
photos_photo_id int NOT NULL ,
address_address_id int NOT NULL ,
CST_cst_id int NOT NULL ,
CONSTRAINT transactions_pk PRIMARY KEY (t_id)
);
-- Table users
CREATE TABLE users (
id int NOT NULL ,
name varchar(255) NOT NULL ,
email varchar(255) NOT NULL ,
password int NOT NULL ,
CONSTRAINT users_pk PRIMARY KEY (id)
);
`
UDT:
Typo fathername. UDT has an FK to what? Users? Slim down the varchar size in UDT. Sure you want a single column for name? Ok, at least document that you chose to not have more than 2 parents. Otherwise you need another table.
Users:
Passwords run thru a hash function won't be ints and user needs a Salt for optimal security. Imho.
awaiting more clarification below
There is zero purpose of UDT and users both existing. Pick one, pickup lost fields from one you are ditching.
Even hypothetically if it wasn't 1:1 name would denomalize the database.
phones:
Your columns for phones need to be described. Also can a user have more than 1 phone (if so, phoneType varchar(10) or int code? )
Also I would go like varchar(30) on phone value not an int.
Addresses:
Same deal as phones. Can a user have more than one? PostalCode pin should be varchar
Transactions:
This seems to be the hub of your schema. All of your transactions hinge on FKs to data that may have subsequently changed. I see RISK in that. Older transaction could get skewed results for lookups. There are easy ways to fix this issue if you even see it as one. Same goes for courses. What if History 101's id stays the same but next semester that id is Early Roman Empire? Just things to think about.
Lastly the documented tables have no FK relationships shown.

SELECT issue in MySQL

I am trying to perform a SELECT but I am not able to retrieve the data I want.
I have tried lots of sentences without success, so that's why I am exposing here the whole problem.
Here is my database (four tables):
-- Table users
CREATE TABLE users (
user_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
device_id VARCHAR(15),
private_user_id VARCHAR(16),
public_user_id VARCHAR(16)
);
-- Table events
CREATE TABLE events (
event_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
public_event_id VARCHAR(8),
ref_user_id INT NOT NULL REFERENCES users(user_id),
name VARCHAR(64),
description VARCHAR(256)
);
-- Table proposals
CREATE TABLE proposals (
proposal_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
public_proposal_id VARCHAR(8),
ref_event_id INT NOT NULL REFERENCES events(event_id),
proposal_date VARCHAR(32),
proposal_location VARCHAR(64),
proposal_type INT
);
-- Table responses
CREATE TABLE responses (
response_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
ref_user_id INT NOT NULL REFERENCES users(user_id),
ref_proposal_id INT NOT NULL REFERENCES proposals(proposal_id),
user_name VARCHAR(32),
response INT
);
There are users. There are events. Events are created by one user. Events have proposals. Proposals have responses from the users. I need:
I need to achieve this given a private user id. Is something like: given
the private user id, analyze the responses of that user, so analyze
the proposals of that responses, and finally get the events of that
responses, RETURNING THE PUBLIC USER ID of the creator of each event.
I have tried with:
SELECT DISTINCT events.ref_user_id, public_event_id, name, description, public_user_id
FROM events, users, proposals, responses
WHERE responses.ref_user_id = user_id
AND private_user_id = 'lk0X3lOQl972U7pB'
but the public_user_id retrieved is the user that has responded, while I need the event creator.
SELECT ALL -- show all
u.pulic_user_id AS creator_pulic_user_id, -- creators' public IDs
e.name AS event_name -- and events' names
FROM -- from
events AS e -- events
JOIN -- and
users AS u -- their creator
ON e.ref_user_id = u.user_id
WHERE -- where
EXISTS -- exists
( SELECT 1 -- a
FROM responses AS r -- response
JOIN users AS ur -- by a user,
ON ur.user_id = r.ref_user_id
JOIN proposals AS p -- for a proposal
ON p.proposal_id = r.ref_proposal_id
WHERE p.ref_event_id = e.event_id -- that is about that event
AND ur.private_user_id = #X -- and the responding user
) ; -- has private ID = #X
Sidenote: Inline foreign key constraints, like this:
ref_user_id INT NOT NULL REFERENCES users(user_id),
are parsed (only for syntax correctness) and ignored in MySQL. You have to add them in the end of a CREATE TABLE statement to be valid and enforced. Or use ALTER TABLE, if you have already inserted data in the tables.
If it's just the ids that are wrong it's because you're returning public_user_id from the user table, you want events.ref_user_id, try this:
SELECT DISTINCT events.ref_user_id AS public_user_id , public_event_id, name, description
FROM events, users, proposals, responses
WHERE (responses.ref_user_id = user_id
AND ref_proposal_id = proposal_id AND ref_event_id = event_id)
Edit
I think this is what you want:
SELECT DISTINCT e.ref_user_id AS public_user_id
FROM events e, users u, proposals p, responses r
WHERE r.ref_user_id = u.user_id
AND r.ref_proposal_id = p.proposal_id
AND p.ref_event_id = e.event_id
AND u.private_user_id = 'lk0X3lOQl972U7pB'
;

Selecting a default value if an entry doesn't exist

I have three tables...users, user_info, and quota_levels. They look like this:
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(31) NOT NULL,
password VARCHAR(33) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE user_info (
userID INT UNSIGNED NOT NULL,
firstName VARCHAR(32),
lastName VARCHAR(32),
phone CHAR(14),
address VARCHAR(128),
birthdate DATE,
misc TEXT,
quotaLevel VARCHAR(32),
PRIMARY KEY (userID)
);
CREATE TABLE quota_levels (
quotaLevel VARCHAR(32) NOT NULL,
quota1 INT NOT NULL,
quota2 INT NOT NULL,
PRIMARY KEY (level)
);
Every user will have an entry in the users table, but not necessarily in the user_info table. Each user in the user_info table has a quotaLevel corresponding to the quotaLevel column in the quota_levels table. Possible values for quotaLevel are BRONZE, SILVER, GOLD, and PLATINUM.
I could go into a long explanation of why it is set up this way, but it would be quicker to just say that this structure cannot be changed.
If the user exists, I want to get the quota1 value of their quotaLevel. If the user doesn't exist, the quota1 value for BRONZE should be returned.
I want to do this with ONE query. Can it be done and how?
SELECT u.Name,
COALESCE(ql.quota1, (SELECT quota1 FROM quota_level WHERE quotaLevel = 'BRONZE'))
FROM users u
LEFT JOIN user_info ui
INNER JOIN quota_level ql
ON ui.quotaLevel = ql.quotaLevel
ON u.id = ui.userID

selecting a user of every type

I have a query against two databases that I'm trying to execute. The first table is just user information and is referenced by a privilege table. For my query I'm trying to find a set of super users, they are users that have every current privilege in the privilege table. It is set up as follows:
create table MEMBER
( id int not null,
name varchar(10),
bdate date,
sex char,
pc_id int not null,
PRIMARY KEY (mid),
FOREIGN KEY (pc_id) REFERENCES PLEDGECLASS(id))
create table MEMBER_PRIVILEGE
( mid int not null,
pid int not null,
PRIMARY KEY (mid,pid),
FOREIGN KEY (mid) REFERENCES MEMBER(id),
FOREIGN KEY (pid) REFERENCES PRIVILEGE(id))
create table PRIVILEGE
( id int,
pname varchar(15)
PRIMARY KEY(id))
Although obviously the incorrect query, I'm trying to do something equivalent to the following:
Select name
From MEMBER,MEMBER_PRIVILEGE
Where id=mid AND pid = ALL (select id
From PRIVILEGE);
SELECT *
FROM MEMBER
WHERE NOT EXISTS (
SELECT *
FROM MEMBER_PRIVILEGE LEFT JOIN PRIVILEGE ON MEMBER_PRIVILEGE.pid = PRIVILEGE.id)
WHERE MEMBER_PRIVILEGE.mid = MEMBER.id AND PRIVILEGE.id IS NULL
)
Try this ::
Select name, (select count(id) from PRIVILIGE) as p_count
From MEMBER m
inner join MEMBER_PRIVILEGE mp on (m.id=mp.mid)
group by mp.mid having count(*) = p_count;