MS SQL Server - rename table name but don't change all queries - sql

I need to rename a table. There are a lot of different queries. Is there something like a global table alias? I want to have ability to use old queries. So I expect to use two table's names for the same table.

You could use SYNONYM:
CREATE SYNONYM OldTableName FOR DBName.SchemaName.NewTableName;

Related

What does double dots .. mean in SQL Server?

I am using SQL Server. I found the following way to backup a database table:
-- Taking a backup
SELECT * INTO MY_BACKUP_DATABASE..CustomersTemporaryTable FROM Customers
I am trying to understand the .. in the syntax. From what I understand, the sentence means that Customers is the table that is going to be backed-up by placing it all of its content into the database called MY_BACKUP_DATABASE using CustomersTemporaryTable as the destination table. I assume when executing the sentence, CustomersTemporaryTable must already exist. Is my understanding of the sentence to take a backup correct?
Each MS SQL Table identifiers can have a name compound of three parts separates with a dot :
the database name
the SQL schema name (by default dbo)
the table, view or Table UDF name
Syntax :
db_name.schema_name.table_name
But it is not always necessary to specify the three parts.
Inside the current database, no need to specify the db_name. It's implicit...
By default every SQL user is associate with a specific default schema (frequently dbo too...).
So you can specify a table name with :
schema_name.table_name
...SQL Server will try to find the table into the current DB
db_name..table_name
...SQL Server will try to find the table into the specified DB and the default user schema
table_name
...SQL Server will try to find the table into the current DB and the
default user schema
To know with SQL schema is associated with your SQL user, use :
SELECT SCHEMA_NAME() AS DEFAULT_CURRENT_USER_SCHEMA
To know all the associations between SQL users and SQL schemas, do :
SELECT name AS USER_NAME, default_schema_name
FROM sys.database_principals
WHERE type_desc LIKE '%?_USER' ESCAPE '?'
First of all, understand that what you are doing is not "taking a backup", it is inserting data into a table from another table. If you have not created the destination table the syntax is like this:
Select *
INTO Destination_Table
FROM Source_Table
The destination table will be created automatically. This doesn't necessarily work so well if you will be inserting additional data that might be different lengths or data types, but for a one of select should work fine.

Permanently Alias a Tablename SQL

I have some old database things which are interconnected to some other systems, I have a new database configuration with table names that are somewhat different than the original older database - I want the older systems to still be able to update the database tables as if nothing changed so to speak.
I know in a query I can alias the table name - I am not looking for how to do that.
I want to set the alias in a more permanent fashion; is there a way to do this in SQL Server? If so how?
I had thought on adding a trigger to the original tables on insert, delete, update to accomplish this - but was hoping for something more elegant than to do this for each of the tables I have to do this with.
If the structure of the tables are identical, you can use synonyms.
CREATE SYNONYM <new_table_name>
FOR <old_table_name>;
Otherwise you'll need (updatable) views, possibly with INSTEAD OF triggers implementing the translation.
You can create a view, which in many ways will appear to behave like a table.
create view aliasname
as
select fields1, field2
from originalname
https://learn.microsoft.com/en-us/sql/t-sql/statements/create-view-transact-sql?view=sql-server-ver15

SQL Server Table Schema Name

I have some questions about the schema in a table.
Sometime when you create a table the default schema is dbo.TableName. Is the dbo the default schema name? I believe you can change or specify the schema when creating a table right, because there are tables that have different schema like: Sales.Tablename or Users.Roles, etc. I believe the purpose of a schema is to make a difference between tables or something like that? Something like a namespace within a C# class. Is it possible to have two tables with same name but a different schema, like: Sales.Users, Marketing.Users ?
dbo is the default schema. You can change the default schema for each sql-login.
If you accidentally create a table in the wrong schema, you can move it:
-- Moving Peter table from Sales schema to Orders schema
ALTER SCHEMA Orders TRANSFER Sales.peter
You can specify which schema to create the table in by specifying it before the table name:
CREATE TABLE Sales.Users(id int);
One of the purposes of schemas is to create logical groups of tables, just like namespaces in C#. They are also useful for controlling permissions and more.
Yes, table names only need to be unique within each schema..
Sometime when you create a table the default schema is dbo.TableName. Is the dbo
the default schema name?
Why do you ask? It is quite obvious that dbo is the default schame name if you get it as default, or? On top it is the only usable schema a new database has.
I believe you can change or specify the schema when creating a table right,
What sense would multiple schemata have if you could not use them? And as the create table syntax clearly states you can specify a schema.
I believe the purpose of a schema is to make a difference between tables or
something like that? Something like a namespace within a C# class.
That pretty much sums it up.
Is it possible to have two tables with same name but a different schema,
What about you spend 10 seconds to try it out? Are you challenged by he concept of trying something totally simplistic out? And the answer is yes. object names have to be unique - within their schema.

Copying a table in SQL Server

Is it possible to copy a table (with definition, constraints, identity) to a new table?
Generate a CREATE script based on the table
Modify the script to create a different table name
Perform an INSERT from selecting everything from the source table
No, not really, you have to script it out, then change the names
you can do this
select * into NewTable
FROM OldTable
WHERE 1 =2 --if you only want the table without data
but it won't copy any constraints
It's not the most elegant solution, but you could use a tool like the free Database Publishing Wizard from Microsoft.
It creates an SQL script of the table definition including data and including indexes and stuff. But you would have to alter the script manually to change the table name...
Another possibility:
I just found this old answer on SO.
This script is an example to script the constraints of all tables, but you can easily change it to select only the constraints of "your" table.
So, you could do the following:
Create the new table with data like SQLMenace said (select * into NewTable from OldTable)
Add constraints, indexes and stuff by changing this SQL script

Creating table in mysql

Is it possible to create more than one table at a time using single create table statement.
For MySQL, you can use multi-query to execute multiple SQL statements in a single call. You'd issue two CREATE TABLE statements separated by a semicolon.
But each CREATE TABLE statement individually can create only one table. The syntax supported by MySQL does not allow multiple tables to be created simultaneously.
#bsdfish suggests using transactions, but DDL statements like CREATE TABLE cause implicit transaction commits. There's no way to execute multiple CREATE TABLE statements in a single transaction in MySQL.
I'm also curious why you would need to create two tables simultaneously. The only idea I could come up with is if the two tables have cyclical dependencies, i.e. they reference each other with foreign keys. The solution to that is to create the first table without that foreign key, then create the second table, then add the foreign key to the first table with ALTER TABLE ADD CONSTRAINT. Dropping either table requires a similar process in reverse.
Not with MS SQL Server. Not sure about mysql.
Can you give more info on why you'd want to do this? Perhaps there's an alternative approach.
I don't know, but I don't think you can do that. Why you want to do this?
Not in standard SQL using just the 'CREATE TABLE' statement. However, you can write multiple statements inside a CREATE SCHEMA statement, and some of those statements can be CREATE TABLE statements. Next question - does your DBMS support CREATE SCHEMA? And does it have any untoward side-effects?
Judging from the MySQL manual pages, it does support CREATE SCHEMA as a synonym for CREATE DATABASE. That would be an example of one of the 'untoward side-effects' I was referring to.
(Did you know that standard SQL does not provide a 'CREATE DATABASE' statement?)
I don't think it's possible to create more than one table with a 'CREATE TABLE' command. Everything really depends on what you want to do. If you want the creation to be atomic, transactions are probably the way to go. If you create all your tables inside a transaction, it will act as a single create statement from the perspective of anything going on outside the transaction.