MySQL tables are not aligned right - mysql

I have a question which relates to MySQL. The problem can be seen in these two images:
http://imgur.com/NrOwSxS,yPo9Cra
http://imgur.com/NrOwSxS,yPo9Cra#1
Does anyone know why MySQL is doing this? It should show up as a nice and neat table, not this bundle of gibberish. Thanks in advance! :D

First, to show that there's nothing really wrong, try this query:
SELECT firstname FROM contact_info
That should look good. Now try this:
SELECT firstname, lastname FROM contact_info
That's how you pick individual columns.
Really you want to capture output to a file, this page shows you how: The MySQL Command-Line Tool
Then you can learn to use other programs to format it nicely.

I assume you created your table somewhat like this:
create table automobile (make char(10),model char(10),year int, color char(10), style char(50), MSRP int);
insert into automobile values ('Ford','Mustang',2006,'Blue','Convertible',27000);
insert into automobile values ('Toyota','Prius',2005,'Silver','Hybrid',22000);
insert into automobile values ('Toyota','Camry',2006,'Blue','Sedan',26000);
insert into automobile values ('Dodge','1500',2005,'Green','Pickup',26000);
so a
describe automobile
will show you your columns as:
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| make | char(10) | YES | | NULL | |
| model | char(10) | YES | | NULL | |
| year | int(11) | YES | | NULL | |
| color | char(10) | YES | | NULL | |
| style | char(50) | YES | | NULL | |
| MSRP | int(11) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
as long as your columns in total are smaller than your terminal's width you should see
the expected result:
mysql> select * from automobile;
+--------+---------+------+--------+-------------+-------+
| make | model | year | color | style | MSRP |
+--------+---------+------+--------+-------------+-------+
| Ford | Mustang | 2006 | Blue | Convertible | 27000 |
| Toyota | Prius | 2005 | Silver | Hybrid | 22000 |
| Toyota | Camry | 2006 | Blue | Sedan | 26000 |
| Dodge | 1500 | 2005 | Green | Pickup | 28000 |
+--------+---------+------+--------+-------------+-------+
if you'd like the result smaller then pick the columns you'd like to see e.g.
select make,model from automobile
mysql> select make,model from automobile;
+--------+---------+
| make | model |
+--------+---------+
| Ford | Mustang |
| Toyota | Prius |
| Toyota | Camry |
| Dodge | 1500 |
+--------+---------+
to make the content of a column smaller you may use the left string function
select left(make,4) as make, left(model,5) as model,left(style,5) as style from automobile;
+------+-------+-------+
| make | model | style |
+------+-------+-------+
| Ford | Musta | Conve |
| Toyo | Prius | Hybri |
| Toyo | Camry | Sedan |
| Dodg | 1500 | Picku |
+------+-------+-------+

You can try supplying a line separator character in the end.
mysql> LOAD DATA LOCAL INFILE *file_path* INTO *table_name* LINES TERMINATED BY '\r\n';
Separator character may vary for editors. In Windows, most editors use '\r\n'.

Related

MySQL table displaying strangely

I imported a csv into a table in a MySQL database, but when I do SELECT * FROM hale (where hale is the name of the table), I get something that looks like this:
+----+--------+------+---------+-----------------+-----------------+----------------------------------------+
| id | Source | Page | Country | Word_ID | OriginalWord | OriginalTranslation |
+----+--------+------+---------+-----------------+-----------------+----------------------------------------+
| | word:HH1885:001 | Siksika | Blackfeet
|rd:HH1885:003 | oqkatsh | foot
|d:HH1885:004 | nitokiskam | one
|d:HH1885:005 | natokam | two
|ord:HH1885:006 | newowiskam | three
|rd:HH1885:007 | nijoim | four
|rd:HH1885:008 | nijitji | five
|d:HH1885:009 | nawo | six
|ord:HH1885:010 | ikitchike | seven
|ord:HH1885:011 | nanisho | eight
|rd:HH1885:012 | pikkiso | nine
|d:HH1885:013 | kepo | ten
|word:HH1885:014 | najippo | twenty
|word:HH1885:015 | neppo | thirty
|a | word:HH1885:016 | kepippo | one hundred
|d:HH1885:017 | omakkatose | God
|word:HH1885:018 | spouteh | heaven
|d:HH1885:019 | kristikoy | day
|ord:HH1885:020 | kokoy | night
|d:HH1885:021 | matapi | man
|ord:HH1885:022 | akew | woman
|d:HH1885:023 | saqkomapi | boy
The csv file is formatted like this:
id,Source,Page,Country,Word_ID,OriginalWord,OriginalTranslation
0,HH1885,702,Canada,word:HH1885:001,Siksika,Blackfeet
1,HH1885,702,Canada,word:HH1885:002,siksinam,black
2,HH1885,702,Canada,word:HH1885:003,oqkatsh,foot
3,HH1885,702,Canada,word:HH1885:004,nitokiskam,one
4,HH1885,702,Canada,word:HH1885:005,natokam,two
5,HH1885,702,Canada,word:HH1885:006,newowiskam,three
6,HH1885,702,Canada,word:HH1885:007,nijoim,four
7,HH1885,702,Canada,word:HH1885:008,nijitji,five
8,HH1885,702,Canada,word:HH1885:009,nawo,six
9,HH1885,702,Canada,word:HH1885:010,ikitchike,seven
10,HH1885,702,Canada,word:HH1885:011,nanisho,eight
11,HH1885,702,Canada,word:HH1885:012,pikkiso,nine
12,HH1885,702,Canada,word:HH1885:013,kepo,ten
13,HH1885,702,Canada,word:HH1885:014,najippo,twenty
14,HH1885,702,Canada,word:HH1885:015,neppo,thirty
15,HH1885,702,Canada,word:HH1885:016,kepippo,one hundred
And the commands I ran were:
CREATE TABLE hale (
id int NOT NULL AUTO_INCREMENT,
Source nvarchar(255) NOT NULL,
Page int NOT NULL,
Country nvarchar(255),
Word_ID varchar(255) NOT NULL,
OriginalWord nvarchar(255),
OriginalTranslation nvarchar(255),
PRIMARY KEY (id)
);
LOAD DATA LOCAL INFILE '/home/pearlh6527/sel_cols.csv'
INTO TABLE hale
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(id, Source, Page, Country, Word_ID, OriginalWord, OriginalTranslation);
When I select individual columns, however, I see that the information is present in the table. For example, SELECT Source FROM hale gives something like:
+--------+
| Source |
+--------+
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
Does anyone know why the information might not be displaying correctly when I select everything?

Get difference of unique and duplicate set - SQL

I have two similar data sets (table, view, CTE), one of which contains unique rows (guaranteed by DISTINCT or GROUP BY), the second contains duplicates (no primary key constraint involved).
How can I get the difference of two data sets so that I only get the duplicates of the second set in MySql 8?
Say I have a table called Animals, which stores NAME and SPECIES.
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| ID | int(11) | NO | PRI | NULL | |
| NAME | varchar(255) | YES | | NULL | |
| SPECIES | varchar(255) | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+
ANIMALS
+----+---------+-------------+
| ID | NAME | SPECIES |
+----+---------+-------------+
| 1 | Lion | Carnivorous |
| 2 | Giraffe | Herbivores |
| 3 | Zebra | Herbivores |
| 4 | Trutle | Herbivores |
| 5 | Tiger | Carnivorous |
| 6 | Bear | Carnivorous |
+----+---------+-------------+
With that in place, I define the view DUPLICATED.
CREATE VIEW DUPLICATED AS
SELECT * FROM ANIMALS
UNION ALL
SELECT * FROM ANIMALS WHERE SPECIES = "Carnivorous";
(Duplicates every Carnivorous in the set)
DUPLICATED
+---------+-------------+-----+
| NAME | SPECIES | CNT |
+---------+-------------+-----+
| Lion | Carnivorous | 2 |
| Tiger | Carnivorous | 2 |
| Bear | Carnivorous | 2 |
| Giraffe | Herbivores | 1 |
| Zebra | Herbivores | 1 |
| Trutle | Herbivores | 1 |
+---------+-------------+-----+
Now I want to get the difference of SELECT * FROM ANIMALS and DUPLICATED or vice versa, essential getting all Carnivorous from ANIMALS.
Basically you can group by whatever combination of fields that guarantee the uniqueness of a record in your result. you haven't provided your queries or your table's schema, so i will try to demonstrate this using a general example. you can get my drift and apply it to your query.
SELECT field1, field2, field3 COUNT(*)
FROM MyTable
GROUP BY field1, field2, field3
HAVING COUNT(*) > 1

Combine two SQL tables with replacement?

Long story short, I need to be able to combine two tables (one is a continuation of the previous with a little overlap). I then need to set one column to be a Primary Key, which means no duplicates. When I tried to just use the Import Wizard in SQL Management Studio and import one table into the other, it just added all the data from one table into the next. I need to figure out some way or SQL command that will import all the data from the new table into the older table, and replace any existing duplicate data with the ones from the newer table.
Think of it like this, I have two tables with the following data:
People_Old table
+------+--------+
| Name | Color |
+------+--------+
| Mary | Blue |
| Katy | Yellow |
| Jim | Green |
| John | Red |
+------+--------+
People table
+------+--------+
| Name | Color |
+------+--------+
| Jim | Silver |
| John | Brown |
| Greg | Purple |
| Liz | Pink |
+------+--------+
Assuming the "Name" column are suppose to be a Primary Key, I would like to add the data from the newer "People" table into the older "People_Old" table, but replace the overlapping data so there are no duplicates. In this example, I would like the final "People_Old" table to be:
+------+--------+
| Name | Color |
+------+--------+
| Mary | Blue |
| Katy | Yellow |
| Jim | Silver |
| John | Brown |
| Greg | Purple |
| Liz | Pink |
+------+--------+
Use the REPLACE statement, something like:
replace into people_old select * from people;
DROP TABLE IF EXISTS old;
CREATE TABLE old
(Name VARCHAR(12) NOT NULL PRIMARY KEY
,Color VARCHAR(12) NOT NULL
);
INSERT INTO old VALUES
('Mary','Blue'),
('Katy','Yellow'),
('Jim','Green'),
('John','Red');
DROP TABLE IF EXISTS new;
CREATE TABLE new
(Name VARCHAR(12) NOT NULL PRIMARY KEY
,Color VARCHAR(12) NOT NULL
);
INSERT INTO new VALUES
('Jim' ,'Silver'),
('John','Brown'),
('Greg','Purple'),
('Liz' ,'Pink');
SELECT * FROM old;
+------+--------+
| Name | Color |
+------+--------+
| Mary | Blue |
| Katy | Yellow |
| Jim | Green |
| John | Red |
+------+--------+
SELECT * FROM new;
+------+--------+
| Name | Color |
+------+--------+
| Jim | Silver |
| John | Brown |
| Greg | Purple |
| Liz | Pink |
+------+--------+
INSERT INTO old
SELECT name
, color
FROM new
ON DUPLICATE KEY UPDATE old.color = new.color;
SELECT * FROM old;
+------+--------+
| Name | Color |
+------+--------+
| Mary | Blue |
| Katy | Yellow |
| Jim | Silver |
| John | Brown |
| Greg | Purple |
| Liz | Pink |
+------+--------+
6 rows in set (0.00 sec)

Why is LOAD DATA from text file into table not working properly in mysql?

I have a table event as:
+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| date | date | YES | | NULL | |
| type | varchar(15) | YES | | NULL | |
| remark | varchar(255) | YES | | NULL | |
+--------+--------------+------+-----+---------+-------+
Now I wish to insert some data into this table from a text file event.txt. The text file is as:
Tommy 2000-01-02 litter 4 kittens, 3 females, 1 male
Bowser 2001-04-26 vet needed break straightened
puffball 2002-05-13 birthday gave him a new chew toy
The whitespaces after values are tabs. The last values are sentences with actual whitespaces. When I load this txt file into the table eventit doesn't load the values properly. I run the query:
load data local infile 'D:/Softwear/mysql/install/data/event.txt' into table event;
The table generated is as following:
+----------+------------+----------+-------------------------------+
| name | date | type | remark |
+----------+------------+----------+-------------------------------+
|Tommy | 2000-01-02 | litter | 4 kittens, 3 females, 1 male
|ser | 2001-04-26 | vet | needed break straightened
| puffball | 2002-05-13 | birthday | gave him a new chew toy |
+----------+------------+----------+-------------------------------+
The value bowser is truncated. Why is it so? When I uploaded a similar pet.txt file into table pet then the table generated correctly. I used notepad to write the txt file, used tab after values and newline after rows.

Default values for all users in mysql table

In my app, I allow people to create additional rooms for inventory purposes. Let's say I have a table called "user_data." The d_key field is the data type for a lack of a better description. I have that just in case I introduce more custom fields for the user. Anyways, I want every user to have a room that's called None. This way any item that doesn't have a room assigned to it will default to "None."
What's the best way to introduce None into the table? Users won't be able to delete this default room, but they can edit/delete rooms they input into the DB table.
user_data table
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | | NULL | |
| d_key | varchar(20) | NO | | NULL | |
| d_value | varchar(40) | NO | | NULL | |
+---------+-------------+------+-----+---------+----------------+
Sample data:
+----+---------+----------+--------------+
| id | user_id | d_key | d_value |
+----+---------+----------+--------------+
| 20 | 2 | location | bedroom |
| 21 | 2 | location | living room |
| 22 | 2 | location | attic |
| 23 | 3 | location | kitchen |
+----+---------+----------+--------------+
Add the row as part of your user creation process.
For existing users, loop over the users and ensure they have a "none" room; if they don't, insert one.