I created a table using the following statement.
create table constraint_test(name varchar(20), city varchar(20) not null check (city in ('chennai','vellore')), phone numeric(10));
But when I insert as,
insert into constraint_test values('abcd,'ooty',123456);
it gets stored. How can I restrict it?
How about
city ENUM('chennai', 'vellore')
or maybe
city ENUM('UNKNOWN', 'chennai', 'vellore')
Or you could use a TRIGGER.
Or you could implement the check in your application code. After all, not everything can be done in SQL.
Related
I've created and worked with Triggers in Oracle for years however I'm unable to wrap my head around how to update a field when inserting data into a sqlite database.
All I want to do is create a trigger that automatically inserts the current DateTime into a column in the sqlite database named 'createdDate' for ANY record that is inserted.
What is the best approach to accomplish this?
Below is what I've attempted without success.
CREATE TRIGGER outputLogDB_Created_Trig
BEFORE INSERT
ON outputLog
WHEN NEW.createdDate IS NULL
BEGIN
SELECT CASE WHEN NEW.createdDate IS NULL THEN NEW.createdDate = datetime('now', 'localtime') END;
END;
The above is almost a replica of how I would implement my triggers in Oracle with some modifications of course for sqlite. The logic is basically identical.
What am I missing?
Later Edit - I can get it to work if I instead use AFTER INSERT and not using FOR EACH ROW
CREATE TRIGGER outputLog_Created_Trig
AFTER INSERT
ON outputLog
WHEN New.createdDate IS NULL
BEGIN
UPDATE outputLog
SET createdDate = datetime('now', 'localtime')
WHERE outputLog_ID = New.rowid;
END;
But why can't I just insert the record using the new value while I'm inserting it? Am I ONLY able to get this in there using an Update AFTER I've already inserted the record?
The issue I have with this is the fact that I'd like to have a NOT NULL constraint on the createdDate column. Perhaps I'm simply used to how I've done it for years in Oracle? I realize the Trigger 'should' take care of any record and force this field to NEVER be NULL. It's just that NOT being able to add the constraint for some reason makes me uneasy. Do I need to let go of this worry?
Thanks to Shawn pointing me toward an easy simple solution to my problem. All that is needed in a SQLite database to insert the current Date/Time for each record being inserted is to set the DEFAULT value on that column to CURRENT_TIMESTAMP.
Since I wanted the timestamp in my own local time see below my create table script that is the solution to my problem.
CREATE TABLE outputLog (
outputLog_ID INTEGER PRIMARY KEY ASC ON CONFLICT ROLLBACK AUTOINCREMENT
NOT NULL ON CONFLICT ROLLBACK,
outputLog TEXT,
created DATETIME DEFAULT (datetime(CURRENT_TIMESTAMP, 'localtime') )
NOT NULL )
;
I had to rename an existing table in my sqlite database using the following command:
ALTER TABLE users RENAME TO widgets;
After running that command, when I check the schemas using the .schema command, this is what I see:
CREATE TABLE "widgets"(id integer primary key AUTOINCREMENT, widget_tag varchar(10), destination varchar(100), class varchar(10), name varchar(255), grp active bit(1));
CREATE TABLE uu(id integer primary key AUTOINCREMENT, uu_name varchar(255), email varchar(255), active bit(1));
Notice the quotes around the table name. I'm not sure if that's a bad thing or not. My web application runs just fine and I'm able to update / delete / select records no problem.
Can someone tell me what these quotes are and if I need to worry ?
Thanks.
As far as I am aware, you have nothing to worry about. The quotes simply define the name of the table as a string literal to SQLite. You would only need to worry if the value came back with the double quotes in "widgets" escaped, which you would have seen in your schema check as ""widgets"".
I'm not sure that I'm getting an id primary key in my sqlite table.
When I create my table I use
CREATE TABLE IF NOT EXISTS HABITS (id unique, uid, text, orglvl, habitstatus)
The other properties are fine, and I can retrieve results.rows.item(i).text, but results.rows.item(i).id is NULL
Also upon Insert, when I try to get results.insertId, I get an error of "undefined", which started this whole wondering for me.
What am I doing wrong - how come I have no id's when I have created them in my table?
To get an autoincrementing column, you must use INTEGER PRIMARY KEY.
I am new to Flyway. Flyway is good and friendly.
I want to create and give schema name for my tables in V1__Initial_structure.sql file. I don't know where to assign value to the placeholder. I have configured Flyway programmatically. My sql file contains,
create schema ${schemaName}
create table ${schemaName}.brand(brand_code IDENTITY,
brand_name varchar(50) unique not null, active char(1) default 'Y')
Please help.
This is the method you are looking for: http://flywaydb.org/documentation/javadoc/com/googlecode/flyway/core/Flyway.html#setPlaceholders(java.util.Map)
I have it a point that has really got me stuck and help is need
I have two tables
1) Jobs
2) JobNotes
Jobs is made up like the following
ID integer PRIMARY KEY,
Name nvarchar(100) COLLATE NOCASE
etc..
JobNotes is made up like the following
ID integer PRIMARY KEY,
JobID integer .
Notes nvarchar(100) COLLATE NOCASE
FOREIGN KEY ([JobID ]) REFERENCES Jobs,
When i insert a job into the database I need to insert the jobnote and set the JobID to be the id of the job just inserted previously.
Has anyone got examples or know of a method this can be accomplished using phonegap & sqlite.
Best Regards,
Lmac
After doing the insert I get the max id and then pass this into another function that updates the Foreign key.
Its not the most desired way but so far seems to work.