How to add a column with a default value to an existing table in SQL Server?

Syntax: ALTER TABLE {TABLENAME} ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL} CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE} WITH VALUES Example: ALTER TABLE SomeTable ADD SomeCol Bit NULL –Or NOT NULL. CONSTRAINT D_SomeTable_SomeCol –When Omitted a Default-Constraint Name is autogenerated. DEFAULT (0)–Optional Default-Constraint. WITH VALUES –Add if Column is Nullable and you want the Default Value for Existing Records. Notes: … Read more

Is it possible to run multiple DDL statements inside a transaction (within SQL Server)?

I know most databases have restrictions, but Postgres doesn’t. You can run any number table creations, column changes and index changes in a transaction, and the changes aren’t visible to other users unit COMMIT succeeds. That’s how databases should be! 🙂 As for SQL Server you can run DDL inside of a transaction, but SQL … Read more

Whats the size of an SQL Int(N)?

It depends on the database. MySQL has an extension where INT(N) means an INT with a display width of 4 decimal digits. This information is maintained in the metadata. The INT itself is still 4 bytes, and values 10000 and greater can be stored (and probably displayed, but this depends how the application uses the … Read more

How to store only time; not date and time?

You could try the INTERVAL DAY TO SECOND data type but it won’t save you any disk space … it is very suitable for this purpose though. create table t1 (time_of_day interval day (0) to second(0)); insert into t1 values (TO_DSINTERVAL(‘0 23:59:59’)); select date ‘2009-05-13’+time_of_day from t1; 11 bytes though.

Oracle SQL – max() with NULL values

max(end_dt) keep (dense_rank first order by end_dt desc nulls first) upd: SQL Fiddle Oracle 11g R2 Schema Setup: CREATE TABLE t (val int, s date, e date) ; INSERT ALL INTO t (val, s, e) VALUES (1, sysdate-3, sysdate-2) INTO t (val, s, e) VALUES (1, sysdate-2, sysdate-1) INTO t (val, s, e) VALUES (1, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)