Database Tables with possible Join Candidates - sql-server-2008

Let's say I have some db tables created in SQL Server, those could be named Table1, Table2, Table3, Table4 and Table5 (but could be more).
Now, what I want to achieve is to possibly mark the columns in those tables that could possibly indicate a candidate I could use for joining the tables.
Is this possible somehow with SQL? Or is there any SW Tool which could create a database model upon the data/tables which would join the tables with same column names?
Example could be with Business Intelligence table structures: I could have 5 tables and wanted to decide which dimension is shared for fact tables etc.

You could view all the columns with a certain piece of text in it, such as "%ID"
Use this script:
SELECT t.name As TableName, c.name AS ColumnName
FROM sys.tables AS t
INNER JOIN sys.columns AS c
ON t.[object_id] = c.[object_id]
WHERE c.name Like ('%ID') --You can change ID to anything you need
AND t.is_ms_shipped = 0
AND t.name NOT LIKE '#%';
A similar question was posted here: How to select column names from multiple tables in SQL Server 2000-2008 that are in a set of names

Related

assigning relationships between tables using common filelds in MYSQL database

Hi , Here my scenario is too create the relationship between the
existing tables which are not having/not defined any relation but
having common field names in multiple tables of database .
is there any tool to generate/achieve above explained scenario.
I think this should do the trick:
SELECT T1.id, T1.Name, T1.sal, T2.Sid, T2.department, T2.category
FROM T1 NATURAL JOIN T2
This will join the tables on the common field(s), leaving only the rows where they both have the same value(s) on the field(s).

Getting a string from a referenced table

I am relativly new to the SQL language. I can do a basic select, but for performance increase, I'd love to know if it is possible to merge the two queries I am doing at the moment into one.
Scenario: There are two tables. Table one has a few columns, one of them is a VARCHAR(45) named 'user', and another one is a INT which is called 'gid'. In the second table, there is a primary key column called 'gid' (INT) and a column called 'permissions' which is a TEXT column and it contains values seperated by ';'.
I have a user name, and want the text in the permissions column. The current way I do it is by fetching the gid of the first table, then doing a second query with the gid to get the permissions.
I've heard there are other ways to do this, and I have searched on Google, but I'm not sure what I should do.
EDIT:
Like this:
select t2.permissions
from table1 t1, table2 t2
where t1.user = '<SPECIFIED NAME>'
and t1.gid = t2.gid;
or you could use INNER JOIN syntax:
select t2.permissions
from table1 t1
inner join table2 t2 on t1.gid = t2.gid
where t1.user = '<SPECIFIED VALUE>'
To do this you use a JOIN. A join connects two tables in a select statement.
Like this
select *
from usertable u
join permissiontable p on u.gid = p.gid
This will give you all the columns from both tables with the id column joined. You can treat the joined table just like any table (eg select a sub-set of columns in the select list, add a where clause, etc).
You can read more about joins in any intro sql book or doing a google search.

create a new table from a full join

I want to create a new table that includes certain columns from two existing tables. Some columns are in both existing tables, but not all. The column "unique_number" is in both tables. What I want is to check if the same unique_number exists in both tables, and if so then make a single row with all of the columns (from both tables) that correspond to that unique_number. If the unique_number is not in both existing tables then I don't care about including that in my new table.
I can do this in SAS, but I am being forced to do this in SQL with little background knowledge of the program.
It sounds that you need an INNER JOIN instead of a FULL JOIN. This is a way of doing what you want:
SELECT A.unique_number,
A.col1FromA, -- list the other columns from TableA here
B.col1FromB -- list the other columns from TableB here
INTO TableC
FROM TableA A
INNER JOIN TableB B
ON A.unique_number = B.unique_number
An inner join on the "unique_number" column should be all you need, if your description is accurate.
select t1.unique_number, t1.other_column, t2.something_else
into new_table_name
from one_table t1
inner join other_table t2
on t1.unique_number = t2.unique_number;
This glosses over the complication that "some columns are in both tables", and that those columns that are in both tables might have different values. Give some thought to that.
Select...into syntax

Select same column name in microsoft sql server from multiple tables

Is it possible to write a query:
SELECT * from table1, table 2, table 3 ...
where table1.column name like table2.column name like table3.column name
We don't know the name of our column, we say any of our columns which are in table1.column name like table2.column name in like table3.column name
For example: employee id maybe is a column that is common to 3 tables, or publisher id is a column that is common to 3 tables.
I can find the foreign key or primary key with a diagram in my database, but I want to write a query that shows just columns which have a same title as column name.
No clue as to why you are doing this, so a bit hard to steer, but there are a number of tables and views in the database that describe the database.
Information_Schema.Columns is probably where you should start. Note this is meta data, if you want the content of the actual tables you'll have to go a step further, and build some dynamic sql, or equivalent.
Select Column_Name,Count(*) As NumberOfOccurences From Information_Schema.Columns
will give you the column names and how often you've used them for instance.
If you know the column name and want to know in which different table same column name is used then write SQL query as
SELECT schema_name(t.[schema_id]) + '.' + t.[name] AS Tables FROM sys.tables t INNER JOIN sys.columns c on t.[object_id] = c.[object_id] WHERE c.[name] = 'Your_Common_Column_Name'
You will get all the table list with their respective schema
To get column names:
select sc.name
from sysobjects so inner join syscolumns sc on so.id = sc.id
where so.name = 'tableName'

Multiple tables in one view?

Today my question is how would I go about creating a view in a MySQL database that uses more than two tables?
Here is my query (it works) I am not looking to change my current query, mostly looking for a nice reference with examples on this topic.
CREATE OR REPLACE VIEW vw_itemsPurchased AS
SELECT `tbl_buyers`.`fldPrimaryKey` as fldFKeyBuyer, `tbl_buyers`.`fldEmail` as fldBuyerEmail, `tbl_buyers`.`fldAddressStreet`, `tbl_buyers`.`fldAddressCity`, `tbl_buyers`.`fldAddressState`, `tbl_buyers`.`fldAddressZip`, `tbl_buyers`.`fldAddressCountry`, `fldPaymentCurrency`, `fldPaymentGross`, `fldPaymentStatus`, `fldReceiverEmail`, `fldTransactionId`
FROM `tbl_transactions` INNER JOIN `tbl_buyers`
ON `tbl_transactions`.`fldFKeyBuyer` = `tbl_buyers`.`fldPrimaryKey`
Thanks for your time!
To use more than two tables, you simply continue adding JOIN statements to connect foreign keys. Adapting your code to add an imaginary third table tbl_products might look like this:
CREATE OR REPLACE VIEW vw_itemsPurchased AS (
SELECT
tbl_buyers.fldPrimaryKey as fldFKeyBuyer,
tbl_buyers.fldEmail as fldBuyerEmail,
tbl_buyers.fldAddressStreet,
tbl_buyers.fldAddressCity,
tbl_buyers.fldAddressState,
tbl_buyers.fldAddressZip,
tbl_buyers.fldAddressCountry,
fldPaymentCurrency, fldPaymentGross,
fldPaymentStatus,
fldReceiverEmail,
fldTransactionId,
tbl_tproducts.prodid
FROM
tbl_transactions
INNER JOIN tbl_buyers ON tbl_transactions.fldFKeyBuyer = tbl_buyers.fldP
-- Just add more JOINs like the first one..
JOIN tbl_products ON tbl_products.prodid = tbl_transactions.prodid
In the above method, the first and second tables relate, and the first and third tables relate. If you have to relate table1->table2 and table2->table3, list multiple tables in the FROM and relate them in the WHERE. The below is just for illustration and doesn't make much sense, as you probably wouldn't have a customer id in the same table as a product price.
SELECT
t1.productid,
t2.price,
t3.custid
FROM t1, t2, t3
WHERE
-- Relationships are defined here...
t1.productid = t2.productid
AND t2.custid = t3.custid