I am using below code to add constraint on CreatID column. But its not working. I can still add text like ' xyz' which I don't want to. Also when only spaces are there in text it should be considered as 'null' value. Like if I enter '<space><space><space>' this then it should be considered as null.
ALTER TABLE [dbo].[OneC_Gadget_AppDetails] WITH CHECK
ADD CONSTRAINT [chk_CreateID_val]
CHECK ((len(ltrim(rtrim([AppActionContent])))>(1)))
Related
So I've found that I can use SELECT LOCATE(1,"TEST1"); and it returns 5 correctly.
What I want to do though is add this as a constraint to my table, so TEST1 will be changing and the goal is to check that the 5th position is always a number. This works, but I was wondering whether there is a much easier method to add in 0 until 9 without having to write this line 10 times.
Right now my constraint looks like this :
ALTER TABLE Accommodatie
ADD CONSTRAINT acode_checker
CHECK (LENGTH(accommodatie_code) = 5);
So now I have to add the check for the 5th position, any tips?
Check out the MySQL REGEXP documentation
Something like the below should work for you. REGEXP returns 1 here if accomodatie_code matches the given pattern which is any 4 characters (each . is a single character), any digit in the 5th character space ([0-9]), the ^ tells it to start matching from the beginning of the string.
ALTER TABLE Accommodatie
ADD CONSTRAINT acode_checker
CHECK (LENGTH(accommodatie_code) = 5
AND accommodatie_code REGEXP '^....[0-9]' = 1)
I have an Access table with a TEXT primary key. If I use an INSERT statement to add a row where the primary key value is
'PART'
and then try to INSERT another row where the primary key is
'PART '
(note the trailing space) then I get
Microsoft Access ... didn't add 1 record(s) to the table due to key violations
If there is no primary key on the field all is fine, and the second row is indeed added with the trailing space in the field value. But if primary key is set on the field then it causes problems with the key violation error.
How can I avoid this issue?
Funky behavior. It is normal that Access removes trailing spaces when entering data into a textbox or datasheet field. But that 'PART' and 'PART ' cannot coexist in a primary key column is weird.
#Gord Thompson: You can first insert 'PART ', that works and the trailing space is saved. But then you cannot insert 'PART'.
If this is the only occurrence of spaces in your data, you could replace the spaces by Chr(160), which also looks like a space, and doesn't violate the PK.
CurrentDb.Execute "INSERT INTO tblTextPK (TextPK) VALUES('part')", dbFailOnError
CurrentDb.Execute "INSERT INTO tblTextPK (TextPK) VALUES('part" & Chr(160) & "')", dbFailOnError
In your import code you would use Replace([Fieldname], " ", Chr(160)).
(Note that to type Chr(160), you use Alt+255 )
You could add an additional field, DatebaseID, and fill this during import with A, B, C, etc.
Then create a compound primary key of this field and your current ID.
That would also allow you to get rid of the trailing space(s) which, in general, is a very bad idea.
Very simple :
suppose that your field is : BEN SALEM
so you have to write your commande as below (example Update query) :
"update datatablename set [BEN"+" "+"SALEM]='"+textbox1.text+"' where [ID]='"+textbox2.text+"'";
This is my 5th day learning SQL on mySQL.
I don't understand why the system tells me the following is an error.
My code is:
CREATE TABLE elements(
Name VARCHAR(20),
Symbol VARCHAR(2) PRIMARY KEY,
AtomicNumber TINYINT,
AtomicMass DEC(4,2) -- ending doesn't need , similar to SAS
);
INSERT INTO elements
VALUES ('Actinium','Ac',89,227);
-- here we want to modify the field type of the existing table because Aluminum has decimal #'s in mass
ALTER TABLE elements
MODIFY AtomicMass DECIMAL(9,3);
-- Q: I do not understand this why this is an syntax error.
System Message: "Syntax error: unexpected 'DECIMAL(decimal)'"
It runs and changes the field constraint but the error message is still there.
Additional Question(utterly noob question,please tolerate me): How do you put 'space' in a name of a column? Like now I'm using "AtomicNumber" but I really want "Atomic Number."
Thanks! :)
Consult https://dev.mysql.com/doc/refman/5.6/en/alter-table.html.
You are missing COLUMN.
ALTER TABLE elements
MODIFY COLUMN `AtomicMass` DECIMAL(9,3);
The name of the MySQL data type is DECIMAL, not DEC. So your original table definition statement should look like this:
CREATE TABLE elements(
Name VARCHAR(20),
Symbol VARCHAR(2) PRIMARY KEY,
AtomicNumber TINYINT,
AtomicMass DECIMAL(4,2) -- ending doesn't need , similar to SAS
);
Your modification should look like this:
ALTER TABLE elements
CHANGE COLUMN AtomicMass AtomicMass DECIMAL(10,2)
But, you should consider using FLOAT or DOUBLE rather than DECIMAL for values like atomic weight. Two decimal places of precision isn't very representative of the actual scientific truth.
Finally if you want to wrap your column names in backticks like this
`Atomic Mass`
everywhere you use them, you can put spaces in column names. But this will drive you crazy when you're writing code, I suspect.
So I have a form that can get data from a database by its ID (auto-incremented column(Primary Key)) and display all fields in the <input> tags via value properties. And when I submit the form I want it to either INSERT a new row if the ID from the ID column doesn't already exist and if it does I want to UPDATE the rest of the data in the row with the same ID.
I have been trying to research this, but no one seems to be doing the same thing I am trying to do, its always slightly different. I found a REPLACE INTO and created it like below:
$sqlString = 'REPLACE INTO coursework
SET cwID=`'. $cwID .'`,
cwTitle=`'. $cwTitle .'`,
cwContent=`'. $cwContent .'`,
cwProgress=`'. $cwProgress .'`,
cwDue=`'. $cwDue .'`;
All the $cw[] variables being content received from $_POST method.
I keep getting a Error code: 1054-Unknown column '6' in 'field list' -
the "Unknown column '6'" is mysql trying to call $cwID (value of $_POST['cwID']) instead of the cwID column(which is the Primary Key for my table). I feel like there is something simple and stupid I am missing but I have never used this REPLACE INTO method before.
I saw a post about INSERT IGNORE INTO and INSERT ... ON DUPLICATE KEY UPDATE but both of those sound more destructive than what I am looking for.
I just want to make sure that the table is updated if the cwID exists and the auto-increment is kept in tact, or a new row is added if there is no ID. Should I just run a SELECT query to see if it exists and INSERT/ UPDATE appropriately?
Remove Replace the back-ticks (`) surrounding the strings with single quotes (').
MySql is trying to find a column named by the strings you are using as values.
See http://dev.mysql.com/doc/refman/5.5/en/identifiers.html
Replace the backticks with single quotes:
$sqlString =
"REPLACE INTO coursework SET cwID='$cwID', cwTitle='$cwTitle', cwContent='$cwContent', cwProgress='$cwProgress', cwDue='$cwDue'";
Also, note that " will interpolate your variables.
I want a column to have only two values. For example I want to make the column active can only contain the values "Y" and "N" I don`t want to use boolean data type.
I`m looking for a way similar to the Look Up Wizard of the MS Access how can this be done?
Use a non-nullable bit
What if you want J and N for German? Or other languages? This is client formatting
Ditto "true", "false"
What about y/Y/n/N? Unicode Ys and Ns?
You'd need a check constraint to restrict to Y or N: why when you have this anyway with bit?
Finally, SQL Server has no boolean type as such: client code will interpret bit as boolean though
Edit, after comment on question.
If you need to add more values, then I suggest a lookup table and foreign key. This means you can support new values without changing code (CHECK constraint) and/or datatypes.
What you're looking for are Check Constraints
e.g.
ALTER TABLE dbo.Vendors ADD CONSTRAINT CK_Vendor_CreditRating
CHECK (CreditRating >= 1 AND CreditRating <= 5)
Or for you
ALTER TABLE dbo.MyTableName ADD CONSTRAINT CK_MtTable_FieldName_YN
CHECK (FieldName = 'Y' OR FieldName = 'N')
You could use a varchar(1) or nvarchar(1). Put a constraint on the column in which you state that only Y and N are possible as input to keep data integrity.
Grz, Kris.