-
Notifications
You must be signed in to change notification settings - Fork 8.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Script Change for instaWDB in Drop Database Section #1311
base: master
Are you sure you want to change the base?
Conversation
@Imran-imtiaz48 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the revised code is more robust and user-friendly. It handles existing connections more gracefully, provides clear feedback messages, and uses dynamic SQL for better flexibility. These improvements make the script more reliable and easier to maintain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix the SQL Injection issues.
BEGIN | ||
-- Close existing connections to the database | ||
DECLARE @SQL NVARCHAR(MAX) = N''; | ||
SELECT @SQL += 'ALTER DATABASE [' + @DBName + '] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modify to use QUOTENAME(@dbname). Current string concatenation technique is susceptible to SQL injection attacks.
EXEC sp_executesql @SQL; | ||
|
||
-- Drop the database | ||
SET @SQL = N'DROP DATABASE [' + @DBName + '];'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here. Get the quoted identifier first & then concatenate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SQL script effectively creates the authors
, publishers
, and titles
tables with necessary constraints and defaults. However, there are areas for improvement to enhance readability and maintainability. Consider using a consistent naming convention for constraints and indexes. Simplify the CHECK constraints for au_id
and zip
using regex patterns. Ensure all default values are meaningful, especially for the phone
and type
columns. Additionally, aligning the data types and constraints formatting improves clarity. Here's an example of a refined version:
CREATE TABLE authors (
au_id CHAR(11) CHECK (au_id LIKE '[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9]') CONSTRAINT PK_authors PRIMARY KEY,
au_lname VARCHAR(40) NOT NULL,
au_fname VARCHAR(20) NOT NULL,
phone CHAR(12) NOT NULL DEFAULT 'UNKNOWN',
address VARCHAR(40) NULL,
city VARCHAR(20) NULL,
state CHAR(2) NULL,
zip CHAR(5) CHECK (zip LIKE '[0-9][0-9][0-9][0-9][0-9]'),
contract BIT NOT NULL
);
CREATE TABLE publishers (
pub_id CHAR(4) NOT NULL CONSTRAINT PK_publishers PRIMARY KEY,
pub_name VARCHAR(40) NULL,
city VARCHAR(20) NULL,
state CHAR(2) NULL,
country VARCHAR(30) NULL DEFAULT 'USA',
CHECK (pub_id IN ('1389', '0736', '0877', '1622', '1756') OR pub_id LIKE '99[0-9][0-9]')
);
CREATE TABLE titles (
title_id CHAR(6) CONSTRAINT PK_titles PRIMARY KEY,
title VARCHAR(80) NOT NULL,
type CHAR(12) NOT NULL DEFAULT 'UNDECIDED',
pub_id CHAR(4) NULL REFERENCES publishers(pub_id),
price MONEY NULL,
advance MONEY NULL,
royalty INT NULL,
ytd_sales INT NULL,
notes VARCHAR(200) NULL,
pubdate DATETIME NOT NULL DEFAULT (GETDATE())
);
By implementing these changes, the script will become more consistent and easier to maintain.
Declare Variable: The script starts by declaring a variable @dbname to hold the database name.
Check for Existence: It checks if the database exists.
Close Existing Connections: If the database exists, it closes all existing connections by setting the database to SINGLE_USER mode with immediate rollback of active transactions.
Drop the Database: It then drops the database.
Error Handling: After attempting to drop the database, it checks again if the database still exists. If it does, it raises an error indicating there are still open connections.