I have given two table, and I want to solve following tasks:
- Write the SQL DDL for creating a new table called Job that relates a person to a company in a Many-to-Many relationship.
Provided an SQL parameter called
@searchValue
, create an SQL query that will return all the people who worked for a company where the name or description contains the search term.
I have studied https://social.technet.microsoft.com/wiki/contents/articles/19854.tutorial-many-to-many-dimension-example-for-sql-server-analysis-services.aspx and a similar question like How to make SQL many-to-many same-type relationship table but still can not figure it out.
I have tried solving question (1) as below but wondering that is it recommend to add job-id
as primary key in job table?
create table Job
(
person_id int,
company_id int,
CONSTRAINT person_cat_pk PRIMARY KEY (person_id, company_id),
CONSTRAINT FK_ person
FOREIGN KEY (person_id) REFERENCES person (person_id),
CONSTRAINT FK_company
FOREIGN KEY (company_id) REFERENCES category (company_id)
);
I also tried solving question (2):
SELECT * FROM Company WHERE (name LIKE '%' + @searchValue + '%') OR (description LIKE '%' + @searchValue + '%')
Then, what is the role of Job table in question 2? Is this query correct?