sql server update table - sql-server

the table:
CREATE TABLE [dbo].[User] (
[UserName] NVARCHAR (100) NOT NULL,
[Pasword] NVARCHAR (100) NOT NULL,
[Name] TEXT NOT NULL,
[LastName] TEXT NOT NULL,
[Location] TEXT NOT NULL,
[profesion] TEXT NOT NULL,
[Email] NVARCHAR (50) NOT NULL,
[Gender] TEXT NOT NULL,
PRIMARY KEY CLUSTERED ([UserName] ASC)
);
i want to update to:
CREATE TABLE [dbo].[User] (
[UserName] NVARCHAR (100) NOT NULL,
[Pasword] NVARCHAR (100) NOT NULL,
[Name] TEXT NOT NULL,
[LastName] TEXT NOT NULL,
[Location] TEXT NOT NULL,
[profesion] TEXT NOT NULL,
[Email] NVARCHAR (50) NOT NULL,
[Gender] TEXT NOT NULL,
[moneyinmillions] INT NOT NULL,
PRIMARY KEY CLUSTERED ([UserName] ASC)
);
the problem:
an error occurred while the batch was being executed
thanks for the help

In the interest of answering your question, here is the code you would want to add the moneyinmillions column to the User table:
ALTER TABLE [User]
ADD [moneyinmillions] INT NOT NULL;

Ways to Insert a column in your existing Table
Use the ALTER TABLE Statement
Do the following:
ALTER TABLE [dbo].[User]
ADD [moneyinmillions] INT NOT NULL
Using the Table Designer
In Object Explorer, right-click the table (here, User table) to which you want to add columns and choose Design.
Click in the first blank cell in the moneyinmillions column.
Press the TAB key to go to the Data Type cell and select a Data Type from the dropdown.
When you are finished adding columns, from the File menu, choose Save table name (User).
Using DROP TABLE and Re-Creating the Table
DROP TABLE [dbo].[User]
and then Execute the statements below:
CREATE TABLE [dbo].[User] (
[UserName] NVARCHAR (100) NOT NULL,
[Pasword] NVARCHAR (100) NOT NULL,
[Name] TEXT NOT NULL,
[LastName] TEXT NOT NULL,
[Location] TEXT NOT NULL,
[profesion] TEXT NOT NULL,
[Email] NVARCHAR (50) NOT NULL,
[Gender] TEXT NOT NULL,
[moneyinmillions] INT NOT NULL,
PRIMARY KEY CLUSTERED ([UserName] ASC));
(Note: The DROP Table Statement will remove the table definition and all the data, indexes, triggers, constraints, and permission specifications for that table. So, if you have data entry in some fields/columns, then do not use the DROP TABLE Statement because you'll loose all the data).

Did you know that you can right click on the table and open the design view to add/remove columns to or from a table ??

Related

Index for identity in SQL Server automatic or not

I have a huge table for logging. The definition is:
CREATE TABLE [dbo].[TRACELOG]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[TYPE] [varchar](15) NOT NULL,
[DATEHEURE] [datetime] NOT NULL,
[PROGRAMME] [varchar](25) NOT NULL,
[APPLICATION] [varchar](50) NOT NULL,
[DESCRIPTION] [text] NULL,
[UTILISATEUR] [varchar](10) NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Indexes are like this:
The table has now about 18 millions or row. When I run a query using ID = 123456, the query is very long.
SELECT *
FROM TRACELOG
WHERE ID = 123456
I'm very surprised... My question is: in a table with IDENTITY, is there an implicit index created on the column in question (not visible in indexes?) or have I to create manually?
NO - having an IDENTITY column does not automatically create an index.
What does create an automatic (and by default clustered) index is the PRIMARY KEY constraint - which is often used on IDENTITY columns.
But not every IDENTITY column has to be the primary key of its table - you have to specify that if you want it that way.

SQL create table using code and populate with code

I'm working on an assignment and have created my code on NotePad++ to then copy over to SSMS, however when I create a table it brings me to the default view which is manually entering the column names and data. I want to just paste my created code into the database to create the table. How do I go about this?
Also my prof has provided data to populate my tables but I can't seem to figure out where I paste his INSERT INTO data into?
Also, for some reason I attempted to create a new query in the database i created for the assignment and pasted the code into that, but the tables don't show up even if I execute them in the Tables drop down.
Code for notepad++
CREATE TABLE tblMajors
(
MajorCode varchar(10) PRIMARY KEY NOT NULL,
MajorDescription varchar(MAX) NOT NULL,
)
CREATE TABLE tblInstructors
(
InstructorNumber int PRIMARY KEY NOT NULL,
InstructorFirst varchar(50) NOT NULL,
InstructorLast varchar(50) NOT NULL,
ContractStatus varchar(10) NOT NULL,
PhoneNumber bigint NOT NULL,
)
CREATE TABLE tblStudents
(
StudentNumber int PRIMARY KEY NOT NULL,
StudentFirst varchar(20) NOT NULL,
StudentLast varchar(20) NOT NULL,
MajorCode varchar(10) FOREIGN KEY REFERENCES tblMajors (MajorCode),
)
CREATE TABLE tblCourses
(
CourseCode varchar(10) PRIMARY KEY NOT NULL,
CourseDescription varchar(MAX) NOT NULL,
)
CREATE TABLE tblGrades
(
StudentNumber bigint FOREIGN KEY REFERENCES tblStudents (StudentNumber),
CourseCode varchar(10) FOREIGN KEY REFERENCES tblCourses (CourseCode),
Grade int NOT NULL,
IntructorNumber int FOREIGN KEY REFERENCES tblInstructors (IntructorNumber),
PRIMARY KEY (StudentNumber, CourseCode)
)

Is a query against an index containing all columns of a table as efficient as a query against the table itself?

I created a table in SQL Server like this:
CREATE TABLE [dbo].[WordForm] (
[WordFormId] VARCHAR (20) NOT NULL,
[Ascii] AS (ascii([WordFormId])) PERSISTED,
[WordId] VARCHAR (20) NOT NULL,
[Primary] BIT DEFAULT ((0)) NOT NULL,
[PosId] INT NOT NULL,
[Definition] VARCHAR (MAX) NULL,
[Sample1] VARCHAR (MAX) NULL,
[Synonym] VARCHAR (100) NULL,
[CreatedBy] INT NOT NULL,
[CreatedDate] DATETIME NOT NULL,
[ModifiedBy] INT NOT NULL,
[ModifiedDate] DATETIME NOT NULL,
[Version] ROWVERSION NOT NULL,
[Sample2] VARCHAR (MAX) NULL,
[Sample3] VARCHAR (MAX) NULL,
[Sample4] VARCHAR (MAX) NULL,
[Sample5] VARCHAR (MAX) NULL,
PRIMARY KEY CLUSTERED ([WordFormId] ASC),
CONSTRAINT [FK_WordFormPos] FOREIGN KEY ([PosId]) REFERENCES [dbo].[Pos] ([PosId]),
CONSTRAINT [FK_WordFormWord] FOREIGN KEY ([WordId]) REFERENCES [dbo].[Word] ([WordId])
);
I used the WordFormId as the primary key as it is unique and would be easy to look up.
Now on my front-end I realize there may be an advantage in me creating an identity key
and using that for a primary key for this table instead of WordFormId. Note that to put things in perspective this table will
hold a maximum of 9,000 rows. Data is inserted, added and updated very infrequently so I am not concerned about the time taken to update the index.
What I am thinking to do now is to:
a) Add an identity column as the primary key
b) Create an index of every column and order by WordId.
If I was to do these two things (a) and (b) would that be as efficient for queries? My thinking is
that the query would use the index most of the time and this would be very similar to
before when I was using WordId as the primary key.

Row update if row exists. Insert it if row doesn't exist

I'm developing a SQL SERVER 2012 express and developer solution.
I will receive an xml in an stored procedure. In the stored procedure I will parse the xml and insert its data into a table.
My problem here is that in this xml could contain data that exists on the table, and I need to update the data on the table with the new one.
I don't want to check if each row in xml exists on the table.
I think I can use IGNORE_DUP_KEY but I'm not sure.
How can I update or insert new data without checking it?
This is the table where I want to insert (or update) the new data:
CREATE TABLE [dbo].[CODES]
(
[ID_CODE] [bigint] IDENTITY(1,1) NOT NULL,
[CODE_LEVEL] [tinyint] NOT NULL,
[CODE] [nvarchar](20) NOT NULL,
[COMMISIONING_FLAG] [tinyint] NOT NULL,
[IS_TRANSMITTED] [bit] NOT NULL,
[TIMESPAN] [datetime] NULL,
[USERNAME] [nvarchar](50) NULL,
[SOURCE] [nvarchar](50) NULL,
[REASON] [nvarchar](200) NULL
CONSTRAINT [PK_CODES] PRIMARY KEY CLUSTERED
(
[CODE_LEVEL] ASC,
[CODE] ASC
)
)
The "IGNORE_DUP_KEY" parameter ,is ignore inserting new row, if he is already exists, but it is not dealing with update in case it exists.
the solution to your request is by MERGE or DML operation (INSERT/UPDATE/DELETE) .
BTW,
The parameter "IGNORE_DUP_KEY" is covering existsnce for the index key only (index column).

Sql Server - Insert data in one table along with the new auto-incremented inserted ids of another table

I have 2 databases DB1 and DB2. I need to write query to copy data from DB2 to DB1.
Both the databases have same table structure.
For Example:
CREATE TABLE DB1.Group(
GroupID [int] IDENTITY(1,1) NOT NULL,
[Company] [varchar](10) NOT NULL,
[Description] [varchar](1000) NOT NULL
)
CREATE TABLE DB1.Instance(
[InstanceID] [int] IDENTITY(1,1) NOT NULL,
[Description] [varchar](1000) NOT NULL,
[GroupID] [int] NOT NULL,
)
I read the data from DB2.Group and insert it in DB1.Group :
Insert into DB1.Group (Company,Description)
select Company,Description from DB2.Group
The GroupID is auto-incremented in DB1. And this I do not want to turn off as will conflict with the existing data.
Now, while inserting data into the DB1.Instance, I need to provide the new auto-incremented insert ids (GroupID) of DB1.Group table
Insert into DB1.Instance (Description,GroupID)
select Description, GroupID from DB2.Instance
Please guide me how can I do that.
Thanks.
Insert you first table with the new keys (leave the pk blank on insert) and make a (temporary) col in the DB1 table for the old key. lookup (join) your second insert on the old key column to get your new fk. When your done delete the old key column and your done.
Here is the sql:
CREATE TABLE DB1.Group(
GroupID [int] IDENTITY(1,1) NOT NULL,
[Company] [varchar](10) NOT NULL,
[Description] [varchar](1000) NOT NULL,
[old_key] [int]
)
CREATE TABLE DB1.Instance(
[InstanceID] [int] IDENTITY(1,1) NOT NULL,
[Description] [varchar](1000) NOT NULL,
[GroupID] [int] NOT NULL,
)
Insert into DB1.Group (Company,Description, old_key)
select Company,Description,GroupID from DB2.Group
Insert into DB1.Instance (Description,GroupID)
select Description, DB1.Group.GroupID
from DB2.Instance join DB1.Group ON DB1.Group.old_key = DB2.Instance.GroupID
ALTER TABLE DB1.Group DROP COLUMN old_key

Resources