SQL Server - How make allow NULL value in column - sql

I'm newbie level in SQL Server
I want to allow null value in column, but it can't save.
So I will re-create a new table with same structure.
Please your help to allow null value in column with SQL script.
Thank you..

It looks as though the designer is trying to re-create the whole table and warning you about that.
It is possible to allow it to do so by disabling the option specified in the error message to "prevent saving changes that require the table to be recreated" but there is no need for this here.
You can just run
ALTER TABLE SAMPLE_UPL
ALTER COLUMN KORESKI VARCHAR(300) NULL
Or simply
ALTER TABLE SAMPLE_UPL
ALTER COLUMN KORESKI VARCHAR(300)
(as altering an existing column always sets it to allow NULL unless explicitly specified otherwise)

go in table design and checked check box this can allow you null values
script of this table
CREATE TABLE [dbo].[AWBuildVersion](
[SystemInformationID] [tinyint] IDENTITY(1,1) NOT NULL,
[Database Version] [nvarchar](25) NULL,
[VersionDate] [datetime] NOT NULL,
[ModifiedDate] [datetime] NOT NULL,
CONSTRAINT [PK_AWBuildVersion_SystemInformationID] PRIMARY KEY CLUSTERED
(
[SystemInformationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

Related

Adding a SQL Default value of 01 to a database column

How do you add the following default value to a sql database table?
01
I have tried various datatypes but the 0 keeps disappearing. I haven't even been able to find an example on the exchange or google but I assume this is very straight forward and I am being a numpty!!!
I have tried numeric/integer/varchar without luck. Thanks.
create table MyTable (Id int, Value varchar(100) not null default('01'))
Insert into MyTable (Id) values (1)
Select Value
From MyTable
will be '01'
Sql fiddle
Another option could be:
Open your Table Designer and you will see something like this:
If you prefer code:
USE ["name of your dataBase"]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table_1](
[id] [int] IDENTITY(1,1) NOT NULL,
[sample02] [nvarchar](50) NOT NULL,
[sample03] [nvarchar](50) NULL,
CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ON [PRIMARY]
GO
ALTER TABLE [dbo].[Table_1] ADD CONSTRAINT [DF_Table_1_sample02] DEFAULT ('01') FOR [sample02]
GO
And finally, you can visit this Microsoft post: Specify Default Values for Columns

How to create two or more Unique Columns in SQL Azure?

I want to select two columns in my table and make them unique but I don't know how to do it in SQL Azure database. As you can see in the image below, it doesn't show any option to modify the table properties, so everything is done using sql queries:
Here is the generated script of the table:
USE [mydbase]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[clientaccess](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[ModuleName] [nvarchar](50) NOT NULL,
[ClientAuthenticationId] [bigint] NOT NULL,
[HasAccess] [bit] NOT NULL,
CONSTRAINT [PK_clientaccess_ID] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
GO
ALTER TABLE [dbo].[clientaccess] WITH CHECK ADD CONSTRAINT [CAI_caID] FOREIGN KEY([ClientAuthenticationId])
REFERENCES [dbo].[clientauthentication] ([ID])
GO
ALTER TABLE [dbo].[clientaccess] CHECK CONSTRAINT [CAI_caID]
GO
This is the preview where I encountered the problem, it contains duplicate records:
Hope someone understand my explanation.
Sometimes GUIS have limitations (or not but you haven't discovered yet how all functionalities work). You can always add a unique constraint with ALTER TABLE:
ALTER TABLE [dbo].[clientaccess]
ADD CONSTRAINT Module_Client_UQ --- choose a name
UNIQUE (ModuleName, ClientAuthenticationId) ;

sql- Alter table to add a column which default value is based on other value

my sql table:
CREATE TABLE [dbo].[PayrollParameter]
(
[PayrollParameterID] [CHAR](36) NOT NULL,
[Description] [NVARCHAR](50) NOT NULL,
[NumberOfDaysInMonth] [INT] NOT NULL,
[IsFixedDaysInMonth] [BIT] NOT NULL,
CONSTRAINT [PK_PayrollParameter] PRIMARY KEY CLUSTERED (
[PayrollParameterID] ASC )WITH (pad_index = OFF, statistics_norecompute =
OFF, ignore_dup_key = OFF, allow_row_locks = on, allow_page_locks = on) ON
[PRIMARY]
)
ON [PRIMARY]
GO
the database already contain records; I want to alter the table to set the default value of IsFixedDaysInMonth to Checked if NumberOfDaysInMonth!=0
How can i do this?
CREATE DEFAULT needs a constant value as the default
Is an expression that contains only constant values (it cannot include
the names of any columns or other database objects).
The only ways I know to do what you want would be to use a trigger or insert using a stored procedure.

SQL Server equivalent to MySQL enum data type?

Does SQL Server 2008 have a a data-type like MySQL's enum?
It doesn't. There's a vague equivalent:
mycol VARCHAR(10) NOT NULL CHECK (mycol IN('Useful', 'Useless', 'Unknown'))
The best solution I've found in this is to create a lookup table with the possible values as a primary key, and create a foreign key to the lookup table.
IMHO Lookup tables is the way to go, with referential integrity.
But only if you avoid "Evil Magic Numbers" by following an example such as this one:
Generate enum from a database lookup table using T4
Have Fun!
CREATE FUNCTION ActionState_Preassigned()
RETURNS tinyint
AS
BEGIN
RETURN 0
END
GO
CREATE FUNCTION ActionState_Unassigned()
RETURNS tinyint
AS
BEGIN
RETURN 1
END
-- etc...
Where performance matters, still use the hard values.
Probably the best solution for this is a simple look-up table (What is a lookup table?). Nevertheless you can implement something like this:
Solution
SQL Server Management Studio (SSMS)
User.Role is the Foreign Key here, and Role.Type is the Primary Key it refers. And in that table, you'll have the following values:
The type of which must match in both tables. In this case that type is: nvarchar(15)
If you try to add a value at User.Role different than those available at the Role table, you'll get an error.
SQL Code
CREATE TABLE [dbo].[User](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Username] [nvarchar](50) NOT NULL,
[Email] [nvarchar](75) NOT NULL,
[Password] [nvarchar](25) NOT NULL,
[Role] [nvarchar](15) NOT NULL,
CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[User] WITH CHECK ADD CONSTRAINT [FK_User_Role] FOREIGN KEY([Role])
REFERENCES [dbo].[Role] ([Type])
GO
ALTER TABLE [dbo].[User] CHECK CONSTRAINT [FK_User_Role]
GO
CREATE TABLE [dbo].[Role](
[Type] [nvarchar](15) NOT NULL,
CONSTRAINT [PK_Role] PRIMARY KEY CLUSTERED
(
[Type] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
Solution 2
You can implement a lookup table like this one: Create enum in SQL Server
You can try something like
ALTER TABLE dbo.yourTable
ADD CONSTRAINT yourColumn CHECK(yourColumn IN('XL','L','M','S','XS'))

SQL insert into related tables

This seems to me to be the kind of issue that would crop up all the time with SQL/database development, but then I'm new to all this, so forgive my ignorance.
I have 2 tables:
CREATE TABLE [dbo].[Tracks](
[TrackStringId] [bigint] NOT NULL,
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[Time] [datetime] NOT NULL,
CONSTRAINT [PK_Tracks] PRIMARY KEY CLUSTERED
(
[Id] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Tracks] CHECK CONSTRAINT [FK_Tracks_AudioStreams]
GO
ALTER TABLE [dbo].[Tracks] WITH CHECK ADD CONSTRAINT
[FK_Tracks_TrackStrings] FOREIGN KEY([TrackStringId])
REFERENCES [dbo].[TrackStrings] ([Id])
GO
ALTER TABLE [dbo].[Tracks] CHECK CONSTRAINT [FK_Tracks_TrackStrings]
GO
and
CREATE TABLE [dbo].[TrackStrings](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[String] [nvarchar](512) NOT NULL,
CONSTRAINT [PK_Strings] PRIMARY KEY CLUSTERED
(
[Id] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
I want to insert a new entry into the tracks table. This will also involve inserting a new entry in the trackstrings table, and ensuring that the foreign key column trackstringid in tracks points to the new entry in trackstrings. What is the most efficient means of achieving this?
First, insert into TrackStrings, omitting the primary key column from the column list. This invokes its IDENTITY column which generates a value automatically.
INSERT INTO [dbo].[TrackStrings] ([String])
VALUES ('some string');
Second, insert into Tracks and specify as its TrackStringId the function SCOPE_IDENTITY(), which returns the most recent value generated by an IDENTITY column in your current scope.
INSERT INTO [dbo].[Tracks] ([TrackStringId], [Time])
VALUES (SCOPE_IDENTITY(), CURRENT_TIMESTAMP());
If you are using SQL Server 2005 or later and are inserting a lot of records in a single INSERT, you can look into OUTPUT or OUTPUT INTO options here to use the identities from the first insert in the second without haveing to "re-find" the rows to get all the IDENTITY values.
First insert into the primary table.
INSERT INTO trackstrings VALUES('myvalue')
Next get the identity. This method depends on whether you're are doing it all in 1 statement or a stored procedure or some other method. I will assume 1 statement so I'll just insert with the identity special variable.
INSERT INTO tracks VALUES( ##IDENTITY, getdate() )
Something like that should do it depending on your exact scenario. The key is the ##IDENTITY variable. It holds the last inserted identity value for the connection you are using. It is not table specific, it is simply the most recent identity inserted during the connections lifespan.