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.
Related
I need to move one table from one database (used for development) to another (used by user applications/the purpose of the database).
So far I do it with the select or into statement.
That works fine with just one drawback.
When doing this I lose my primary (PK) and foreign keys (FK).
Now I'm handling that by alter the tables after copying them, see the example below, but, it would be beautiful and more effective (and secure copying process) if I could do a copy of the tables with PK and FK intact all the way.
select *
into DB2.dbo.Ta
from DB1.dbo.Ta
alter table DB2.dbo.Ta add primary key (id)
alter table DB2.dbo.Ta add foreign key (Tb_id) references DB2.dbo.Tb
(Where the attribute Tb_id is the PK in table Tb, already existing in the database DB2.)
I work the Microsoft SQL server standard version 2017 and Microsoft SQL server express database engines with Microsoft SQL Studio Management version 2018.
I named my tables as Employee and Department whereas in MSSMS it is displayed as dbo.Employee and dbo.Department respectively and the query works if I use either of them, Why is it so?
dbo schema is the default schema when you create tables, views and etc in SQL Server.
When you create a table it will be placed in default schema and you can call it like dbo.tableName or just tableName.
Schemas
SQL Server ships with ten pre-defined schemas that have the same names as the built-in database users and roles. These exist mainly for backward compatibility. You can drop the schemas that have the same names as the fixed database roles if you do not need them.
You cannot drop the following schemas:
Default schemas :
dbo
guest
sys
INFORMATION_SCHEMA
For more info see : Microsoft SQL Server schemas
You can create your schemas to allow you to better manage your objects too.
dbo is a so-called schema. This is similar to a folder.
Every table, view, procedure, etc. is always put into a schema.
But if you don't specify the schema, then dbo is used.
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.
I found SimpleMembershipProvider to be pretty neat and productive.
I was wondering if there is a way to control the generate table/column names/datatypes.
You can control the table name for the user table and the column names for the user id and user name columns in it. You specify those in the InitializeDatabaseConnection method. The SimpleMembershipProvider expects the UserId column to be an IDENTITY column. It uses ##IDENTITY to obtain the ID of newly created records. Currently, the SimpleMembershipProvider only works with SQL Server (Express or Full) or SQL Compact 4.0 databases.
You can't change the schema of the membership or roles tables. The SQL for managing accounts and using those tables is hard-coded into the SimpleMemberhipProvider.
I'm converting database from Teradata to SqlServer. I've noticed all tables and procedures are named by the prefix "dbo." (e.g. "dbo.Table1").
I would like to know if and how I can get rid of "dbo" because it would make the conversion task a lot more easier.
dbo is not part of the table name. It is the name of the schema that the tables and stored procedures are attached to. dbo is the default schema in SQL server, though you can add others if needed.
See this MSDN article about them.
dbo is the schema, you can specify a new schema if you like. dbo is default is sql server.
All tables have to go into a schema. As durilai says, dbo is the "default" schema for SQL Server (it always exists). However, note that different users can have different default schemas (if more than one exists).
If you make reference to a table without specifying the schema, then SQL Server will search in your default schema for it (and the same goes for any other objects).
So, if your default schema is dbo, the following two statements are equivalent:
select * from Table1
select * from dbo.Table1
dbo is the default schema in SQL Server if no schema was set by user,
if you want you can create new schema and create tables to it.
Schemas are useful. For example, I used to grant an app login execute permission to every stored procedure separately. Now I grant the app login execute permission to the schema that contains all the stored procedures. This is much easier.