I have a table which has no identity column. I want to change a column's identity specification, but SQL Server 2008 doesn't allow that. So, how can I change identity property in SQL Server 2008?
under tools-->options-->designers-->table and database designers
uncheck prevent saving changes that require table re-creation
If you want to add a new column as an identity column:
ALTER TABLE [tablename] ADD COLUMN [columnName] int NOT NULL IDENTITY(1,1)
GO
ALTER TABLE [tablename] ADD PRIMARY KEY ([columnName])
If you're trying to use the SQL 2008 designer, there is a setting you have to disable so that the designer can drop and recreate the table.
Related
I am new to MS SQL Server, coming from MySQL. I sort of understand MS SQL schemas and their purpose, but I don't see any need for them for small applications with the one DBA.
Is it possible to ignore schemas altogether, for example to create and query tables? If so what would be the format to create a table without specifying a schema? This is in Azure, with an Azure SQL DB.
UPDATE
Thanks to the answer below you don't apparently need to specify a schema when creating a table. Once created, the table will automatically have the schema 'dbo' applied by default.
CREATE TABLE cm_user
(
cm_user_pk int PRIMARY KEY CLUSTERED,
user_code VARCHAR(10) NOT NULL,
first_name VARCHAR(60) NOT NULL,
last_name VARCHAR(60) NOT NULL,
email VARCHAR(100) NOT NULL,
user_type VARCHAR(20) NOT NULL
)
results in the table dbo.cm_user_pk
Is it possible to ignore schemas altogether, for example to create and query tables?
I think, it's impossible. When we create a table in Azure SQL database without specify the default schema, the table will have the same schema with the Azure SQL database user.
In Azure SQL database, when a user created, we must specify the schema, the default schema of server admin and user is DBO. That means all the tables created by user will has the same default schema.
Please reference:
Logins and Users
CREATE USER (Transact-SQL)
CREATE TABLE (Transact-SQL)
Hope this helps.
I could change the index property in one of the table from Unique, Non-Clustered, Filtered to Non-Unique, Non-Clustered in SQL Server on local machine. But I could NOT find any property in accessing Azure SQL Database via SQL Server MAnagement Studio.
How do I change in via TSQL?
After upgrading to latest SSMS 17.9.1, still not be able to change this via this tool So the query will drop first and then recreated:
DROP INDEX [IX_ClubApplicationUser_LastModifiedBy] ON [dbo].[ClubApplicationUser]
GO
CREATE NONCLUSTERED INDEX [IX_ClubApplicationUser_LastModifiedBy] ON [dbo].[ClubApplicationUser]
(
[LastModifiedBy] ASC
)
GO
This will change into NONCLUSTERED, NON-UNIQUE as well as NON-FILTERED.
The reason we change as the "Code First" approach in .NET CORE 2 generated automatically this type of index.
I have two identical SQL Server 2005 databases (MYDB_Pub, MYDB_Sub1) where the merge replication was configured and it works fine.
Recently we upgraded to SQL Server 2014. To test the replication functionality on the new SQL Server I followed the below steps:
Backup the MYDB_Pub on the SQL Server 2005.
Restore the MYDB_Pub on the SQL Server 2014 with the same name.
Restore the same MYDB_Pub on the SQL Server 2014 with name
'MYDB_Sub1'.
Configure Merge Replication on a single table 'Country'.
Now when I run the replication process, I get the following error message:
The table structure is given below:
CREATE TABLE [dbo].[Country](
[CountryId] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[Code] [char](2) NOT NULL,
[CountryName] [varchar](50) NOT NULL,
[rowguid] [uniqueidentifier] ROWGUIDCOL NOT NULL CONSTRAINT [DF_Country_rowguid] DEFAULT (newsequentialid())
)
GO
ALTER TABLE [dbo].[Country] WITH NOCHECK ADD CONSTRAINT [repl_identity_range_BEB70305_9154_4BBE_B898_61681A047BA2] CHECK NOT FOR REPLICATION (([CountryId]>(112251) AND [CountryId]<=(113251) OR [CountryId]>(113251) AND [CountryId]<=(114251)))
GO
ALTER TABLE [dbo].[Country] CHECK CONSTRAINT [repl_identity_range_BEB70305_9154_4BBE_B898_61681A047BA2]
GO
Please note that [rowguid] column was already there in the database before defining the replication. Country table in both publisher and subscriber have similar data as publisher and subscriber were restored from same file.
To configure the replication I used the instruction provided in this article.
Subscription properties and Article properties of Country table are shown in the below screenshots
I have tried to find the solution for this error but nothing helped. I must make it clear that I am not a DBA and its the first time I am playing with replication on SQL Server. I would really appreciate any help.
The reason you are getting the error is because the table already exists on the subscriber, and is populated, and the initialization is trying to re-populate the table with the data from the publisher. It is doing this because your article property says "Action if name is in use = Keep existing object unchanged". I believe the default is "drop table and recreate object" or something like that, which means when you synchronize the initialization, it will drop the table at the subscriber, then populate it. This would avoid the error above, since the table at the subscriber will be empty before it is populated.
So you need to decide, if you need the existing data at the subscriber, or if during initialization, you can let merge replication drop the objects, and repopulate it based off the snapshot from the publisher.
In SQL Server how to change a collation of only selected columns? Is there a way to do that using SQL Server Management Studio?
I was googling around but what I found where instructions how to change collation of a database not a specific column.
EDIT:
So I found this bit of SQL to change column's collation:
ALTER TABLE MyTable
ALTER COLUMN Column1 [TYPE] COLLATE [NewCollation]
Still is there a way to do that using SQL Server Management Studio?
Right-click on table name and choose Design. Then select column and go to Collation in column properties tab (inside Table Designer group).
Visual Studio 2005 doesn't provide an interface for creating relationships between tables in a SQL Server CE database (I'm using version 3.0) and you can't open a Compact Edition DB using Management Studio as far as I know. Any ideas?
Unfortunately there is currently no designer support (unlike for SQL Server 2005) for building relationships between tables in SQL Server CE. To build relationships you need to use SQL commands such as:
ALTER TABLE Orders
ADD CONSTRAINT FK_Customer_Order
FOREIGN KEY (CustomerId) REFERENCES Customers(CustomerId)
If you are doing CE development, i would recomend this FAQ:
EDIT: In Visual Studio 2008 this is now possible to do in the GUI by right-clicking on your table.
Visual Studio 2008 does have a designer that allows you to add FK's. Just right-click the table... Table Properties, then go to the "Add Relations" section.
You need to create a query (in Visual Studio, right-click on the DB connection -> New Query) and execute the following SQL:
ALTER TABLE tblAlpha
ADD CONSTRAINT MyConstraint FOREIGN KEY (FK_id) REFERENCES
tblGamma(GammaID)
ON UPDATE CASCADE
To verify that your foreign key was created, execute the following SQL:
SELECT * FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
Credit to E Jensen (http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=532377&SiteID=1)
Alan is correct when he says there's designer support. Rhywun is incorrect when he implies you cannot choose the foreign key table. What he means is that in the UI the foreign key table drop down is greyed out - all that means is he has not right clicked on the correct table to add the foreign key to.
In summary, right click on the foriegn key table and then via the 'Table Properties' > 'Add Relations' option you select the related primary key table.
I've done it numerous times and it works.
create table employee
(
empid int,
empname varchar(40),
designation varchar(30),
hiredate datetime,
Bsalary int,
depno constraint emp_m foreign key references department(depno)
)
We should have an primary key to create foreign key or relationship between two or more table .
I know it's a "very long time" since this question was first asked. Just in case, if it helps someone,
Adding relationships is well supported by MS via SQL Server Compact Tool Box (https://sqlcetoolbox.codeplex.com/). Just install it, then you would get the option to connect to the Compact Database using the Server Explorer Window. Right click on the primary table , select "Table Properties". You should have the following window, which contains "Add Relations" tab allowing you to add relations.
Walkthrough: Creating a SQL Server Compact 3.5 Database
To create a relationship between the tables created in the previous procedure
In Server Explorer/Database Explorer, expand Tables.
Right-click the Orders table and then click Table Properties.
Click Add Relations.
Type FK_Orders_Customers in the Relation Name box.
Select CustomerID in the Foreign Key Table Column list.
Click Add Columns.
Click Add Relation.
Click OK to complete the process and create the relationship in the
database.
Click OK again to close the Table Properties dialog box.