I'm working on a few ASP.NET MVC projects which all require database functionality. Unfortunately, my hosting provider only gives me two SQL Server databases to work with, so I want to put the tables of multiple projects into a single database.
However, some of my tables are similarly named, so I might run into some issues. Thus, I'm trying to find a way to change the names of all the tables so that they reflect what application they belong to.
Currently, I have the following tables in Project A:
Table1
Table2
In Project B, I have these tables:
Table1
Table2
I would like to combine the tables into a single database:
ATable1
ATable2
BTable1
BTable2
Table1
from my code into ATable1
or BTable1
(depending on what application it is)?In MySQL, there are two ways to find the names of all tables, either by using the "show" keyword or by query INFORMATION_SCHEMA. In the case of SQL Server or MSSQL, You can either use sys. tables or INFORMATION_SCHEMA to get all table names for a database.
Tables do not require prefixes. This is purely up to you. However, we prefix tables with relation to the MODULES in the application they belong to, just to group the tables more easily.
Prefix enables developers to easily see what their code is doing as they write and test their code. Including SQL queries, HTTP calls, errors, logs, and much more. This makes Prefix really handy for viewing SQL queries your code is using. After installing Prefix, it will automatically track all SQL calls.
You can create table or column name such as "15909434_user" and also user_15909434 , but cannot create table or column name begin with numeric without use of double quotes.
Put the tables in separate schemas, e.g.
create schema ProjectA;
create schema ProjectB;
create table ProjectA.Table1 (...);
create table ProjectA.Table2 (...);
create table ProjectB.Table1 (...);
create table ProjectB.Table2 (...);
I haven't tested this myself, but I would be very surprised if LINQ to SQL (and any other ORM for that matter) didn't store and correctly make use of the schema names.
If you write your own SQL commands manually, be aware that you have to quote the schema name on all objects unless it is the default schema for your current database user. This could cause a problem if (a) you have only been allocated one SQL Server login, and (b) you have a lot of existing SQL.
To move existing tables into a new schema, e.g. tables in dbo
into ProjectA
:
alter schema ProjectA transfer dbo.Table1;
alter schema ProjectA transfer dbo.Table2;
To do an automated rename, you could write a simple loop:
declare objectsCursor cursor local fast_forward for
select o.name as objectname, s.name as schemaname
from sys.objects as o
inner join sys.schemas as s on o.schema_id = s.schema_id
-- Alter these filters depending on what you want to convert
where s.name = 'dbo'
and o.type = 'U'
declare @objectname sysname, @schemaname sysname, @sql nvarchar(max)
open objectsCursor
fetch next from objectsCursor into @objectname, @schemaname
while @@fetch_status = 0 begin
select @sql = N'alter schema ProjectA transfer ' + quotename(@schemaname) + '.' + quotename(@objectname)
execute (@sql)
fetch next from objectsCursor into @objectname, @schemaname
end
close objectsCursor
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With