SQL Not Empty instead of Not NULL - sql

I am using postgreSQL. I have a column that:
NOT NULL
However when I want to insert a row with an empty string as like:
''
it doesn't give me an error and accepts. How can I check insert value should be not empty? (Neither empty nor null)
PS: My column defined as:
"ads" character varying(60) NOT NULL

Add a constraint to column definition. For example something like:
ads character varying(60) NOT NULL CHECK (ads <> '')
For more, see http://www.postgresql.org/docs/current/static/ddl-constraints.html

Found in the current documentation of postgreSQL you can do the following to achieve what you want:
CREATE TABLE distributors (
did integer PRIMARY KEY DEFAULT nextval('serial'),
name varchar(40) NOT NULL CHECK (name <> '')
);
From the documentation:
CHECK ( expression )
The CHECK clause specifies an expression producing a Boolean result
which new or updated rows must satisfy for an insert or update
operation to succeed. Expressions evaluating to TRUE or UNKNOWN
succeed. Should any row of an insert or update operation produce a
FALSE result an error exception is raised and the insert or update
does not alter the database. A check constraint specified as a column
constraint should reference that column's value only, while an
expression appearing in a table constraint may reference multiple
columns.
Currently, CHECK expressions cannot contain subqueries nor refer to variables other than columns of the current row.

Related

The EntityEntries property will return null because a single entity cannot be identified as the source of the exception

I am using Code first approach. how can i fix this problem?
I am assuming "Modified" column is a date time field in the table which is mandatory column.
You can resolve this issue in two ways
You can pass the "Modified" column field each time you do a insert.
Alter the column to accept null values if that field is not mandatory. Following is
sql query you can run to change the column to accept null values.
ALTER TABLE myTable ALTER COLUMN myColumn {DataType} NULL

How to specify a conditional DEFAULT constraint in SQL Server?

I am adding a column as a foreign key which cannot be NULL and so need to have a DEFAULT value.
ALTER TABLE location
ADD [retailer_brand_id] INT NOT NULL DEFAULT (SELECT retailer_id from retailer),
FOREIGN KEY(retailer_brand_id) REFERENCES retailer_brand(retailer_brand_id);
What I want to achieve is, get the retailer_id from SELECT retailer_id from retailer and if it is equal to 12 then set it to 0, otherwise set to the retailer_id returned by the select query.
When I use the above query, I get an error message
Subqueries are not allowed in this context. Only scalar expressions are allowed.
I recommend a calculated column instead....so you don't also have to have this case statement in application logic as well as the table definition...don't want it in 2 spots...and don't have to worry about when retailerid changes...calc column would take care of that
I needed similar functionality. Calculated column is not an option for me, since my value should be changeble later by user, so I went with trigger on insert.
Described for example here: Trigger to update table column after insert?

Cannot insert the value NULL into column 'id', table 'XXX'; column does not allow nulls. INSERT fails

I am getting an insert error:
Cannot insert the value NULL into column 'id', table 'db.dbo.table';
column does not allow nulls. INSERT fails
.
I have checked the data and the column I am inserting into 'id' does not have any nulls.
Any suggestions?
The error
column does not allow nulls
is happening because you are trying to insert data which has NULL for the column id, for at least one record which you are trying to insert. It has nothing to do with the state of the column before you attempted the insert. Check the source of your insertion data and remove/replace the NULL values, or alter the id column to accept NULL.
When looking at your SQL table design, scroll through "Column Properties" to "Identity Specification". In that drop-down, change "(Is Identity)" to Yes. This setting tells the table to auto-increment the ID field.
Null is not a particular value, but rather, an indication that you don't know the value for this field. It means, "VALUE UNKNOWN". If the field is not NULLable (i.e., if "UNKNOWN" is not permissible), then the INSERT will not be allowed. This is particularly true for an ID column, which might be INDEXed — perhaps even the PRIMARY KEY of the table. Some databases do not allow NULL in INDEXes (particularly primary indexes) at all.

How to insert columns in between in table in sql server 2008

I want to add or update columns using alter table if i am adding a new column i want show error. I am using the code below
alter table Personal_Details alter columns DOB datetime
if i uncheck the NULL to not NULL then it will shows column does not allow nulls; update fails;
i want to insert the fields in between columns not at end.
Plese fix my bug,
Thanks in advance.
The position of the column in the table declaration has nothing to do with its being NULL or NOT NULL.
If you are adding a column (of any type) which you want to be NOT NULL, i.e. you want to prohibit NULL values in that column, and the table already contains some rows, you must also provide some default value. For example:
ALTER TABLE Personal_Details
ADD COLUMN DOB datetime NOT NULL DEFAULT (GETDATE())
Otherwise the engine will attempt to add that column with NULLs as its values, which will violate the NOT NULL property, and the change, therefore, will be reverted.
Basically, the same applies when you want to set an existing column's NOT NULL property on while the column already contains NULLs. But in this case you must explicitly eliminate the NULLs before the change by either replacing them with values or removing the respective rows.
Source:
ALTER TABLE (Transact-SQL). (The particular section related to your problem is just above this code snippet.)
1)For ur adding column with not null problem
Use
ALTER TABLE Personal_Details ADD COLUMN DOB datetime NULL
Update the DOB column with the required dates and make sure there is no null in the column
then alter the column using
ALTER TABLE Personal_Details ALTER COLUMN DOB datetime not NULL
2)For your column going to the end problem...
you should not be worried...the order in which the columns are arranged doesnt matter...unless u are using a pathetic way of accessing data by column order..in which case again..u should stop accessing it by column order...
If the column order really matters you can change it using design option in the sql management table(rightclick on table >design and drag the column to its required place.)

ERROR 1364 (HY000): Field 'MY_DATE' doesn't have a default value

create table MYTABLE
{
MY_DATE int NOT NULL AUTO_INCREMENT,
NAME varchar(255) NOT NULL UNIQUE
};
INSERT INTO MYTABLE(NAME)values(jessica);
Why do I get this error?
ERROR 1364 (HY000): Field 'MY_DATE' doesn't have a default value
From the docs:
There can be only one AUTO_INCREMENT column per table, it must be indexed, and it cannot have a DEFAULT value.
I think you'll find that, because you're not indexing MY_DATE, it may be silently ignoring the AUTO_INCREMENT option (I can't guarantee that, it's just supposition, but the note in the documentation is still relevant).
All the samples I can see on the AUTO_INCREMENT stuff have (the equivalent of):
PRIMARY KEY (MY_DATE)
Alternatively, you may be running in strict SQL mode. All the docs I've seen seem to indicate that, in the absence of a specific default, unlisted columns in an insert will get NULL if they're nullable, or the type default if the not nullable:
If you are not running in strict SQL mode, any column not explicitly given a value is set to its default (explicit or implicit) value. For example, if you specify a column list that does not name all the columns in the table, unnamed columns are set to their default values. Default value assignment is described in Section 10.1.4, “Data Type Default Values”.
For an integral type, that default is 0 (which should kick in the auto increment anyway).
However, in strict SQL mode (from here):
an error occurs for transactional tables and the statement is rolled back.
Which version of MySQL are you using?
I'm using 5.1.41 community and the create table SQL gives
SQL Error (1075): Incorrect table definition; there can be only one auto column and it must be defined as a key
Next correct it to the below
create table MYTABLE
(
MY_DATE int NOT NULL AUTO_INCREMENT primary key,
NAME varchar(255) NOT NULL UNIQUE
);
The insert statement
INSERT INTO MYTABLE(NAME)values(jessica);
Results in
SQL Error (1054): Unknown column 'jessica' in 'field list'
Because it has not been quoted. Once that is corrected to the below:
INSERT INTO MYTABLE(NAME)values('jessica');
It works. You don't have to supply any values for the auto_increment primary key, but you can.
Now, let's talk about MySQL quirks. You can optionally include the column, and set a value of NULL. You can also give it a SPECIFIC value and MySQL will happily use it, even if it is defined as auto_increment.
INSERT INTO MYTABLE(my_date,NAME)values(41,'jessica2');
INSERT INTO MYTABLE(my_date,NAME)values(null,'jessica3');
INSERT INTO MYTABLE(NAME)values('jessica4');
DELETE FROM MYTABLE where my_date=43;
INSERT INTO MYTABLE(my_date,NAME)values(3,'jessica5');
INSERT INTO MYTABLE(NAME)values('jessica?');
select * from mytable;
When you set the number specifically to 41, the next number becomes 42, then 43. It allows us to specifically use 3, even then though 43 was deleted before we used the specific number 3, the next value is still 44.
Output
MY_DATE NAME
41 jessica2
42 jessica3
3 jessica5
44 jessica?
You don't supply a value for MY_DATE in your INSERT statement, and there is no default value defined. (And as paxdiablo points out, there can't be a default value defined in this situation.) Unintuitively, you do need to give a value for MY_DATE, and if that value is zero or null then MySQL will assign the next autoincrement value. Like this:
insert into mytable values(null,'jessica');
Edit this file /etc/mysql/mysql.conf.d/mysqld.cnf
add the below statement under [mysqld]
sql_mode =
This is working perfectly for me.