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

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.

Related

Maria DB : Alter a field to a PERSISTENT Calculated

I have created a table and I wish to make a Computed Column from the concatenated values of three other fields in the table.
I want this Computed Field to take place at INSERT or UPDATE, so I am specifying PERSISTENT
I have tried the following code (in various ways) in phpMyAdmin but always get errors, which seem to be referencing immediately after ALTER table
I did not see a way of doing this when adding the field in phpMyAdmin, so I hoped I could ALTER it.
Alter TABLE 'tlImages'
CHANGE COLUMN tlImageQuery
AS CONCAT(tlImgTitle,"~",tlImgDescrip,"~",tlImgWhereWhen) PERSISTENT;
MariaDB version 10.0.29-MariaDB-cll-lve - MariaDB Server
phpMyAdmin . Version information: 4.0.10.18
First, lose single quotes around the table name, they are not suitable for this purpose. Use backticks or nothing.
You will still get a syntax error further in the statement, because AS clause should be in brackets. Add them.
You will still get a syntax error because you are missing column type before the AS (...) clause, add it.
You will still get a syntax error because CHANGE COLUMN needs two column names, old and new, use MODIFY instead.
Alter TABLE `tlImages`
MODIFY COLUMN tlImageQuery VARCHAR(128)
AS (CONCAT(tlImgTitle,"~",tlImgDescrip,"~",tlImgWhereWhen)) PERSISTENT
;
(Type VARCHAR(128) is given just as an example).

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

Failed to read auto-increment value from storage engine, Error Number: 1467

When inserting data in mysql i get this error:
Error Number: 1467
Failed to read auto-increment value from storage engine
I don't now how to solve this issue please any help will be appreciated.
After some searching i found the answer and it solved my problem.
run this sql query it will fix the problem
ALTER TABLE `YOUR_TABLE` AUTO_INCREMENT =1
To add a little comment to kiddingmu's answer: it is not just a question of the number of digits, but also of the range of the datatype.
If the column is INT(11), the 11 says that 11 digits are used for display; but this does not release the constraint that INT can only encode the range -2147483648:2147483647 when signed, and 0:4294967295 when unsigned.
So: for an INT(11) column, an AUTO_INCREMENT of 10000000000 will work; an AUTO_INCREMENT of 90000000000 will not, despite it being 11 digits.
If a larger range is needed, then another type should be used, like BIGINT.
I received this message too. My problem was that my table was not sorted by index. Try using the following:
ALTER TABLE `YOUR-TABLE` ORDER BY `index` ;
One possible explanation for this behavior is that the autoincrement value has reached the maximum value for the datatype, and its not possible for the database engine to increment it by one.
I suggest you check the current value. One relatively easy way to do that is to run a SHOW CREATE TABLE mytable;, the table definition will show the current value. (You can also query the information_schema.tables view, to get the same information.)
As spencer7593 says, the autoincrement value has reached the maximum value for the datatype
I just came to the problem.
use the command SHOW CREATE TABLE tablename;, I get
table name {
`id` int(11) NOT NULL AUTO_INCREMENT,
......
}ENGINE=InnoDB AUTO_INCREMENT=100000000000 DEFAULT CHARSET=utf8
You will see the length of 100000000000 is 12, beyond the limit 11.
Check your database structure properly. I have also faced this problem, but I found some error in database structure. After fix the structure, problems were resolved.
Another possibility with this error is that you've hit the max value in the ID field (generally an INT). We had this error when our ID values got close to 4294967295. We ended up having to drop the ID from the table in order to get past the issue. The various INT fields are mentioned in this answer: https://stackoverflow.com/a/5634147/4573630
I had the same problem, after changing ID column to id, and after exporting my db, so I just switched the column back to ID and then back to id again, and every thing worked fine after. Working with elequent orm in laravel, it expects column id and I had column ID, that is why I changed it in the first place
**For me it was the auto increment ID(column) that brings about the Error **
What i just do to solve it is i drop(delete) the ID column and add it again.
ps aux | grep mysql
sudo kill (your pid)
/etc/init.d/mysql restart

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.

Not getting concept of null

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.