#1034-137 while fixing the table when adding new column to table - mysql

I have one table in my database in Cpanel server in which there are 17 columns currently,out of them,one is auto incremented field(which is a primary key).
currently in that table I have 600k rows.
I want to add a new column to table,but I'm getting the following error
1034-137 while fixing the table
Alter command I am using for adding the column
ALter table 'tablename' add 'columnname' Varchar(200) NOT NULL
Please help me what should i do to add the columns to the existing table

Because the new column does not allow NULL values, you will need to specify a default value for the column. All the existing rows in the table will be populated with the default value for the new column when you alter the table.
For example, to set the default value to empty string, you can use this statement.
alter table 'tablename' add 'columnname' Varchar(200) NOT NULL DEFAULT ''
Here is some documentation on using DEFAULT.
You can also drop the DEFAULT constraint after altering the table, so that future inserts into the table will fail if there is an attempt to insert a NULL value into that column,

After researching the error it seems that the indexing on your table may be having issues. Which tables are indexed other than your primary key?
In phpmyadmin select your table, then on the toolbar open Operations.
Under "Table maintenance" on the right hand side click the check table link to find out if there are any errors.
You may need to do some reindexing to correct the error.

Related

MYSQL table column contraint based upon another tables columns

This is my first post and I'm a mysql noob, so I apologize for this question's length.
BACKGROUND
I have a lookup table cctypevals, with a foreign field 'cctypeID', in mysql this would be:
CREATE TABLE `cctypevals` (
`cctypevalsKEY` integer NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`cctypeID` varchar(50) NOT NULL, FOREIGN KEY(cctypeID) REFERENCES cctype(cctypeID) ,
`value` varchar(50) )
cctypeID contains field names from user tables, eg 'taskSTATE', 'serviceTYPE', 'projectCAT' etc.
The value field contains the only allowed values for these user table fields.
Thus cctypevals acts like a 'multi' keyed lookup table, for example:
select value from cctypevals where cctypeID ='serviceTYPE'
might return HomeVisit, BackToBase etc
I know it would be easier to have one lookup table per field but this is what I have.
QUESTION
How do I constrain (in a sql create table or alter statement), tables with fields like task.taskSTATE, service.serviceTYPE etc so they can only accept values from cctypevals.value where cctypeID contains the appropriate field name ?
In create or alter table statement you cannot do that, since the check constraint would be able to such things, but mysql has not implemented the check constraint yet (mysql can parse a check constraint, but it will not work).
You can create before insert and update triggers that check the specific restrictions and raise an sql error message if the updated value does not meet the requirements.

How to alter the table that can accept not null values

Can you please tell How to alter the table so that it can accept the not null constraint
alter table customer add emp_id int not null foreign key references emp(emp_id)
I have tried as above, but it is showing error:
Msg 4901, Level 16, State 1, Line 1
ALTER TABLE only allows columns to be added that can contain nulls, or have a DEFAULT definition specified, or the column being added is an identity or timestamp column,
or alternatively if none of the previous conditions are satisfied the table must be
empty to allow addition of this column. Column 'emp_id1' cannot be added to non-empty table 'customer' because it does not satisfy these conditions.
The error is pretty clear.
You are trying to add a non-null column to a table that has data. When you add the column, it has no value. So, the default value is put into the table for the existing rows. There is no default value, so NULL is used.
You can solve this in a few ways. One would be to empty the table truncate table customer, add the new column, then load the data again.
Another way would be to add the column with NULL allowed. Then fill in the value everywhere and then add the NOT NULL constraint.

Is it possible that a primary key is not autoincrement?

I am trying to insert an element in a table where there are already 3 rows.
Its a table called usuarios=[id (primary, autoincrement), fid, first_name, last_name....]
So there are already 3 rows with id's: 0,1,2
And when I am trying to execute this query (note I am not setting value for id attribute)
INSERT INTO usuarios (fid,email,pass,first_name,last_name,avatar,bday,fecha,id_loc,id_loc_from)
VALUES (-1,'toni#ideadeia.com','72253f579e7dc003da754dad4bd403a6','','','',NOW(),NOW(),'','')
I get this mysql error:
Duplicate entry '0' for key 1
extra: I don't know how this 3 items where inserted (if via interface, by console query, ..)
So question is, how can I make sure that the Primary Keyis autoincrement, and if not; how to set it? (will that solve the problem?)
You should be able to set it as auto-increment using this statement if you have rights to alter the table:
ALTER TABLE usuarios modify id INT(11) NOT NULL AUTO_INCREMENT;
Although I've seen reports of bugs that recommend instead dropping the colulmn and recreating it, if the above doesn't work for some reason. the syntax recommended in those posts is:
ALTER TABLE usuarios
DROP COLUMN id;
ALTER TABLE usuarios
ADD COLUMN idINT(11) NOT NULL AUTO_INCREMENT FIRST;
Warning: Do this in a test database first. You'd want to be careful doing this if the table is using this as a foreign key. It could fail at best, or break all the relationships at worst.
There's tons of info at the MySql reference manual.

MySQL: Creating new unique field in already populated table

I need to create a field in a database table, already populated with data. Of course just adding the field empty is not possible. I can only think of creating a new table with a new structure and copy the existing data to the new table, but I wonder if there is a n easier way.
Hint: it is a composite key, comprised of 3 other fields in the same table.
Edit: the field holds a varchar value
Edit: since some of you ask, it is not possible to create a new UNIQUE field in a populated table just by using ADD UNIQUE. It duplicates the new (empty or not) value throughout all entries. Example:
ALTER TABLE 'tablename' ADD 'fieldname' VARCHAR( 64 ) NOT NULL ,
ADD UNIQUE (
'fieldname'
)
error: Duplicate entry '' for key 'fieldname'
Of course just adding the field empty is not possible.
Why?
I'd do the following:
Create field by ALTER TABLE t ADD COLUMN new_column *type_definition*
Update newly created field like UPDATE t SET new_column=*computation_expression*
Add index by ALTER TABLE t ADD INDEX ... (or ALTER TABLE t ADD PRIMARY KEY ... if you need it to be primary).
Hint: it is a composite key, comprised of 3 other fields in the same table.
This sounds like a red-flag to me. You want to create a new field with a unique constraint comprised of the values of 3 other fields that already exist in the same table?
If all you want to do is to enforce that the combination of those three fields is unique, then you should just add a unique constraint on those 3 existing fields (not a new field with duplicate data).
ALTER TABLE tablename ADD UNIQUE (fieldname1, fieldname2, fieldname3);
You can create a new field with AUTO_INCREMENT option:
ALTER TABLE `your_table` ADD COLUMN `new_id` INT NOT NULL AUTO_INCREMENT
This is a possible workaround:
alter table foo add bar varchar(36) not null default '';
then add a trigger as per default value of GUID in for a column in mysql
I came accross the same problem that I had to add a unique constraint on an already populated table.
The error:
Integrity constraint violation: 1062 Duplicate entry '' for key 'url_UNIQUE'
How did I solve it? I put the id of the row into the unique field to avoid duplicates:
UPDATE content SET new_url = content_id WHERE new_url = '';
I then didn't have any duplicate rows anymore and was able to add the unique constraint.

How to restrict Blank values in Primary Key column

I have a database of sql server 2008 which I am using to display in a gridview. I have written some code to add new rows in asp.net and C# as Code behind.
When ever a row is added through the Programming I have put some checking which will not allow the required values to be Null.
But, here comes my problem when ever any one of the user adds a new row manually by opening the Database, then a blank value is allowing in the Primary key column which is not 'Null value'.
So, here I have to restrict even not to allow blank values in primary key column, how can I solve this problem.
You need a check constraint
ALTER TABLE [TableName]
ADD CONSTRAINT [CK_PrimaryKeyNotEmpty]
CHECK
(
LEN([ColumnName]) > 0
)
I'm having trouble figuring out what you actual question is.
1) If you need to make a column not accept null values:
Add a constraint to the column in the db:
ALTER TABLE [Table] ALTER COLUMN [Column] INTEGER NOT NULL
See: Altering a column: null to not null
2) If you need to restrict your PK column to only certain values add a constraint:
ALTER TABLE Table
ADD CONSTRAINT CK_Table_Column_Range CHECK (
Column >= 0
)