1

I created table using command:

CREATE TABLE genre(
    genre_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(20) NOT NULL
);

Then I filled table with data using command:

load data infile 'D:/MySQL/.../genres.txt' into table genre(name);

There is one record on every line.

Problem occured when I tried to print content of table into terminal using command:

select * from genre;

Output:

 +----------+--------------+
 | genre_id | name         |
 +----------+--------------+
 |       1 | alternative
   | 2 | blues
 |   3 | classic
 |   4 | country
   | 5 | dance
 |   6 | electro
    |7 | folk
     | | pop
     | | rap
    |0 | jazz
  | 11 | latino
   |12 | opera
     | | r&b
  | 14 | reagge
    |5 | rock
 |       16 | metal        |
   |
 +----------+--------------+

(vertical bars are printed wrong too)

Does anyone know why is output of first column printed wrong? I am mysql beginner so sorry if it is a dumb question.

File content:

alternative
blues
classic
country
dance
electro
folk
pop
rap
jazz
latino
opera
r&b
reagge
rock
metal
6
  • 1
    Looks like your input file has white-spaces. Can you try running SELECT genre_id, TRIM(name) AS name. Commented Mar 23, 2014 at 23:55
  • SELECT genre_id, TRIM(name) AS name - same output Commented Mar 24, 2014 at 0:00
  • 3
    Please show the contents of your file (first 10 lines) Commented Mar 24, 2014 at 0:16
  • Does the table already have rows in it? Commented Mar 24, 2014 at 0:42
  • Someone edited output code. | characters are on wrong position too. Commented Mar 24, 2014 at 0:54

1 Answer 1

1

This is too long for a comment.

The column genre_id is auto-incremented. So, when a row is inserted into the table, the column gets a value. It is as simple as that.

You appear to have rows with no genre_id. That is not possible. The column is declared as an integer and it cannot take on a NULL value. So, you would see some value for every inserted row.

The conclusion is that the rows that look like they are missing genre_id really are not separate rows at all. Instead, they are single rows that have embedded newline or carriage return characters that are messing up the output. If you are using a Unix-based system, you can use od (octal dump) to see the contents of the file. There is probably some correct line terminator that will fix the problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.