Question

Create 2 stored procedures: sp_AddNewUser and sp_AddNewRole. Use the ManyToMany script as your base table structures....

Create 2 stored procedures: sp_AddNewUser and sp_AddNewRole. Use the ManyToMany script as your base table structures.

The stored procedure should accept the parameters needed to input the data for each table.

NOTE: You do not need to input the UserID or RoleID. These are surrogate keys and the system automatically inserts them when you insert a row in the tables.  

On execution, the stored procedure should check the database to see if the user exists, if so, return a message that says so. If the user does not exist, it should insert the information for a new user. Then do the same thing (in a different stored procedure) for the Role.

MANYTOMANY SCRIPT

Create database ManyToMany
-- use master
-- drop database ManyToMany
go
use ManyToMany
go

-- Create tables to be used for the discussion
CREATE TABLE [dbo].[tblUsers](
        [Userid] [int] IDENTITY(100,1) NOT NULL PRIMARY KEY,
        [username] [varchar](32) NOT NULL,
        [password] [varchar](32) NOT NULL,
        [FName] [varchar](32) NULL,
        [LName] [varchar](32) NULL,
        [MName] [varchar](32) NULL,
        [eMail] [varchar](128) NOT NULL,
        [Phone] [varchar](16) NULL
) 

go
CREATE TABLE [dbo].[tblRoles](
        [RoleID] [int] IDENTITY(100,1) NOT NULL PRIMARY KEY,
        [Role] [varchar](32) NOT NULL
)
go
CREATE TABLE [dbo].[tblUserRoles](
        [RoleID] [int] NOT NULL,
        [UserID] [int] NOT NULL
) ON [PRIMARY]

go

-- Add the foreign keys to the userroles table for data integrity

alter table tblUserRoles add constraint FK_Users Foreign Key (UserID) references tblUsers(UserID)
alter table tblUserRoles add constraint FK_Roles Foreign Key (RoleID) references tblRoles(RoleID)
go

-- insert sample data to the tblRoles Table

insert into tblRoles values ('Employee')
insert into tblRoles values ('Manager')
insert into tblRoles values ('Salesman')
insert into tblRoles values ('Security')

go

-- Insert sample data ti the tblUsers table
insert into tblUsers ([username],[password],[FName],[LName],[eMail],[Phone])
  values ('derf', 'password1', 'Fred','Flintstone','[email protected]','555-1234')
insert into tblUsers ([username],[password],[FName],[LName],[eMail],[Phone])
  values ('bam', 'bambam', 'Babbam','Rubble','[email protected]','555-5489')
insert into tblUsers ([username],[password],[FName],[LName],[eMail],[Phone])
  values ('boss', 'bigboss', 'Sylvester','Slate','[email protected]','555-0258')
go

-- this is a script block used to get valid key values for the relationships
-- between the tables and the rows.

begin
declare @user int 
declare @role int
set @user = (select userid from tblUsers where username = 'derf')
set @role = (select RoleID from tblRoles where Role = 'Employee')
insert into tblUserRoles (RoleID,UserID) values(@role,@user)
set @user = (select userid from tblUsers where username = 'bam')
insert into tblUserRoles (RoleID,UserID) values(@role,@user)
set @user = (select userid from tblUsers where username = 'boss')
insert into tblUserRoles (RoleID,UserID) values(@role,@user)
set @user = (select userid from tblUsers where username = 'derf')
set @role = (select RoleID from tblRoles where Role = 'Manager')
insert into tblUserRoles (RoleID,UserID) values(@role,@user)
set @user = (select userid from tblUsers where username = 'boss')
insert into tblUserRoles (RoleID,UserID) values(@role,@user)
set @role = (select RoleID from tblRoles where Role = 'Salesman')
insert into tblUserRoles (RoleID,UserID) values(@role,@user)
end
go

-- see the results

select * from tblUsers
select * from tblRoles
select * from tblUserRoles

Homework Answers

Answer #1

CREATE PROCEDURE sp_AddNewUser(
@username as varchar(32) ,
@password as varchar(32),
@FName as varchar(32),
@LName as varchar(32),
@MName as varchar(32),
@eMail as varchar(32),
@Phone as varchar(32)
)
AS
BEGIN
IF not EXISTS (SELECT 1 FROM tblUsers WHERE @username = USERNAME)
BEGIN
INSERT INTO tblUsers
values( @username,@password,@FName,@LName,@MName,@eMail,@Phone )
END
ELSE
BEGIN
RAISERROR('=============================', 0, 1);
RAISERROR('USERNAME ALREDY EXISTS', 0, 1);
RAISERROR('=============================', 0, 1);
END
END
CREATE PROCEDURE sp_AddNewRole(
@Role as varchar(32)
)
AS


BEGIN
IF NOT EXISTS (SELECT 1 FROM tblRoles WHERE Role = @Role)
BEGIN
INSERT INTO tblRoles
values( @Role )
END
ELSE
BEGIN
RAISERROR('=============================', 0, 1);
RAISERROR('USER ROLE ALREDY EXISTS', 0, 1);
RAISERROR('=============================', 0, 1);
END
END

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
At the MySQL prompt, create a new database table, insert data into the table and query...
At the MySQL prompt, create a new database table, insert data into the table and query the table as follows: mysql>CREATE TABLE users ( username VARCHAR(100) PRIMARY KEY, password VARCHAR(100) NOT NULL, firstname VARCHAR(100) NOT NULL, lastname VARCHAR(100) NOT NULL, ); mysql>INSERT INTO users VALUES(‘testuser’,’password’,’test’,’user’); mysql>SELECT * FROM users
Create an application that will give valuable advice to future students from someone (you!) who is...
Create an application that will give valuable advice to future students from someone (you!) who is close to graduation. However, only end-users who have their credentials validated against the database (which uses encrypted passwords) are allowed entry. ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- advice_ddl.sql CREATE DATABASE advice; USE advice; CREATE TABLE users ( id int primary key auto_increment, username varchar(255), password varchar(255) ); -- insert a row into the users table: -- username = foo -- password = bar INSERT INTO users (username, password) VALUES...
If you had to create a new table in your database called EMPLOYEE and load data...
If you had to create a new table in your database called EMPLOYEE and load data into it using SQL commands below. CREATE TABLE EMPLOYEE (               EMP_ID INT PRIMARY KEY,               EMP_FNAME VARCHAR(200) NOT NULL,               EMP_LNAME VARCHAR(200) NOT NULL,               EMP_CITY VARCHAR(500) NULL ,               EMP_STATE VARCHAR(20) NULL ) INSERT INTO EMPLOYEE VALUE (1, ‘Kevin’, ‘Lailnay’, NULL, ‘PA’) INSERT INTO EMPLOYEE VALUE (2, ‘Sarita, ‘Kawn, ‘Princeton’,’NJ’) INSERT INTO EMPLOYEE VALUE (2, ‘Gloria, ‘Pitts, ‘Allentown, ‘PA’) INSERT INTO EMPLOYEE...
-- Table construction (just 1 simplest possible way) CREATE TABLE PetType ( petTypeId VARCHAR(10) PRIMARY KEY,...
-- Table construction (just 1 simplest possible way) CREATE TABLE PetType ( petTypeId VARCHAR(10) PRIMARY KEY, animalType VARCHAR(20), breed VARCHAR(20) ); CREATE TABLE Owner ( ownerId VARCHAR(10) PRIMARY KEY, firstName VARCHAR(20), lastName VARCHAR(20) NOT NULL, homePhoneNumber VARCHAR(20), streetAddress VARCHAR(80), suburb VARCHAR(20), postcode VARCHAR(10) ); CREATE TABLE Pet ( petId VARCHAR(10) PRIMARY KEY, petName VARCHAR(20), sex CHAR(1) CHECK (sex IN ('M', 'F')), petTypeId VARCHAR(10) FOREIGN KEY REFERENCES PetType ); CREATE TABLE PetAndOwner ( ownerId VARCHAR(10), petId VARCHAR(10), PRIMARY KEY (ownerId, petId),...
Space X Bank CREATE TABLE Branch(BranchIDNumber VARCHAR(15) PRIMARY KEY NOT NULL, BranchName VARCHAR(6) NOT NULL, Address...
Space X Bank CREATE TABLE Branch(BranchIDNumber VARCHAR(15) PRIMARY KEY NOT NULL, BranchName VARCHAR(6) NOT NULL, Address VARCHAR(50) NOT NULL, City TEXT NULL, State CHAR(2) NULL, ZipCode INT(11) NOT NULL, OfficeNum VARCHAR(15) NOT NULL, FaxNum VARCHAR(15) NOT NULL); CREATE TABLE Employee(EmployeeIDNumber VARCHAR(15) NOT NULL,FirstName VARCHAR(35) NOT NULL, LastName VARCHAR(35) NOT NULL, Email VARCHAR(100) NOT NULL, BranchIDNumber VARCHAR(11) NOT NULL, FOREIGN KEY(BranchIDNumber) REFERENCES Branch(BranchIDNumber), JobTitle ENUM("Manager","Staff") NOT NULL, Salary DECIMAL(8, 2) NOT NULL, HomeNumber VARCHAR(13) NULL, CellNumber VARCHAR(13) NOT NULL); CREATE TABLE...
SQL Data: you can just copy paste it into mysql to. CREATE DATABASE University;USE University; CREATE...
SQL Data: you can just copy paste it into mysql to. CREATE DATABASE University;USE University; CREATE TABLE Student (  sid INT PRIMARY KEY,  name VARCHAR(20) NOT NULL,  address VARCHAR(20) NOT NULL,  major CHAR(2)); CREATE TABLE Professor (  pid INT PRIMARY KEY,  name VARCHAR(20) NOT NULL,  department VARCHAR(10) NOT NULL); CREATE TABLE Course (  cid INT PRIMARY KEY,  title VARCHAR(20) NOT NULL UNIQUE,  credits INT NOT NULL,  area VARCHAR(5) NOT NULL); CREATE TABLE Transcript (  sid INT,  cid INT,  pid INT,  semester VARCHAR(9),  year YEAR,  grade CHAR(1) NOT NULL,  PRIMARY KEY (sid, cid, semester, year),  FOREIGN KEY (sid) REFERENCES Student...
SQL DATABASE Task 4 [1.5 marks] Create Index (0.5 marks) Currently, the database only contains a...
SQL DATABASE Task 4 [1.5 marks] Create Index (0.5 marks) Currently, the database only contains a small number of records. However, the data contained within it is expected to grow significantly in the future. Creating indexes on commonly searched columns is a way performance issues can be minimized. Write a command to create an index on student_name column of the student table. Create view – 1 mark Write a command to create a view to list the student ID and...
1) Add to a relational table DEPARTMENT information about the total number of employees in each...
1) Add to a relational table DEPARTMENT information about the total number of employees in each department. Note, that if a department has not employee then for such a department the total number of employees must be set to zero (0). The total number of employees must be a positive number no more than 999. Use SELECT statement to list the contents of an extended relational table DEPARTMENT in the descending order of the total number of employees. Finally, remove...
PLEASE USE MICROSOFT SQL SERVER MANAGEMENT STUDIO 18 The purpose of this task is to create...
PLEASE USE MICROSOFT SQL SERVER MANAGEMENT STUDIO 18 The purpose of this task is to create a student table such that it has 2 partitions. One that will include all student id's through 500 and a second one that will include all student id's above 500. StudentID                   int           StudentName              varchar(30) *StudentID should be a a primary key. **StudentName should be required. Do you not use the master filegroup. Each partition should be associated with a different physical file. Use any...
Using MySQL... Join the Students, Enrollment, and Classes table to display the UniqueCode and the students’...
Using MySQL... Join the Students, Enrollment, and Classes table to display the UniqueCode and the students’ classes (34 rows) Alter the Enrollment table to create column FullName Update the Enrollment table (using a subquery) to fill in the FullName selecting the students from the Student table (34 rows changed) Using Union (WHERE conditions limited to 1 condition each), display the students names whose HeightInches is greater than 65 or less than 72 (13 rows) Using “Intersection”, display the students names...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT