Design store ip table sql server - sql-server

I am creating database for a website and want to store IP Address of user when they are login,
what data type should I be using for IP? does it need to support IPv6?
Is this script good and does it need to pars IP?
CREATE TABLE [dbo].[temp](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[userId] [bigint] NOT NULL,
[ip] [varchar](40) NOT NULL,
[dateTime] [datetime] NOT NULL,
CONSTRAINT [PK_temp] 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]

Here is the link which describes how to store IP addresses in database.

Related

Bulk data load not loading all records because of constraint, but I don't know why

I have to bulk load data from an older system(Caché I think) to a SQL Server. I was handling the creating of the SQL Server DB and table, and another person was handling getting me the old data into a delimited file that I would load in using SSIS package. Once the data load was done, he had an interface created which would constantly feed any old records into the SQL table. For his interface, he sent me a schema of what his interface was expecting the table to look like, which was this:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[CASENOTE](
[PAS_INT_NO] [int] NOT NULL,
[CASENOTE] [varchar](14) NOT NULL,
[DEFAULT_STORAGE_LOCATION] [varchar](20) NULL,
[CURRENT_STATUS] [varchar](10) NULL,
[DATE_ALLOCATED] [date] NULL,
[DATE_PLACED] [date] NULL,
[DATE_WITHDRAWN] [date] NULL,
[STORAGE_COMMENT] [varchar](30) NULL,
[PAS_VALID] [varchar](3) NULL,
CONSTRAINT [IX_CASENOTE] UNIQUE NONCLUSTERED
(
[CASENOTE] 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
However, I have written an application to search this data, and I had added an extra field to my table, CASENOTE_ID. So I wanted this added to the table too, so I changed his script to add that, which looked like this:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[CASENOTE](
[CASENOTE_ID] [int] IDENTITY(1,1) NOT NULL,
[PAS_INT_NO] [int] NOT NULL,
[CASENOTE] [varchar](14) NULL,
[DEFAULT_STORAGE_LOCATION] [varchar](20) NULL,
[CURRENT_STATUS] [varchar](10) NULL,
[DATE_ALLOCATED] [datetime] NULL,
[DATE_PLACED] [datetime] NULL,
[DATE_WITHDRAWN] [datetime] NULL,
[STORAGE_COMMENT] [varchar](30) NULL,
[PAS_VALID] [varchar](3) NULL,
CONSTRAINT [PK_CASENOTE_1] PRIMARY KEY CLUSTERED
(
[CASENOTE_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_CASENOTE] UNIQUE NONCLUSTERED
(
[CASENOTE] 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
SET ANSI_PADDING OFF
GO
So that's all fine I think, I go to load the data in - the are 1.6 million records, but when the SSIS package tried to load them, it sent around 200,000 to the error file. It always seems to send this same amount to the error file that it couldn't insert into the table. So, I removed the original constraint:
CONSTRAINT [IX_CASENOTE] UNIQUE NONCLUSTERED
(
[CASENOTE] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
And this worked! All of the data inserted perfect.
So, the reason for this question is to ask why. Can anyone help me understand what happened here, why that constraint prevented 200,000 records being loaded, and why removing it allowed them? I'm not a DBA, and everything I have done so far is learning as I go, so I don't understand constraints too well, or their implications, I've just been learning what I need to get the job done. Does anybody know what happened here?
ps. figuring out how to create and run the SSIS package and seeing those numbers go up was really fun and cool.

AspNetUserLogins table and maximum size of index keys in SQL Server

The schema of the identity model in VS2017/aspnetcore defines a table called AspNetUserLogins table to store external logins (CREATE statement below). It defines the primary key as a composite of [LoginProvider] [nvarchar] (450) and [ProviderKey] [nvarchar] (450). The SQL server limits for the maximum size of index keys is specified at 900 bytes here. A note on that page specifically says
"If a table column is a Unicode data type such as nchar or nvarchar,
the column length displayed is the storage length of the column. This
is two times the number of characters specified in the CREATE TABLE
statement. In the previous example, City is defined as an nvarchar(30)
data type; therefore, the storage length of the column is 60."
So is this key not twice the allowed size?
Sql Server Management Studio seems to think so....
Warning! The maximum key length for a clustered index is 900 bytes.
The index 'PK_AspNetUserLogins' has maximum length of 1800 bytes. For
some combination of large values, the insert/update operation will
fail.
CREATE TABLE [dbo].[AspNetUserLogins](
[LoginProvider] [nvarchar](450) NOT NULL,
[ProviderKey] [nvarchar](450) NOT NULL,
[ProviderDisplayName] [nvarchar](max) NULL,
[UserId] [nvarchar](450) NOT NULL,
CONSTRAINT [PK_AspNetUserLogins] PRIMARY KEY CLUSTERED
(
[LoginProvider] ASC,
[ProviderKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Looks like they know...issue1451
It looks as though this will cause subsequent issues. I originally created my database on my desktop prior to deploying it to Azure and there is a significant difference between the 2 databases. In SSMS, using the "Script Table as > CREATE table", the tables designs are:
Azure database:
CREATE TABLE [dbo].[AspNetUserLogins](
[LoginProvider] [nvarchar](225) NOT NULL,
[ProviderKey] [nvarchar](225) NOT NULL,
[ProviderDisplayName] [nvarchar](max) NULL,
[UserId] [nvarchar](450) NOT NULL,
CONSTRAINT [PK_AspNetUserLogins] PRIMARY KEY CLUSTERED
(
[LoginProvider] ASC,
[ProviderKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
Desktop database:
CREATE TABLE [dbo].[AspNetUserLogins](
[LoginProvider] [nvarchar](450) NOT NULL,
[ProviderKey] [nvarchar](450) NOT NULL,
[ProviderDisplayName] [nvarchar](max) NULL,
[UserId] [nvarchar](450) NOT NULL,
CONSTRAINT [PK_AspNetUserLogins] PRIMARY KEY CLUSTERED
(
[LoginProvider] ASC,
[ProviderKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Note the [PRIMARY] references, I cannot get these into Azure. This results in the following error from a: MVC Net core 2 website using Microsoft.AspNetCore.Identity;
MVC Net Core 2.0 error resulting from the inability to add primary clustered keys

Add data from one Azure DB table to another Azure DB table

I Want add data from one Azure DB table to another Azure DB table But same server and same table format. this task i want to run every 5 min. how can i do it any idea about it? i'm founded use elastic database but i want to do different way
CREATE TABLE [dbo].[AuditLog](
[AuditLogId] [int] IDENTITY(1,1) NOT NULL,
[TableName] [nvarchar](max) NULL,
[UserId] [int] NULL,
[EmployeeName] [nvarchar](max) NULL,
CONSTRAINT [PK_Application.AuditLog] PRIMARY KEY CLUSTERED ([AuditLogId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]
TEXTIMAGE_ON [PRIMARY]
GO
INSERT INTO [DBTwo].[dbo].
[AuditLog(TableName,UserId,EmployeeName,Actions)
SELECT TableName,UserId,EmployeeName,Actions
FROM [CemexTenant].[Application].[AuditLog]
DELETE FROM [DBOne].[dbo].[AuditLog]

Insert to microsoft sql server user database failed

Why I can't add more than one user to yii2 application? I'm using microsoft sql server and the signup code for yii2 app has not changed yet. When there is no record in user table, I can add user by using signup page from yii.
this is the DB script
USE [IBR]
GO
/****** Object: Table [dbo].[user] Script Date: 11/05/2016 16.51.02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[user](
[id] [int] IDENTITY(1,1) NOT NULL,
[username] [nvarchar](255) NOT NULL,
[auth_key] [nvarchar](32) NOT NULL,
[password_hash] [nvarchar](255) NOT NULL,
[password_reset_token] [nvarchar](255) NULL,
[email] [nvarchar](255) NOT NULL,
[status] [smallint] NOT NULL DEFAULT ((10)),
[created_at] [int] NOT NULL,
[updated_at] [int] NOT NULL,
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],
UNIQUE NONCLUSTERED
(
[password_reset_token] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED
(
[email] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED
(
[username] 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
this is the screenshot.error while signup for 2nd times
Looks like your issue is with the [password_reset_token] column.
You have a unique constraint on that column but it allows NULL values and you're not inserting data in this statement, that's why it's seeing the NULL as a duplicate key. You can either remove the unique constraint on this field or look at inserting unique data into this field.

Database Design - Preventing duplications for "Room" table

Hey everyone, I'm trying to create a database for a personal friend of mine and given my inexperience with developing databases I'm having difficulty trying to establish one I'm currently dealing with. Essentially, my is issue is with my "rooms" table which has an association with another table called "location"; The location is the is everything you would expect (buildingID, streetAddress,etc.), and Room has a foreign key containing the buildingId. I want my "rooms" table to have unique values for room numbers based on the buildingId.
To give you a clearer idea, I'll just c&p the script I'm using to create those tables.
CREATE TABLE [dbo].[Location](
[buildingId] [int] IDENTITY(1,1) NOT NULL,
[streetAddress] [varchar](50) NOT NULL,
[postalCode] [varchar](7) NOT NULL,
[province] [varchar](30) NOT NULL,
[city] [varchar](30) NOT NULL,
CONSTRAINT [PK_Location] PRIMARY KEY CLUSTERED
(
[buildingId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [UN_postalCode] UNIQUE NONCLUSTERED
(
[postalCode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [UN_streetAddress] UNIQUE NONCLUSTERED
(
[streetAddress] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[Room](
[rmId] [int] IDENTITY(1,1) NOT NULL,
[roomNum] [varchar](10) NOT NULL,
[floor] [int] NOT NULL,
[capacity] [int] NOT NULL,
[permission] [bit] NOT NULL,
[buildingId] [int] NOT NULL,
CONSTRAINT [PK_Room_1] PRIMARY KEY CLUSTERED
(
[rmId] 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
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Room] WITH CHECK ADD CONSTRAINT [FK_Room_Location] FOREIGN KEY([buildingId])
REFERENCES [dbo].[Location] ([buildingId])
GO
ALTER TABLE [dbo].[Room] CHECK CONSTRAINT [FK_Room_Location]
GO
Any help would greatly be appreciated.
Thanks.
A table level unique constraint?
ALTER TABLE dbo.Room WITH CHECK ADD
CONSTRAINT UQ_Room_RoomBuildingLocation UNIQUE (roomNum, buildingId)
This can be a unique index too which would allow INCLUDE columns

Resources