in Education by
How can a column with a default value be added to an existing table in SQL Server 2000 / SQL Server 2005? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
Syntax: ALTER TABLE {Table_Name} ADD {Column_Name} {NOT NULL|NULL} CONSTRAINT {Constraint_Name} DEFAULT {Default_Value} WITH Values Have a look at this video on ALTER TABLE STATEMENT in SQL Let’s understand this via an example: ALTER TABLE TableA ADD Column1 Bit NULL --Or you can also use NOT NULL CONSTRAINT D_TableA_Column1 --when the Constraint is deleted a default constraint name will be generated automatically DEFAULT (0) --Default constraint, not mandatory to use. WITH values --If you want to add a default value for existing records and column is nullable then add this. Note: When you use optional With-Values Statement: The WITH VALUES is only needed if you want to add a default value for existing records and column is nullable then add this. It will automatically use the default Value for all existing records, whether you specify WITH VALUES or not if the Column is NOT NULL. You can refer to this video to understand how CONSTRAINTS works. When you use Optional Constraint Name: If you’ll not use CONSTRAINT D_TableA_Column1 then SQL Server will generate a Default-Constraint automatically with a different Name like: DF__TableATa__Column1C__4FB7FEF6 Let’s understand how Inserts work with a Default-Constraint: If you insert a Record into TableA and do not Specify Column1’s value, then it will Default to 0. But, if you insert a Record and Specify SomeCol's value as NULL (and your column allows nulls), then the Default-Constraint will not be used, and NULL will be inserted as the Value.

Related questions

0 votes
    How do I do a SELECT * INTO [temp table] FROM [stored procedure]? Not FROM [Table] and without defining [ ... not supply any columns. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have a SQL column with a length of 6. Now want to take only the first char of that column. Is there any ... in SQL to do this? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I have a SQL column with a length of 6. Now want to take only the first char of that column. Is there any ... in SQL to do this? Select the correct answer from above options...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    I need to remove a highly referenced table in a SQL Server database. How can I get a list of all the ... of the management studio.) Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I didn't see any similar questions asked on this topic, and I had to research this for something I'm working ... had the same question. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I didn't see any similar questions asked on this topic, and I had to research this for something I'm working ... had the same question. Select the correct answer from above options...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    Just wondering if any of you guys use Count(1) over Count(*) and if there is a noticeable difference in ... SQL Server 2005.) Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    SELECT GETDATE() Returns: 2008-09-22 15:24:13.790 I want that date part without the time part: 2008-09-22 00: ... How can I get that? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    Consider a database table holding names, with three rows: Peter Paul Mary Is there an easy way to turn this into ... Peter, Paul, Mary? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    In SQL Server, it's possible to insert into a table using a SELECT statement: INSERT INTO Table (col1, col2, ... .id = other_table.id Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    Could anybody tell me where I can learn Microsoft SQL Server? Select the correct answer from above options...
asked Jan 6, 2022 in Education by JackTerrance
0 votes
    How do I perform an IF...THEN in an SQL SELECT statement? For example: SELECT IF(Obsolete = 'N' OR ... Saleable, * FROM Product Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    Academia has it that table names should be the singular of the entity that they store attributes of. I dislike any ... or should I go? Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    I have written an SQL script which runs fine when executed directly in SQL Management Studio. However, when entering ... are removed: Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I have a user defined function in SQL called getBuisnessDays it takes @startdate and @enddate and returns the number of ... WHERE ... Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
...