Adding Default Constraints to table in PostgreSQL - postgresql-9.6

I am working with PostgreSQL database. I have created the required tables. Now I have to alter table columns as per constraints. I have to apply default constraint to one of my columns whose default value should be 1.
This is the query I am using,
ALTER TABLE Alerts ADD CONSTRAINT DF_Alerts_bIsActive SET DEFAULT ((1)) FOR bIsActive;
This is the error I am Getting,
ERROR: syntax error at or near "SET"
LINE 30: ... TABLE Alerts ADD CONSTRAINT DF_Alerts_bIsActive SET DEFAUL...
^
SQL state: 42601
Character: 948
Please can anyone suggest me the proper way to achieve this.

There is no such thing as a "default constraint". You simply define default values.
alter table alerts alter column bisactive set default 1;
Unrelated, but:
bisactive sounds like that is some kind of flag. You should define that as a proper boolean column, not an integer.

Related

Can we alter the dbspace of a informix table?

Suppose I have following schema.
create table tb1
(col1 Integer,
col2 varchar(50)
) in dbspace1 extent size 1000 next 500 lock mode row;
and I want to change the dbspace of above table to dbspace2 . After doing my alteration table schema should be look like as follows .
create table tb1
(col1 Integer,
col2 varchar(50)
) in dbspace2 extent size 1000 next 500 lock mode row;
Is it possible to do? If it is possible what is the command?
On the face of it, the ALTER FRAGMENT statement and the INIT clause allows you to write:
ALTER FRAGMENT ON TABLE tb1 INIT IN dbspace2;
The keyword TABLE is required; you could specify an index instead.
I've not actually experimented to prove that it works, but the syntax diagram certainly allows it.

Contao Database update always shows ALTER TABLE with unchanged column

I am using contao 4 but I had this problem already in version 3.
I have created a table with some columns. One of this column is marked as int(10) default NULL. Nothing special about that.
However: When I run my database upate, this column is always shown as changed.
ALTER TABLE `tl_products` CHANGE `tags` `tags` int(10) default NULL;
It doenst matter how often I press upate. This statement never disappears.
I already saw that at different other cases (e.g. when one writes default 0 instead of default '0'). Does anyone know how to fix this one?
The correct definition is
int(10) NULL
which should work in Contao. Your previous definition, int(10) default NULL is shorthand for int(10) NOT NULL default NULL, which makes no sense of course (thus it cannot be detected by Contao).

How to change the sql column data type in iOS?

In my iOS app,I want to change the column data type in database.
ALTER TABLE XXX ALTER COLUMN myColumn INT.
I always get 'near ALTER Syntax error'
How to resolve the problem?
Thanks for your help.
You cannot change the column type. You can create a new table, using the correct data type for the column this time, and then select data from the old table and insert it into the new table. The full procedure is outlined in the ALTER TABLE documentation:
Remember the format of all indexes and triggers associated with table X. This information will be needed in step 7 below. One way to do this is to run a query like the following: SELECT type, sql FROM sqlite_master WHERE tbl_name='X'.
Use CREATE TABLE to construct a new table "new_X" that is in the desired revised format of table X. Make sure that the name "new_X" does not collide with any existing table name, of course.
Transfer content from X into new_X using a statement like: INSERT INTO new_X SELECT ... FROM X.
If foreign key constraints are enabled, disable them using PRAGMA foreign_keys=OFF.
Drop the old table X: DROP TABLE X.
Change the name of new_X to X using: ALTER TABLE new_X RENAME TO X.
Use CREATE INDEX and CREATE TRIGGER to reconstruct indexes and triggers associated with table X. Perhaps use the old format of the triggers and indexes saved from step 1 above as a guide, making changes as appropriate for the alteration.
If foreign key constraints were originally enabled (prior to step 4) then run PRAGMA foreign_key_check to verify that the schema change did not break any foreign key constraints, and run PRAGMA foreign_keys=ON to re-enable foreign key constraints.
If any views refer to table X in a way that is affected by the schema change, then drop those views using DROP VIEW and recreate them with whatever changes are necessary to accommodate the schema change using CREATE VIEW.
Note, SQLite uses type affinity (the column definition doesn't alter what type of data you insert into the table). So if you change the data type, you'll want to change the data, too.
ALTER TABLE table ADD newColumn INTEGER;
UPDATE table SET newColumn = oldColumn;

How to update a certain column with large number of rows

We are using the following query to set a certain field to null in a table having 2 million rows. Is there a faster way to do this using the ActiveRecord API. Right now it takes 2-3 minutes to return from this call.
Foo.update_all(:bar => nil)
Try this, another way to do what you want.
ALTER TABLE foo DROP COLUMN bar;
and then,
ALTER TABLE foo ADD COLUMN bar INT(10) DEFAULT NULL;
Probably not. This will just execute very straightforward SQL:
UPDATE foos SET bar = NULL
No idea how to utilize the ActiveRecord API to make that faster.

Create missing auto increment attribute with rails migration

I'm writing a migration to convert a non-rails app into the right format for rails - one of the tables for some reason does not have auto increment set on the id column. Is there a quick way to turn it on while in a migration, maybe with change_column or something?
You need to execute an SQL statement.
statement = "ALTER TABLE `users` CHANGE `id` `id` SMALLINT( 5 ) UNSIGNED NOT NULL AUTO_INCREMENT"
ActiveRecord::Base.connection.execute(statement)
Note this is just an example. The final SQL statement syntax depends on the database.
If you're on postgesql, a single request won't make it. You'll need to create a new sequence in the database.
create sequence users_id_seq;
Then add the id column to your table
alter table users
add id INT UNIQUE;
Then set the default value for the id
alter table users
alter column id
set default nextval('users_id_seq');
Then populate the id column. This may be quite long if the table has many rows
update users
set id = nextval('users_id_seq');
Hope this helps postgresql users...
The Postgres answer by #jlfenaux misses out on the serial type, which does all of it for you automatically:
ALTER TABLE tbl add tbl_id serial;
More details in this related answer.

Resources