What is Creating and Working with Tables in SQL SERVER PART 5 With Example Code

These statements take the database "DatabaseName" offline, renames it to "NewDatabaseName", and then brings it back online. Ensure you replace "DatabaseName" and "NewDatabaseName" with actual database names.



USE master; -- Ensure you are in the master database
DROP DATABASE Free;
USE master; -- Ensure you are in the master database
ALTER DATABASE DatabaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
-- Take the database offline
ALTER DATABASE DatabaseName SET OFFLINE WITH ROLLBACK IMMEDIATE;
-- Modify the database name
ALTER DATABASE DatabaseName MODIFY NAME = NewDatabaseName;
-- Bring the database back online
ALTER DATABASE NewDatabaseName SET ONLINE;
-- Change table name to 'NewTable', constraint name to 'NewConstraint', and column name to 'NewColumn'
ALTER TABLE NewTable
ADD CONSTRAINT NewConstraint UNIQUE(NewColumn);
-- Change table name to 'YourTableName' and constraint name to 'YourConstraintName'
ALTER TABLE YourTableName
DROP CONSTRAINT YourConstraintName;
 
-- Create the NewGender table
CREATE TABLE NewGender (
    NewId INT PRIMARY KEY IDENTITY(1,1),
    NewGenderName NVARCHAR(50)
);
 
 
-- Create the NewEmployee table
CREATE TABLE NewEmployee (
    NewCId INT PRIMARY KEY IDENTITY(1,1),
    NewFirstName VARCHAR(50),
    NewLastName VARCHAR(50),
    NewEmail VARCHAR(100),
    NewGenderId INT, -- Added NewGenderId column for foreign key reference
    CONSTRAINT FK_NewEmployee_NewGenderId FOREIGN KEY (NewGenderId) REFERENCES NewGender(NewId)
);
 
 
-- Create NewForeignKeyTable and NewPrimaryKeyTable for demonstration purposes
CREATE TABLE NewPrimaryKeyTable (
    NewPrimaryKeyColumn INT PRIMARY KEY
);
 
 
CREATE TABLE NewForeignKeyTable (
    NewForeignKeyColumn INT,
    CONSTRAINT FK_NewForeignKeyTable_NewPrimaryKeyTable FOREIGN KEY (NewForeignKeyColumn) REFERENCES NewPrimaryKeyTable(NewPrimaryKeyColumn)
);
 
 
 
-- Assuming you want to add a foreign key constraint in the table
ALTER TABLE NewEmployee
ADD CONSTRAINT FK_NewEmployee_NewGenderId
FOREIGN KEY (NewGenderId) REFERENCES NewGender(NewId);
 
 
-- Assuming you want to add a foreign key constraint in the NewForeignKeyTable table
ALTER TABLE NewForeignKeyTable
ADD CONSTRAINT FK_NewForeignKeyTable_NewForeignKeyColumn
FOREIGN KEY (NewForeignKeyColumn) REFERENCES NewPrimaryKeyTable(NewPrimaryKeyColumn);