Not getting concept of null - mysql

Hy Guys,
Beginning with mysql. I am not able to grasp the concept of NULL. Check screen-shot (*declare_not_null, link*). In it when I specifically declared 'name' field to be NOT NULL. When i run the 'desc test' table command, the table description shows default value for name field to be NULL.Why is that so?
From what I have read about NULL, it connotes a missing or information that is not applicable. So when I declare a field to be NOT NULL it implies (as per my understanding) that user must enter a value for the name field else the DB engine should generate an error i.e. record will not be entered in DB. However when i run 'insert into test value();' the DB engine enters the record in table. Check screen-shot(*empty_value, link*).
FLICKR LINKS
*declare_not_null*
http://www.flickr.com/photos/55097319#N03/5302758813/
*empty_values*
Check the second screenshot on flickr
Q.2 what would be sql statemetn to drop a primary key from a table's field.
If I use 'ALTER TABLE test drop key id;' it gives the following:
ERROR:
Incorrect table definition; there can be only one auto column and it must be defined as a key.
Thanks for your help..

You are looking at the default value column. The database won't let you update or insert that column with null.

I'll take the first question:
When i run the 'desc test' table command, the table description shows default value for name field to be NULL.Why is that so?
The default being NULL means either:
You have specified that the default is NULL or
You haven't specified a default value for that column.
In this case it is the second option. It does not mean that it is possible to insert a NULL.

Q.1. I don't have access to Flickr so I can't see your screenshots, anyway, if you declare a column NOT NULL, there may be a default value for this column set, and that is why you can add a record.
Q.2. Looks like you cannot have an auto increment column that is not a Primary Key. So if you want to drop the Primary Key, you need to drop the auto increment first.

Related

Is it necessary to rebuild index after changing column default value?

Is is necessary to drop and re-create an index involving a particular column when that column is altered so that the default value is changed? My column looks like SubNum varchar(8) NOT NULL DEFAULT '' and I want to change it to SubNum varchar(8) NOT NULL. My related indexes looks like:
KEY `Docs1` (`First`,`SubNum`,`DocNum`),
KEY `Docs2` (`SubNum`,`DocNum`),
I'm using MySQL version 5.5.49 and the table is MyIsam.
Changing the default value of a column does not change it's data-type. Given that the column was not-null before the change, this change does not affect any existing data - it just changes the default for the next row inserted. Since the currently stored data does not change, there's no need to rebuild the index.
You don't need to reindex it after altering the table, internally it will create a new table and index it too.

How do I add a new column to MySQL via phpMyAdmin?

Coming from years of experience with MS SQL Server I though it would be easy to add a simple nullable integer column to a MySQL database table using phpMyAdmin. I simply found the part of the UI that most resembled the part of SSMS where a new column is added to a table in SQL Server, clicked add column, entered a name, selected int, and null for default value.
The table itself is a posts table created by WordPress. When I click save I get an error saying
ALTER TABLE 'wp_posts' ADD 'acserp' INT NULL DEFAULT NULL AFTER 'comment_count';
MySQL said: Documentation
#1067 - Invalid default value for 'post_date'
I really don't see what adding an integer column has to do the the post_date column unless some row in the posts table has an invalid value for post_date and mySQL does some sort of checks to make sure that noting is wrong with the rest of the table before adding anything.
It looks like the answer is that you have to use raw SQL queries and tweak the SQL_Mode setting.
SET SQL_MODE='ALLOW_INVALID_DATES';
ALTER TABLE wp_posts ADD new_table INT AFTER comment_count
Invalid default value for 'create_date' timestamp field
You forgot to define Length/Value of the integer field. It is the third column from the left. Give value to it and your issue will be resolved. Read this for more information.

What are the Default and extra fields used for in MySQL?

Whenever we create a table in MySQL using the command line client why does it add the default and extra field in the table?
They are not fields in the table. Every column must have a default value and if you don't give one it will be NULL.
The extra shows information like auto_increment etc that is important about the column.
All this is shown because of the command line tool.

Error: Duplicate entry '' for key 'email'

I have seen this error with people running php scripts before but this is happending to me in phpmyadmin ??
Error
SQL query:
UPDATE `cl56-goldeng`.`users` SET `email` = '' WHERE `users`.`id` =118
MySQL said: Documentation
#1062 - Duplicate entry '' for key 'email'
It works fine if I give the field another value, but if I clear the field and press enter I get the above error.
The table itself looks like this :
On your table cl56-goldeng.users, the field email was specified on creation to not allow more than 1 of the same value to be allowed into it. This is done using the UNIQUE identifier on table creation in MySQL. You can see more on the UNIQUE identifier at this link.
You have 2 options that you could go about doing.
First would be to remove the unique constraint on the email field. This entirely depends on your logic in your code, but seeing as emails should almost always be unique, this is not suggested.
You can drop a unique key by running the command:
alter table [table-name] drop index [unique-key-index-name];
Second, would be to use NULL instead of an empty string. My assumption is that you are setting an empty string when the users email does not exist. In this scenario, it would be better to use NULL, and then check for that when retrieving data from the database.
You can insert a NULL value by using the NULL identifier in your MySQL statement, like such:
INSERT INTO users (firstName,lastName,email)
VALUES ('Bob','Ross',NULL);
And then check for a NULL value in whatever language you are accessing this data from.
You have a unique constraint on your email field. Either rethink your logic or drop the unique constraint.
Thats because you may have declare the email as unique key, and once you enter one row of empty email, it wont except another empty email

How to alter MySQL table without losing data?

In my application, I make some changes and upload them to a testing server. Because I have no access to the server database I run ALTER commands to make changes on it.
Using a method I ran the following command on server:
ALTER TABLE `blahblahtable` ADD COLUMN `newcolumn` INT(12) NOT NULL
After that, I found that the all the data of the table has been removed. Now the table is blank.
So I need to alter the table without removing his data. Is there any way to do that?
Your question is quite obvious. You're adding a new column to the table, and setting it to NOT NULL.
To make things clearer, I will explain the reaction of the server when you run the command:
You add a new column, so every row of the table has to set a value for that column.
As you don't declare any default value, all the rows set null for this new column.
The server notices that the rows of the table have a null value on a column that doesn't allow nulls. This is illegal.
To solve the conflict, the invalid rows are deleted.
There are some good fixes for this issue:
Set a default value (recommended) for the column you're creating.
Create the column without the NOT NULL, set the appropiate values, and then make the column NOT NULL.
You can create a temp table, pass all the information from the table you want to alter, and then return the info to the altered table.