Creating an object in MYSQL Table - mysql

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

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.

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;

MYSQL: left Join and sum two tables where one table has two columns referring to the first table

I am trying to create a procedure where my transfer table is joined to my account table. In my transfer table, there are two FK columns that reference the account table id column.
account table:
CREATE TABLE account (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
number VARCHAR(30) NOT NULL DEFAULT '',
description VARCHAR(255)NOT NULL DEFAULT '',
is_active BIT(1) NOT NULL DEFAULT b'1',
PRIMARY KEY (id),
UNIQUE account_name (name, number)
);
transfer table:
CREATE TABLE transfer (
id INT NOT NULL AUTO_INCREMENT,
date DATE NOT NULL,
from_account INT NULL,
to_account INT NULL,
amount DECIMAL(12, 2) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (from_account)
REFERENCES account(id),
FOREIGN KEY (to_account)
REFERENCES account(id)
);
get_account procedure:
CREATE PROCEDURE get_account()
SELECT a.*,
(SUM(t.amount) - SUM(f.amount)) AS balance
FROM account a
LEFT JOIN transfer f
ON a.id = f.from_account
LEFT JOIN transfer t
ON a.id = t.to_account
GROUP BY a.id;
I am trying to subtract the total of the from_accout column from the total of the to_account column. I am able to get the sum of just one column but when I try to get both it returns a NULL.
This seems like it should be easy, but I can't figure it out.

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.

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