I was wondering how can I fill a column automatically in the database with the GETDATE() ?
Like when I open the database the date of that column should be GETDATE() automatically
Do you suggest using a constraint or a procedure? and what would be the query?
Regards.
ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT GETDATE()
UPDATE
ALTER TABLE table_name ADD CONSTRAINT contraint_name DEFAULT GETDATE() FOR column_name
Adding a constraint should do the trick:
ALTER TABLE table_name
ADD CONSTRAINT constraint_name
DEFAULT GETDATE() FOR column_name
see http://msdn.microsoft.com/en-gb/library/ms190273.aspx
Read this
This alter cmd will fill all the value
in table with the current date time
suppose if you have last year data in
the table for the same data current
date
will be filled, so think and resolve,
read the Default constraints and
then use the cmd
Related
I'm having a little trouble getting my head around this statement. The idea is it's meant to initialize a table with a single row of values for each player in the database, but I can't figure out from a browser full of search tabs what I'm doing wrong. All I know is apparently my syntax is rubbish.
INSERT INTO tblKebabs
(TransactionID, PlayerID, Amount, Description, Timestamp)
SELECT
(COUNT(tblPlayers.PlayerID)) AS TransactionID,
tblPlayers.PlayerID AS PlayerID,0 as Amount,
"Initializer" as Description,"now" AS Timestamp)
FROM tblPlayers
WHERE tblPlayers.PlayerID > 0;
If you want the TransactionID column to be AUTOINCREMENT you have to define it in the CREATE statement.
If you already have defined it as INTEGER PRIMARY KEY it is already AUTOINCREMENT and you don't need to change something.
If you have nothing of the above then you have to recreate the table with INTEGER PRIMARY KEY for this column because SQLite does not allow such changes with ALTER. Now you can omit this column from your statement:
INSERT INTO tblKebabs
(PlayerID, Amount, Description, Timestamp)
SELECT
PlayerID,
0 as Amount,
'Initializer',
CURRENT_TIMESTAMP
FROM tblPlayers
WHERE PlayerID > 0;
You don't need aliases in the SELECT statement.
Also I used CURRENT_TIMESTAMP.
I'm not sure what you're trying to do here but if it's just about the count, try this
ROW_NUMBER() OVER(ORDER BY tblPlayers.PlayerID) AS TransactionID
Ho can i define a integer auto increment with oracle 11g?This is my code with mysql user_id int(6) not null auto_increment primary key how can i have this line with oracle?Because i've already the same database in mysql now i want to build the same structure with oracle
You can achieve this with a sequence.
CREATE SEQUENCE seq_user;
The above will auto increment by 1, and start at 1.
To insert values using this sequence, you can do the following (for example):
INSERT INTO table_name (user_id) VALUES (seq_user.NEXTVAL);
To automate this process, you could reference the sequence in a trigger on the table, that adds this value on an insert automatically:
CREATE OR REPLACE TRIGGER user_trg
BEFORE INSERT ON table_name
FOR EACH ROW
DECLARE
BEGIN
IF(inserting)
THEN
:NEW.USER_ID := seq_user.NEXTVAL;
END IF;
END;
I need to update a table row IF EXISTS, otherwise INSERT a new row.
I tried:
INSERT OR REPLACE INTO table VALUES ...
but if the row row exist this statement changes the row's ROWID, and that's what I'm trying to avoid (I need the rowid :D)
I also tried to find a way to get some sort of return value from the update, in the case where an update has taken place, but I still don't understand how... If I could get the return value from the update statement, I could choose wether to proceed with an insert or not.
Do you have any suggestion or solution to this problem? Or do I need to make a copy of the ROWID and use that instead of the "pure" table ROWID?
Thanks in advance, best regards
ps: I was looking HERE and I was wondering if sqlite has the OUTPUT special word too, but google didn't help me..
---- EDIT after reading comments:
table schema example
CREATE TABLE test (
table_id TEXT NOT NULL,
some_field TEXT NOT NULL,
PRIMARY KEY(table_id)
)
INSERT or REPLACE INTO test (table_id, some_field) VALUES ("foo","bar")
I tested Chris suggestion but the rowid still gets changed. I think the best alternative is to do a SELECT to see if a row with that key already exist. If so, UPDATE, otherwise, INSERT... good old fashion but guaranteed to work.
Combine it with select, like this
INSERT or REPLACE INTO test (ROWID, table_id, some_field)
VALUES ((SELECT ROWID from test WHERE table_id = 'foo' UNION SELECT max(ROWID) + 1 from test limit 1), 'foo','bar')
You need to specify that your table_id is unique in addition to being the primary key:
sqlite> CREATE TABLE test (
table_id TEXT NOT NULL,
some_field TEXT NOT NULL,
PRIMARY KEY(table_id),
UNIQUE(table_id)
);
sqlite> insert or replace into test values("xyz", "other");
sqlite> select * FROM test;
xyz|other
sqlite> insert or replace into test values("abc", "something");
sqlite> insert or replace into test values("xyz", "whatever");
sqlite> select * FROM test;
abc|something
xyz|whatever
From version 3.24.0 (2018-06-04), SQLite now supports an UPSERT clause that will do exactly what the OP needed: https://www.sqlite.org/lang_UPSERT.html
The insert would now look like this:
INSERT INTO test (table_id, some_field) VALUES ("foo","baz")
ON CONFLICT(table_id) DO UPDATE SET some_field=excluded.some_field;
How do I alter column in sqlite?
This is in Postgresql
ALTER TABLE books_book ALTER COLUMN publication_date DROP NOT NULL;
I believe there is no ALTER COLUMN in sqlite at all, only ALTER TABLE is supported.
Any idea? Thanks!
There's no ALTER COLUMN in sqlite.
I believe your only option is to:
Rename the table to a temporary name
Create a new table without the NOT NULL constraint
Copy the content of the old table to the new one
Remove the old table
This other Stackoverflow answer explains the process in details
While it is true that the is no ALTER COLUMN, if you only want to rename the column, drop the NOT NULL constraint, or change the data type, you can use the following set of dangerous commands:
PRAGMA writable_schema = 1;
UPDATE SQLITE_MASTER SET SQL = 'CREATE TABLE BOOKS ( title TEXT NOT NULL, publication_date TEXT)' WHERE NAME = 'BOOKS';
PRAGMA writable_schema = 0;
You will need to either close and reopen your connection or vacuum the database to reload the changes into the schema.
For example:
Y:\> **sqlite3 booktest**
SQLite version 3.7.4
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> **create table BOOKS ( title TEXT NOT NULL, publication_date TEXT NOT
NULL);**
sqlite> **insert into BOOKS VALUES ("NULLTEST",null);**
Error: BOOKS.publication_date may not be NULL
sqlite> **PRAGMA writable_schema = 1;**
sqlite> **UPDATE SQLITE_MASTER SET SQL = 'CREATE TABLE BOOKS ( title TEXT NOT
NULL, publication_date TEXT)' WHERE NAME = 'BOOKS';**
sqlite> **PRAGMA writable_schema = 0;**
sqlite> **.q**
Y:\> **sqlite3 booktest**
SQLite version 3.7.4
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> **insert into BOOKS VALUES ("NULLTEST",null);**
sqlite> **.q**
REFERENCES FOLLOW:
pragma writable_schema
When this pragma is on, the SQLITE_MASTER tables in which database can be changed using ordinary UPDATE, INSERT, and DELETE statements. Warning: misuse of this pragma can easily result in a corrupt database file.
[alter table](From http://www.sqlite.org/lang_altertable.html)
SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE command in SQLite allows the user to rename a table or to add a new column to an existing table. It is not possible to rename a column, remove a column, or add or remove constraints from a table.
SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE command in SQLite allows the user to rename a table or to add a new column to an existing table. It is not possible to rename a column, remove a column, or add or remove constraints from a table. But you can alter table column datatype or other property by the following steps.
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE t1_backup(a,b);
INSERT INTO t1_backup SELECT a,b FROM t1;
DROP TABLE t1;
CREATE TABLE t1(a,b);
INSERT INTO t1 SELECT a,b FROM t1_backup;
DROP TABLE t1_backup;
COMMIT
For more detail you can refer the link.
CREATE TABLE temp_Table(x,y[,etc]);
INSERT INTO temp_Table SELECT * FROM Table;
DROP TABLE Table;
ALTER TABLE temp_Table RENAME TO Table;
Thanks for helping me to find a definitive method!
ALTER COLUMN does not exist in SQLite.
Only Supported alter operations:
Alter Table Name
Alter Table Column Name
Add New Column
Drop Column
Alex Jasmin's answer shows possible way
Reference:
Sqlite Alter Table
Does sqlite support the SELECT INTO statement?
Actually I am trying to save the data in table1 into table2 as a backup of my database before modifying the data.
When I try using the SELECT INTO statement:
SELECT * INTO equipments_backup FROM equipments;
I get a syntax error:
"Last Error Message:near "INTO":syntax
error".
Instead of
SELECT * INTO equipments_backup FROM equipments
try
CREATE TABLE equipments_backup AS SELECT * FROM equipments
sqlite does not support SELECT INTO.
You can probably use this form instead:
INSERT INTO equipments_backup SELECT * FROM equipments;
SQlite didn't have INSERT INTO syntax.
In 2019, I use TEMPORARY keyword to create temporary table and INSERT data to temp table:
CREATE TEMPORARY TABLE equipments_backup(field1 TEXT, field2 REAL)
INSERT INTO equipments_backup SELECT field1, field2 FROM equipments