User table shows NULL data on multi table - mysql

I was practicing the multi table on mysql. While creating the table, null data appeared on 'user table'. The user table should contain user_id(auto_increment). user_name should appear on user_info table which is from orders table.
create table orders(
primary key (order_id),
order_id int(11) not null auto_increment,
order_no varchar(100) default null,
user_name varchar(100) not null,
product_name varchar(100) not null,
order_status enum('CART', 'DRAFT', 'NEW', 'IN PROCESS', ' COMPLETED', 'FAILED' ) DEFAULT 'CART',
carete_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '注册时间');

There isn't any code which would insert anything into the "users" table, so I'm not sure why you would expect it to contain any data?
Your line
select distinct user_name from orders;
just after the create statement for the users table will simply display a list on screen. Perhaps you meant to write
insert into users (user_name) select distinct user_name from orders;
This will actually add some rows to the users table.

Related

MySQL: Created Table won't show values?

I'm new to MySQL and I created a table called students with some attributes but when I run Select * From Students it appears as null for each category I'm not sure why
use practice;
create table Students(
sid integer default 1,
sname varchar(50) default 'Joe',
GPA real default 1.7,
dateOfBirth date default (2000-12-12),
primary key(sid)
);
select *
from Students
You can try to insert a value in your table (Insert into Students ('sid', 'sname', 'GPA', 'dateOfBirth') Values ('NULL', 'joe', '1.7', '200-12-12'); this will solve your problem with the SELECT *
I would also add an auto increment on your sid so you can putt the value of sid to null and it will automatically ALTER TABLE Students MODIFY 'sid' INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY;

MySQL INSERT INTO statement giving ERROR CODE 1062

I have a F1 database and I'm trying to insert the team principal's name (from Principals table) into the Teams table, but for some reason it won't work. I'm not sure if my insert into statement is wrong, but I can't see why it would be. The code is below
CREATE TABLE Teams (
Team_Name VARCHAR(30) NOT NULL,
Driver_1 INT NULL,
Driver_2 INT NULL,
Nation VARCHAR(30) NOT NULL,
Seasons INT NOT NULL,
No_Titles INT NOT NULL DEFAULT 0,
Principal VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (Team_Name));
CREATE TABLE Principals (
Principal_No INT NOT NULL,
Principal_Name VARCHAR(30) UNIQUE NOT NULL,
Team VARCHAR(30) NULL,
Age INT NOT NULL,
Nationality VARCHAR(30) NOT NULL,
Seasons INT NOT NULL DEFAULT 0,
PRIMARY KEY (Principal_No),
FOREIGN KEY (Team) REFERENCES Teams(Team_Name));
Here's the insert statement. The Principals table has already been populated with the principal names and their corresponding teams
INSERT INTO Teams (Principal)
SELECT Principal_Name
FROM Principals
WHERE Team IN(
SELECT Team_Name
FROM Teams);
In your INSERT statement
INSERT INTO Teams (Principal)
-- ^
you're only inserting a value for the field Principal into the table Teams. But the Teams table has other fields as well... not inserting values into them will default them to NULL. This will lead to a contradiction in the arguments. In your Teams table:
Team_Name VARCHAR(30) NOT NULL
Nation VARCHAR(30) NOT NULL
Seasons INT NOT NULL
these fields, by definition, can't be NULL. But since no values are inserted with the INSERT statement, these will default to NULL, clashing with the definition and causing an error.
You may want to consider setting default values for the above fields or modifying your INSERT statement to accommodate those fields.
INSERT INTO Teams (Principal, Team_Name, Nation, Seasons)
SELECT
-- four columns ...
Your insert statement tries to assign null values to columns that have 'not null' property. Check these columns 'not null' -> false or assign default value or define values in your insert statement. I hope this help

Auto_increment trigger

I need to auto_increment the primary key in a mysql database using a trigger. Unfortunately, I am not quite sure how to do this. In the sample code I have provided, I need the employee table primary key to auto_increment beginning with an empty table and a starting value of 200. Then, I need each new insert to increment by 1.Thanks for looking and I hope you are able to help me.
CREATE TABLE department (
dept_name VARCHAR(50) NOT NULL Primary Key
);
CREATE TABLE employee (
emp_id INT(6) unsigned Default 0 Not NULL
, last_name VARCHAR(25) NOT NULL
, first_name VARCHAR(40) NOT NULL
, dept_name VARCHAR(50) NOT NULL
, PRIMARY KEY(emp_id, dept_name)
, FOREIGN KEY(dept_name) REFERENCES department (dept_name)
);
There are several things you need to do:
Declare the emp_id column as AUTO_INCREMENT;
Set the value of AUTO_INCREMENT property of the table to 200;
Do not provide any value for column emp_id when you INSERT rows in table employee.
Change the table creation as below:
CREATE TABLE employee (
emp_id INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
last_name VARCHAR(25) NOT NULL,
first_name VARCHAR(40) NOT NULL,
dept_name varchar(50) NOT NULL
PRIMARY KEY(emp_id),
FOREIGN KEY(dept_name) REFERENCES department_tbl(dept_name)
) AUTO_INCREMENT=200;
If the table has an AUTO_INCREMENT column then it must be the PRIMARY KEY of the table. I removed dept_name from the definition of the PK above. I also removed the default value 0 from the emp_id column. It's default value is generated by MySQL using the AUTO_INCREMENT policy.
When you INSERT a new record into the employee table all you have to do is to not provide any value for the emp_id column:
INSERT INTO employee (last_name, first_name, dept_name)
VALUES ('Doe', 'John', 'accounting');
Then use the LAST_INSERT_ID() MySQL function to retrieve the value of the emp_id generated on insertion.
The language or the library you use to develop the client application probably has a function that wraps LAST_INSERT_ID() and returns its value.

JOIN LEFT with multiple conditions

I'm having following tables structure
CREATE TABLE IF NOT EXISTS `review_author` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`email` varchar(255) NOT NULL,
`client_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_37D99F0819EB6921` (`client_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2110 ;
AND
CREATE TABLE IF NOT EXISTS `brokers_comments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`hb_broker_id` int(11) NOT NULL,
`client_id` int(11) DEFAULT NULL,
`user_name` varchar(100) NOT NULL,
`user_email` varchar(100) NOT NULL,
`state` int(11) NOT NULL DEFAULT '0',
`text` varchar(3000) NOT NULL,
PRIMARY KEY (`id`),
KEY `IDX_5365DFFB9FE55EF7` (`hb_broker_id`),
KEY `IDX_5365DFFB19EB6921` (`client_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1583 ;
Before extracting value i did following query:
INSERT INTO review_author (
name,
email,
client_id
)
SELECT
brokers_comments.user_name,
brokers_comments.user_email,
brokers_comments.client_id
FROM brokers_comments
LEFT JOIN review_author
ON brokers_comments.user_name=review_author.name AND
brokers_comments.user_email=review_author.email AND
brokers_comments.client_id=review_author.client_id
WHERE review_author.id IS NULL
Not in review_author should be all author from table brokers_comments and now i'm trying to get authors id using following query:
SELECT
review_author.id
FROM brokers_comments
LEFT JOIN review_author
ON brokers_comments.user_name=review_author.name AND
brokers_comments.user_email=review_author.email AND
brokers_comments.client_id=review_author.client_id
WHERE review_author.id IS NOT NULL
but i'm getting about 110 results from total 1531 records from table brokers_comments.
UPDATE
I couldn't manage to insert data in http://sqlfiddle.com/ so following link are dump for two tables review_author and brokers_comments.
Again my issue is to transfer distinct columns(user_name, user_email, client_id) from table brokers_comments to table review_author and then select review_author.id based on relation name/email/client_id from both tables.
http://wrttn.in/7ca325
http://wrttn.in/3a7885
Insert new author was wrong and made duplication. Below is new correct form.
INSERT INTO review_author (
name,
email,
client_id
)
SELECT user_name, user_email, client_id
FROM brokers_comments AS broker
WHERE NOT EXISTS
(
SELECT 1
FROM review_author AS author
WHERE author.email = broker.user_email
)
GROUP BY broker.user_email
P.S. I somebody will make a working online mysql database please put in comments so i could put it there.
Resolved
Only now i realised that user_email must be unique. Based on this i made following select statement:
SELECT
author.id
FROM brokers_comments AS broker
LEFT JOIN review_author AS author
ON broker.user_email = author.email
It seems you use excess fields in JOIN clause since client_id is a key, you need to join tables only on this field. Possible cause of that you getting not same number of records is different name/email for same client_id in those two tables. So, your two queries should be like this:
INSERT INTO review_author (
name,
email,
client_id
)
SELECT
brokers_comments.user_name,
brokers_comments.user_email,
brokers_comments.client_id
FROM brokers_comments
LEFT JOIN review_author
ON brokers_comments.client_id=review_author.client_id
WHERE review_author.id IS NULL
and
SELECT
review_author.id
FROM brokers_comments
LEFT JOIN review_author
ON brokers_comments.client_id=review_author.client_id
WHERE review_author.id IS NOT NULL

SQL select entries in other table linked by foreign keys

I have redesigned my database structure to use PRIMARY and FOREIGN KEYs to link the entries in my 3 tables together, and I am having problems trying to write queries to select data in one table given data in a another table. Here is an example of my 3 CREATE TABLE statements:
CREATE TABLE IF NOT EXISTS players (
id INT(10) NOT NULL AUTO_INCREMENT,
username VARCHAR(16) NOT NULL,
uuid VARCHAR(200) NOT NULL DEFAULT 0,
joined TIMESTAMP DEFAULT 0,
last_seen TIMESTAMP DEFAULT 0,
PRIMARY KEY (id)
);
/* ^
One |
To
| One
v
*/
CREATE TABLE IF NOT EXISTS accounts (
id INT(10) NOT NULL AUTO_INCREMENT,
account_id INT(10) NOT NULL,
pass_hash VARCHAR(200) NOT NULL,
pass_salt VARCHAR(200) NOT NULL,
created BIGINT DEFAULT 0,
last_log_on BIGINT DEFAULT 0,
PRIMARY KEY (id),
FOREIGN KEY (account_id) REFERENCES players(id) ON DELETE CASCADE
) ENGINE=InnoDB;
/* ^
One |
To
| Many
v
*/
CREATE TABLE IF NOT EXISTS purchases (
id INT(10) NOT NULL AUTO_INCREMENT,
account_id INT(10) NOT NULL,
status VARCHAR(20) NOT NULL,
item INT NOT NULL,
price DOUBLE DEFAULT 0,
description VARCHAR(200) NOT NULL,
buyer_name VARCHAR(200) NOT NULL,
buyer_email VARCHAR(200) NOT NULL,
transaction_id VARCHAR(200) NOT NULL,
payment_type VARCHAR(20) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (account_id) REFERENCES accounts(account_id) ON DELETE CASCADE
) ENGINE=InnoDB;
Say for example, I want to select all the usernames of users who purchased anything greater than $30. All the usernames are stored in the players table, which is linked to the accounts table and that is linked to the purchases table. Is this this the best way to design this relational database? If so, how would I run queries similar to the above example?
I was able to get get all of a users purchase history given their username, but I did it with 2 sub-queries... Getting that data should be easier than that!
Here is the SELECT query I ran to get all of a players purchase data:
SELECT *
FROM purchases
WHERE account_id = (SELECT id FROM accounts WHERE account_id = (SELECT id FROM players WHERE username = 'username'));
Also, when I try to make references to the other tables using something like 'players.username', I get an error saying that the column doesn't exist...
I appreciate any help! Thanks!
Your design is ok in my opinion. The relation between players and account is one-to-many and not one-to-one since this way, you can have two tuples referencing a single player.
I would write the query you need as:
SELECT DISTINCT p.id, p.username
FROM players p INNER JOIN accounts a ON (p.id = a.account_id)
INNER JOIN purchases pc ON (a.id = pc.account_id)
WHERE (pc.price > 30);
As Sam suggested, I added DISTINCT to avoid repeating id and username in case a user have multiple purchases.
Note the id is here to avoid confusion among repeated usernames.