how can I create second table in mysql? 1050 error - mysql

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.

Related

Please, can someone help me to create a trigger in mysql that has a same function with the assertion below?

So basically I want to create a trigger for mysql database that has same/similar function with the assertion code below (since mysql does not support assertion)
Here is the assertion code that I want to replicate using trigger
CREATE ASSERTION assert
CHECK NOT EXISTS(SELECT * FROM paper P WHERE 3 <>(SELECT COUNT(*)
FROM review R
WHERE R.paperid = P.paperid)
);
CREATE ASSERTION atmostfivepapers
CHECK NOT EXISTS(SELECT * FROM pcmember P WHERE 5 <
( SELECT *
FROM review R
WHERE R.email = P.email
)
);
And here is my table
CREATE TABLE paper(
paperid INT UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(50) NOT NULL,
abstract VARCHAR(250),
pdf VARCHAR(100),
PRIMARY KEY(paperid)
);
CREATE TABLE author(
email VARCHAR(100) NOT NULL,
name VARCHAR(50),
affiliation VARCHAR(100),
PRIMARY KEY(email)
);
CREATE TABLE writePaper(
paperid INT UNSIGNED NOT NULL AUTO_INCREMENT,
email VARCHAR(100),
paper_order INT,
PRIMARY KEY(paperid, email),
FOREIGN KEY(paperid) REFERENCES paper(paperid),
FOREIGN KEY(email) REFERENCES author(email)
);
CREATE TABLE pcmember(
email VARCHAR(100) NOT NULL,
name VARCHAR(20),
PRIMARY KEY(email)
);
CREATE TABLE review(
reportid INT UNSIGNED,
sdate DATE,
comment VARCHAR(250),
recommendation CHAR(1),
paperid INT UNSIGNED,
email VARCHAR(100),
PRIMARY KEY(paperid, email),
FOREIGN KEY(paperid) REFERENCES paper(paperid),
FOREIGN KEY(email) REFERENCES pcmember(email)
);
Thanks in advance
First create another table replicating the columns and their features. Thereafter create a trigger.
CREATE TRIGGER afterInsert_trigger on yourMainTableName
FOR INSERT
AS
INSERT INTO yourTableCopy(column1ofYourReplicateTable, column2OFyourReplicateTable, ... etc)
SELECT column1ofYourMainTable, column2ofYourMainTable, ..., etc
FROM INSERTED
That's what I use in Microsoft SQL server

Creating an object in MYSQL Table

I am trying to make Table1 which will have columns email, datecreated and watchlist object. I want the watchlist object to hold the columns ticker (string), stockname(string), price(float), volume(int) and column notes which will be a long string. So I was not sure if I could have a watchlist object column in table 1 or would I have to reference another field? Should I just created two different tables: Table 1 and WatchList?
Table 1
Email
DateCreated
Watchlist( ticker
StockName
PRICE
Volume)
OR
Table 1
Email
DateCreated
Watchlist
Watchlist
Ticker
StockName
Price
Volume
CREATE TABLE IF NOT EXISTS userName (
id int NOT NULL AUTO_INCREMENT,
username ...,
password_hash ...,
...,
email varchar(255) NOT NULL,
DateCreated date default current_timespamp(),
PRIMARY KEY (id)
);
Watchlist for each user
CREATE TABLE IF NOT EXISTS userID_WatchL(
id int NOT NULL AUTO_INCREMENT,
ticker varchar(255) NOT NULL,
stockname varchar(255) NOT NULL,
price int NOT NULL, //Why ever u wanne use float!?
volume int NOT NULL
PRIMARY KEY (id)
);
Thats it. On each register you also create the second table

How do I add data from different tables into one new table in mySQL?

CREATE TABLE CU_ORDER
( cordernumber INT PRIMARY KEY,
fname_lname VARCHAR(50) NOT NULL,
product_name VARCHAR(100) NOT NULL,
);
CREATE TABLE TRACKING_NUMBER
( trnumber INT PRIMARY KEY
);
INSERT INTO CU_ORDER VALUES(456, 'John Doe' , Table);
INSERT INTO TRACKING_NUMBER(276734673);
I am trying to created a table called Package and in the table it will have all the items from cu_order and all the items from tracking_number. How will I add all of the attributes of this table to one table. What am I doing wrong?
CREATE TABLE PACKAGE
( orderno INT PRIMARY KEY,
fname VARCHAR(50) NOT NULL,
name VARCHAR(100) NOT NULL,
trno INT PRIMARY KEY);
INSERT INTO PACKAGE (........
The two tables do not seem to have a relation, so, presumably, you want a cartesian product of both tables. If so, you can use the insert ... select ... syntax with a cross join:
insert into package(orderno, fname, name, trno)
select
co.cordernumber,
co.fname_lname,
co.product_name,
tn.trnumber
from cu_order co
cross join tracking_number tn
This inserts all possible combinations of rows from both source tables in the target table.
You should also fix the declaration of the package table: yours has two primary keys, which is not allowed. Instead, you probably want a compound primary key made of both columns:
create table package (
orderno int,
fname varchar(50) not null,
name varchar(100) not null,
trno int,
primary key(orderno, trno)
);
You can create a new table from the data of another table (or several tables) by appending a SELECT statement to the CREATE TABLE statement.
However, your two source tables are missing the 1:1 relation allowing this to work, which I assume is the cordernumber of CU_ORDER. It appears the table TRACKING_NUMBER is missing a 'cordernumber' column.
CREATE TABLE TRACKING_NUMBER (
trnumber INT PRIMARY KEY,
cordernumber INT
);
After you added the column 'cordernumber' to TRACKING_NUMBER, you can create the new table PACKAGE with:
CREATE TABLE PACKAGE (
orderno INT PRIMARY KEY,
fname VARCHAR(50) NOT NULL,
name VARCHAR(100) NOT NULL,
trno INT PRIMARY KEY
)
SELECT
CU_ORDER.cordernumber AS orderno,
CU_ORDER.fname_lname AS fname,
CU_ORDER.product_name AS name,
TRACKING_NUMBER.trnumber AS trno
FROM CU_ORDER, TRACKING_NUMBER
WHERE CU_ORDER.cordernumber=TRACKING_NUMBER.cordernumber;

Function .JSON_OBJECT does not exist

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;

Create MySQL Trigger to update multiple records in another table

Im not to sure if this is possible. I'm developing a fantasy golf tournament App asr a project. The user picks 6 golfers from six groups, each group contains ten golfers. The group that the golfer is in is determined by the group boolean in the golfers table. The scores table is used to record the competition entries.
I have two tables. A golfers table.
CREATE TABLE golfers (
golferid INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
secondtname VARCHAR(30) NOT NULL,
country VARCHAR(50),
worldranking int(3),
tournamentposition int(2),
group1 boolean,
group2 boolean,
group3 boolean,
group4 boolean,
group5 boolean,
group6 boolean,
day1score int(2),
day2score int(2),
day3score int(2),
day4score int(2),
totalscore int(3),
golfscoretotal int(3)
);
And a scores table. As seen below.
CREATE TABLE scores (
scoreid INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
userid INT(11) NOT NULL,
golferid1 INT(6),
golfscoretotal1 INT(3),
golferid2 INT(6),
golfscoretotal2 INT(3),
golferid3 INT(6),
golfscoretotal3 INT(3),
golferid4 INT(6),
golfscoretotal4 INT(3),
golferid5 INT(6),
golfscoretotal5 INT(3),
golferid6 INT(6),
golfscoretotal6 INT(3),
totalscore INT(4),
FOREIGN KEY fk_userid REFERENCES users(id)
);
Is it possible to update the scores in the scores table (taken from the golfers table) for each golfer based on the id of the golfer in the golferid1 INT(6) column before the golfscoretotal1 column.
You could use something simple like this for golferid1:
CREATE TRIGGER update_scores1 After INSERT ON golfers FOR EACH ROW
UPDATE scores
SET golfscoretotal1 = new.golfscoretotal
WHERE golferid1 = NEW.golferid
Then for the others just create more triggers like this, resulting in a total of 6 triggers:
CREATE TRIGGER update_scores2 After INSERT ON golfers FOR EACH ROW
UPDATE scores
SET golfscoretotal2 = new.golfscoretotal
WHERE golferid2 = NEW.golferid