|
|
|
|
|
|
|
|
| Question of the Day |
Today's question (by Steve Jones - SSC Editor): | |
Hyperscale Replicas III | |
| In an Azure SQL Database Hyperscale Edition, how many named replicas can be configured? | |
Think you know the answer? Click here, and find out if you are right. | |
| Yesterday's Question of the Day (by Thomas Franz) |
Adding new column with DEFAULT II Which number did the two COUNT(*) return: DROP TABLE IF EXISTS #tmp CREATE TABLE #tmp (id INT NOT NULL) INSERT INTO #tmp (id) SELECT gs.value FROM GENERATE_SERIES(1, 5) AS gs ALTER TABLE #tmp ADD my_value INT NOT NULL CONSTRAINT df_tmp_my_value DEFAULT 1 SELECT COUNT(*) FROM #tmp AS t WHERE my_value = 1 ALTER TABLE #tmp DROP CONSTRAINT df_tmp_my_value ALTER TABLE #tmp ADD CONSTRAINT df_tmp_my_value DEFAULT 2 FOR my_value SELECT COUNT(*) FROM #tmp AS t WHERE my_value = 1 Answer: both 5 Explanation: When you are adding a new column that does not allow NULL values you have to specify a DEFAULT constraint - otherwise you receive the error 4901 "ALTER TABLE only allows columns to be added that can contain nulls, or have a DEFAULT definition specified, or the column being added is an identity or timestamp column, or alternatively if none of the previous conditions are satisfied the table must be empty to allow addition of this column. Column 'my_value' cannot be added to non-empty table '#tmp' because it does not satisfy these conditions." The value of the specified DEFAULT will be saved in the columns meta data (sys.system_internals_partition_columns), so no rewrite of the possible very large table is necessary. It will not be changed, when you are dropping the CONSTRAINT later or even creating another one. You can use the following query to verify check the saved initial value for the column: SELECT
c.name,
i.has_default,
i.default_value
FROM tempdb.sys.partitions p
JOIN tempdb.sys.system_internals_partition_columns i
ON p.partition_id = i.partition_id
JOIN tempdb.sys.columns c
ON c.object_id = p.object_id
AND c.column_id = i.partition_column_id
WHERE p.object_id = OBJECT_ID('tempdb..#tmp')
AND p.index_id IN (0,1);
|
| Database Pros Who Need Your Help |
Here's a few of the new posts today on the forums. To see more, visit the forums. |
| SQL Server 2019 - Development |
| Querying Multiple Tables for the Same Information - I have about 100 Tables that are all suffixed with _Modify. These tables record changes made to these 100 tables. Each of these tables may or may not contain a column called 'User' to record the userid of who made the change. I can get the names of all the tables with the following query: […] |
| Editorials |
| Designing for Teams - Comments posted to this topic are about the item Designing for Teams |
| Absolute and Relative References - Comments posted to this topic are about the item Absolute and Relative References |
| Get Along - Comments posted to this topic are about the item Get Along |
| Finely Tuned Models - Comments posted to this topic are about the item Finely Tuned Models |
| Make It Routine - Comments posted to this topic are about the item Make It Routine |
| Older Versions of SQL (v6.5, v6.0, v4.2) |
| Looking for old SQL Server 1.x - 4.21 disks, manuals, boxes, etc. - Hi everyone, I'm hoping some of the longtime SQL Server professionals here might be able to help me with something. I've been doing a lot of research into the earliest versions of SQL Server for SQL.FM, a SQL Server version and feature reference site I maintain, and as part of this effort I've also started […] |
| Article Discussions by Author |
| Checking the Error Log II - Comments posted to this topic are about the item Checking the Error Log II |
| Parameter Sniffing on SQL Server 2025: A Walkthrough with Real Numbers - Comments posted to this topic are about the item Parameter Sniffing on SQL Server 2025: A Walkthrough with Real Numbers |
| Moving Beyond the Reboot: Finding the Culprit Behind Memory Pressure - Comments posted to this topic are about the item Moving Beyond the Reboot: Finding the Culprit Behind Memory Pressure |
| Reserved Words I - Comments posted to this topic are about the item Reserved Words I |
| RegEx Functions III - Comments posted to this topic are about the item RegEx Functions III |
| Optimized Locking in SQL Server 2025: Fewer Locks, Less Blocking, and the Cases It Cannot Fix - Comments posted to this topic are about the item Optimized Locking in SQL Server 2025: Fewer Locks, Less Blocking, and the Cases It Cannot Fix |
| Finding Gaps in Sequential Data Using SQL Server - Comments posted to this topic are about the item Finding Gaps in Sequential Data Using SQL Server |
| Negative and Positive Numbers - Comments posted to this topic are about the item Negative and Positive Numbers |
| |
| ©2019 Redgate Software Ltd, Newnham House, Cambridge Business Park, Cambridge, CB4 0WZ, United Kingdom. All rights reserved. webmaster@sqlservercentral.com |