0

I'm using the standard world schema that comes with MySQL Workbench. I initially tried this query to give me all of the cities in the United States; however, I kept getting one row of null entries at the bottom.

select * 
from city 
where CountryCode = 'USA';

I looked up a solution and found MySQL SELECT only not null values. So, next I tried this:

select * 
from city 
where Name is not null and CountryCode = 'USA';

which gave me the exact same thing as the first query. So, I decided to take a different approach. Instead of getting multiple columns, I wanted to see if I could just get the Name column:

select Name 
from city 
where CountryCode = 'USA';

Much to my surprise, this worked fine, and without me having to check for null.

So my question is two-fold:

  • Why did my second approach give me the row of nulls at the bottom (also how to fix it), and
  • Why did the last approach not give me a null at the bottom?

EDIT: Additional Info
Also, not sure if this is important, but despite the fact that the first two queries returned a row with nulls at the bottom, for all three queries, I got the same number of returned rows.

Here's the output from the first two queries: first two queries

Here's the output from the last one:
enter image description here

Community
  • 1
  • 1
Steve P.
  • 14,489
  • 8
  • 42
  • 72
  • 2
    My guess is the row with null's is not really in the result set. Its probably added there by the GUI client of your choice to allow adding new rows. – Vatev Aug 05 '13 at 07:24
  • @Vatev Interesting. **Why wouldn't it appear in the last query, though?** Like I said, not sure if you're familiar with it, but I'm using MySQL Workbench. – Steve P. Aug 05 '13 at 07:24
  • Could you screenshot the results? How have you determined that the last record is "null"? – eggyal Aug 05 '13 at 07:27
  • Sounds like a display issue. Are you sure there aren't any more columns which are NOT null, but not visible on your screen? Maybe you can scroll sideways? Would be useful to know which tools you are using. – Daniel Schneller Aug 05 '13 at 07:29
  • 2
    The last query does not include the primary key, so it might count as a 'read only' result set, for the client. Try a different MySQL client to make sure. – Vatev Aug 05 '13 at 07:29
  • This might be the GUI only. I also getting like that. But that's not included in the result set. If you give complete columns instead of `*` also it will show a empty row there. Nothing error in it. – Coder Aug 05 '13 at 07:33
  • @eggyal I've added screen shots – Steve P. Aug 05 '13 at 07:33
  • @ShijuKBabu I've updated my post with pictures. – Steve P. Aug 05 '13 at 07:34
  • Yea, it's a GUI feature of the editable result set. The actual query doesn't return the null row. – Vatev Aug 05 '13 at 07:34
  • @SteveP. In mySQL you can add new rows to the table by typing there in the null/empty row. But I don't know why it is not possible with few columns. As it is just executing query only – Coder Aug 05 '13 at 07:35
  • @SteveP. I understood the situation. I also get like that. What I was saying is, nothing Big issue with that. – Coder Aug 05 '13 at 07:36

2 Answers2

2

I do not know that much about MySQL, but your two first queries are updatable queries, so you might be allowed by default to modify or add records to it. The last 'null' line is then displayed to allow the addtion of data, but does not correspond to any record in the table.

Your last query is not updatabale, so it will not be displayed with a last 'empty' line.

Philippe Grondier
  • 10,900
  • 3
  • 33
  • 72
  • There is no difference between them. Updatable queries are nothing MySQL defines in any way. – Daniel Schneller Aug 05 '13 at 07:29
  • Well there might not be any difference between them but, depending on the constraints on City table, the same set of queries in SQL SERVER could give such results, where the updatable query displays a 'new' row, while the other one does not display this row. – Philippe Grondier Aug 05 '13 at 07:34
  • Given the asterisk appearing in the margin of the results, I believe this is the correct answer. +1 – eggyal Aug 05 '13 at 07:34
  • @PhilippeGrondier I believe that you're correct, but could you just explain a little more as to why it's not updatable. – Steve P. Aug 05 '13 at 07:37
  • 1
    @SteveP. It would not be 'updatable' because the primary means of identifying a record is not a part of the result set -- the primary key of the 'city' table. – Mr47 Aug 05 '13 at 07:43
  • @Mr47 I think that I understand what a primary key is, but how do I determine what is the primary key simply by looking at the table? In another post it said that it should be called `primary`, but my columns do not contain that. I can infer that the `ID` column is most likely the primary key, but is there a definitive way to tell besides executing queries and eliminating options by brute force? – Steve P. Aug 05 '13 at 07:50
  • You could display the structure of the City table and check there is a key (maybe a yellow one? I do not remember how it looks like in MySQL) in front of the ID field ... – Philippe Grondier Aug 05 '13 at 07:52
  • And, after checking the screenshots, I do confirm my answer is correct ... The 'null' record is just a 'new' record, made available when queries are updatable. – Philippe Grondier Aug 05 '13 at 07:53
0

about your second query.. When you cheking for your field havent any value you should cheking for that field not empty LIKE THIS:

SELECT * FROM `city` WHERE name <> '' AND CountryCode  = 'USA'

in the last query when you select a field Singly then you have Set and Set in mathematics not get the null or empty value LIKE this in a set in mathematics there is no different between

a= {0,1,2} AND b= { , 0 , 1 , 2} so when you select a column Singly you have a set so you haven't empty value or null value because its not defined in set

babak faghihian
  • 1,169
  • 2
  • 13
  • 24
  • Not sure that I understand what you're saying. – Steve P. Aug 05 '13 at 07:42
  • No problem. I'm not going to accept an answer until I know for sure that it's right and it makes sense to me. On that note, I think that Philippe may be correct, since it will allow me to add to the row at the bottom of the first two queries, but won't allow me to add to the column at the bottom of the last query. – Steve P. Aug 05 '13 at 07:43
  • @SteveP. ok my friend ;) hope can help u – babak faghihian Aug 05 '13 at 07:53