SSAS 2005 dsv table from view wrong primary key - ssas

I created a table in DSV from a view:
CREATE VIEW xxx
AS
select ID, code, description from dbo.xxx
where r_types = 'd'
The underlying table (xxx) has ID as primary key. But when I added this view in DSV SSAS automatically identify code as the primary key. Why? And How can I change this?

Related

Create a table with a foreign key referencing to a temporary table generated by a query

I need to create a table having a field, which is a foreign key referencing to another query rather than existing table. E.g. the following statement is correct:
CREATE TABLE T1 (ID1 varchar(255) references Types)
but this one throws a syntax error:
CREATE TABLE T2 (ID2 varchar(255) references SELECT ID FROM BaseTypes UNION SELECT ID FROM Types)
I cannot figure out how I can achieve my goal. In the case it’s needed to introduce a temporary table, how can I force this table being updated each time when tables BaseTypes and Types are changed?
I am using Firebird DB and IBExpert management tool.
A foreign key constraint (references) can only reference a table (or more specifically columns in the primary or unique key of a table). You can't use it to reference a select.
If you want to do that, you need to use a CHECK constraint, but that constraint would only be checked on insert and updates: it wouldn't prevent other changes (eg to the tables in your select) from making the constraint invalid while the data is at rest. This means that at insert time the value could meet the constraint, but the constraint could - unnoticed! - become invalid. You would only notice this when updating the row.
An example of the CHECK-constraint could be:
CREATE TABLE T2 (
ID2 varchar(255) check (exists(
SELECT ID FROM BaseTypes WHERE BaseTypes.ID = ID2
UNION
SELECT ID FROM Types WHERE Types.ID = ID2))
)
For a working example, see this fiddle.
Alternatively, if your goal is to 'unite' two tables, define a 'super'-table that contains the primary keys of both tables, and reference that table from the foreign key constraint. You could populate and update (eg insert and delete) this table using triggers. Or you could use a single table, and replace the existing views with an updatable view (if this is possible depends on the exact data, eg IDs shouldn't overlap).
This is more complex, but would give you the benefit that the foreign key is also enforced 'at rest'.

Define a relationship between two SQL Server tables (parent to child column) without the use of unique or key columns

I am trying to build a table which will hold the 'relationship' of a parent table and a child table. However each column in both tables are no keys or unique and there are duplicate values in each.
Example
Table A - Parent (Fact)
**CartNumber**
Table B - Child
**CartNumber** not unique
CartValue
CartNumber from table A links to CartNumber in B.
I have tried to implement a foreign key with NOCHECK but of course that will not work since the child column is not a primary key or unique. Bear in mind, I am ONLY trying to define that there is a link between the two columns/tables. Is there any way to define a 'loose' relationship between the two columns? Preferably a method where I can reference the sys views or information schema to extract this information
To be honest: This design smells and - if possible - you should think about changing this...
There is no chance to define a FOREIGN KEY CONSTRAINT on non-unique columns the way you describe it.
But: To define a JOIN there is no need for a FK!
My suggestion:
Create a VIEW like this:
CREATE VIEW dbo.MyView
AS
SELECT a.Col1,a.Col2,...
,b.Col1,b.Col2,...
FROM TableA AS a
[INNER/LEFT/RIGHT/FULL OUTER] JOIN TableB AS b ON a.RelField=b.RelField;
With such a VIEW you will get the data joined on this non-unique information.
UPDATE
Taken form your comment:
the end goal is just to provide an external web service with information that says Column A from Table A is used to join onto Column B from Table B
You can create a meta-table like this:
CREATE TABLE dbo.ColumnReference
(
ColumnReferenceID INT IDENTITY
,TABLE_NAME_A VARCHAR(255) NOT NULL
,COLUMN_NAME_A VARCHAR(255) NOT NULL
,TABLE_NAME_B VARCHAR(255) NOT NULL
,COLUMN_NAME_B VARCHAR(255) NOT NULL
);
--inlcude SCHEMA if needed...
In this case you can maintain these relations in your own structure..., you might even add details, rules, what ever...
The web service will call this directly. You might use a VIEW to combine existing relations (defined as FK CONSTRAINT) with your own meta table (simply with UNION ALL).

Adding in a primary key to an SQL view

I have created a view in a SQL Server database which is just a join of two tables.
Is there any way I can insert a unique primary key into the rows of this view ...or I'm not sure how I can specify one of the column names to be a primary key...any ideas?
Thanks
You would have to create materialized (indexed) view in order to be able to add unique index. But you can't create PK constraint.
CREATE VIEW v_test
WITH SCHEMABINDING --optional
AS
SELECT id from table
GO
CREATE UNIQUE CLUSTERED INDEX idx_id
ON v_test (id)
GO

Create a sql foreign key constraint

I wish to create a constraint that state as below
Code.CodeTable ( CodeID smallint, CategoryID smallint,....) --> Parent Table
Admin.Document( DocumentTypeID smallint,.....) --> Child Table
The FK will be Admin.Document.DocumentTypeID map with Code.CodeTable.CodeID
I wish to have the constraint that only check Code.CodeTable.CodeID which the Code.CodeTable.CategoryID = 15 only.
As OMG Ponies already said - you cannot create fk constraints across databases, but if those are just odd table names with dots in them (highly discouraged! since SQL Server already uses a dotted schema: (database).(schema).(object name) and thus having dots in your table names is just asking for trouble at some point....), then you should be able to create your constraint like this:
ALTER TABLE [Admin.Document]
ADD CONSTRAINT FK_AdminDocument_CodeTableCodeID
FOREIGN KEY(DocumentTypeID) REFERENCES [Code.CodeTable](CodeID)
Since you have dots in your table names, you need to enclose those names in square brackets [].
Basically, you need to modify the child table and tell SQL Server which column in that child table refers to what parent table and column in the parent table.

Create a one to many relationship using SQL Server

How do you create a one to many relationship using SQL Server?
Define two tables (example A and B), with their own primary key
Define a column in Table A as having a Foreign key relationship based on the primary key of Table B
This means that Table A can have one or more records relating to a single record in Table B.
If you already have the tables in place, use the ALTER TABLE statement to create the foreign key constraint:
ALTER TABLE A ADD CONSTRAINT fk_b FOREIGN KEY (b_id) references b(id)
fk_b: Name of the foreign key constraint, must be unique to the database
b_id: Name of column in Table A you are creating the foreign key relationship on
b: Name of table, in this case b
id: Name of column in Table B
This is a simple example of a classic Order example. Each Customer can have multiple Orders, and each Order can consist of multiple OrderLines.
You create a relation by adding a foreign key column. Each Order record has a CustomerID in it, that points to the ID of the Customer. Similarly, each OrderLine has an OrderID value. This is how the database diagram looks:
In this diagram, there are actual foreign key constraints. They are optional, but they ensure integrity of your data. Also, they make the structure of your database clearer to anyone using it.
I assume you know how to create the tables themselves. Then you just need to define the relationships between them. You can of course define constraints in T-SQL (as posted by several people), but they're also easily added using the designer. Using SQL Management Studio, you can right-click the Order table, click Design (I think it may be called Edit under 2005). Then anywhere in the window that opens right-click and select Relationships.
You will get another dialog, on the right there should be a grid view. One of the first lines reads "Tables and Columns Specification". Click that line, then click again on the little [...] button that appears on the right. You will get this dialog:
The Order table should already be selected on the right. Select the Customer table on the left dropdown. Then in the left grid, select the ID column. In the right grid, select the CustomerID column. Close the dialog, and the next. Press Ctrl+S to save.
Having this constraint will ensure that no Order records can exist without an accompanying Customer record.
To effectively query a database like this, you might want to read up on JOINs.
This is how I usually do it (sql server).
Create Table Master (
MasterID int identity(1,1) primary key,
Stuff varchar(10)
)
GO
Create Table Detail (
DetailID int identity(1,1) primary key,
MasterID int references Master, --use 'references'
Stuff varchar(10))
GO
Insert into Master values('value')
--(1 row(s) affected)
GO
Insert into Detail values (1, 'Value1') -- Works
--(1 row(s) affected)
insert into Detail values (2, 'Value2') -- Fails
--Msg 547, Level 16, State 0, Line 2
--The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Detail__MasterID__0C70CFB4".
--The conflict occurred in database "Play", table "dbo.Master", column 'MasterID'.
--The statement has been terminated.
As you can see the second insert into the detail fails because of the foreign key.
Here's a good weblink that shows various syntax for defining FK during table creation or after.
http://www.1keydata.com/sql/sql-foreign-key.html
If you are not using SSMS then here is the syntax:
ALTER TABLE <table_name>
ADD <constraint_name> FOREIGN KEY
(<column_name1> ,
<column_name2> )
REFERENCES <table_name>
(<column_name1> ,
<column_name2>)
http://infogoal.com/sql/sql-add-foreignkey.htm
If you are talking about two kinds of enitities, say teachers and students, you would create two tables for each and a third one to store the relationship. This third table can have two columns, say teacherID and StudentId.
If this is not what you are looking for, please elaborate your question.