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
)
Related
On of my foreign keys in a table within MySQL added an extra not null column labeled Customers_CustomerID, so I am not able to enter or update the table as required for my assignment. How can I fix this?
Several options. We could 1) drop the column 2) change the column to allow NULL values, 3) change the column definition to specify a non-NULL DEFAULT value for the column, 4) add a BEFORE INSERT trigger to set a non-NULL value to the column, 5) supply a valid value on an INSERT. Lot's of possibilities.
We could provide some additional assistance with the syntax for each of those options, but without the column definition, we can't give the exact syntax that would be required for adding DEFAULT value attribute.
It might not be possible to drop the column, if its used in an index or referenced in a foreign key constraint. The syntax to remove the column would be something like this:
ALTER TABLE mytable DROP customers_customerid ;
The syntax to change the column definition, would be something like this, to specify a DEFAULT value:
ALTER TABLE mytable CHANGE
customers_customerid customers_customerid BIGINT NOT NULL DEFAULT '1' COMMENT 'foo' ;
^^^^^^^^^^^
But we need to know the entire column definition, and repeat that, adding/changing just the bits that need to be changed. The statement above includes guesses about the datatype, et al.
We can get the current definition of the column from the definition of the table with a statement like this:
SHOW CREATE TABLE mytable;
Note: the statement to add a foreign key constraint doesn't add a column to the table. the statement(s) that you executed must have included syntax to add a column.
You haven't really defined what you're trying to fix. What do you mean by saying you are "not able to enter or update the table"? What tool are you trying to update it with?
Here's an example from the MySQL documentation on how you should create two tables and link them with a foreign key:
CREATE TABLE parent (
id INT NOT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE child (
id INT,
parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id)
REFERENCES parent(id)
ON DELETE CASCADE
) ENGINE=INNODB;
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.
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.
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.
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.