Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

SQL Server DATABASE INSTALLATION AND ADMINISTRATION

I. Downloading SQLServer.
The official download site for the latest SQLServer is:
https://www.microsoft.com/en-us/sql-server/sql-server-downloads
II. Installing and configuring.
Installing and configuring on Windows:
Step 1: Run Installed Application
Navigate to the directory/folder where you downloaded the application then
double-click it to start the installation process.

Step 2: Select Custom Installation


After you have chosen the custom installation, click on “Install“. Allow the
application to download and install packages. I recommend you choose
“Custom” to tweak your installation of SQL Server 2019 Developer Edition on
Windows Server 2019.
Specify a directory where SQL Server will store files downloaded for
installation.

Be patient as the download begins, it may take some time.


Step 3: Choose Installation
After all of the packages have downloaded, a new page as illustrated below
will follow. Click on “Installation
Step 4: Choose New SQL Server standalone
After Step 3, choose the first option in the list and proceed to the next step.
This is illustrated below.

Step 5: Specify Edition


This step depends on whether you need “Developer“, “Evaluation” or
“Express“. Their differences are described in the window. Since we are
installing the “Developer” edition, let us proceed with that option. Click “Next
Step 6: Accept License Terms
Select the “I accept the license terms and Privacy Statement” Radio and click
on “Next“.

Step 7: Microsoft Update


You can either select the “Microsoft update” radio or not in this step then click
“Next“
Step 8: Install Rules
Click “Next” after you are satisfied with the results of that page.
Step 9: Feature Selection
Go ahead and choose what you prefer to have. Click “Next” after you are done.

Step 10: Instance Configuration


Name your instance, in this step and click on “Next“.
Step 11: Server Configuration
Click “Next” after that.

Step 12: Configure Database Engine Parameters


I prefer mixed-mode authentication where a user can either be from a domain
or added manually in the SQL instance.
Click on “Next” when you are done. Do not forget to add an admin user.
Step 13: Analysis Services Configuration
Click on “Next“.

Step 14: Accept Installation of Services


Step 15: Ready to Install
After you are satisfied with the summary, then gladly click on “Install“
Step 16: Finish up
After everything has installed. Click on “Close“. Your SQL instance is
installed and ready to be used.

Step 17
Download and install SSMS Release 18
Version 18.3 or later

Step 18
It gives below message to restart the system to complete setup
Step 19
Try to Connect to SQL Server 2019
Advantages & Disadvantages of Microsoft SQL.
a. Advantages
Common: Microsoft is supported a lot because the Windows operating system is widely
used in enterprise systems
Interface support: User friendly, MS SQL Server comes with an impressive set of tools.
Components like SQL Server Profiler, SQL Server Management Studio, BI tools, and
Database Tuning Advisor can save you a lot of troubleshooting, development, and
administration time. In addition, as a core product in Microsoft's technology arsenal, MS
SQL Server has a wealth of documentation and support.
Separate security privileges.
Maintain backup servers. Easily backup and prepare for problems.
Excellent data recovery support: Corrupted data is always a concern when power outages
or improper shutdowns occur. Microsoft SQL Server has a number of features that
promote data recovery and recovery. Although individual tables cannot be backed up or
restored, complete database recovery options are available. Through the use of log files,
caching, and backups, Microsoft products allow you to feel confident that disaster
recovery options are rich.
b. Disadvantage
Request machine configuration when installing.
Limited Compatibility: Microsoft SQL Server runs only on Windows operating systems.
Cost: Microsoft SQL Server needs to pay a license fee to run multiple databases. The
SQL Server Datacenter edition is $54,990 per processor.
SQL SERVER 2019 ADMINTRATOR
Using query to modify user
Create user to login
Step1: Open new query page with File > New > Query with current connection ( or press
ctrl + N)

Step2: To create new Login User, use this query:


CREATE LOGIN Teacher1 WITH PASSWORD = '123'
CREATE LOGIN Teacher2 WITH PASSWORD = '123'
CREATE LOGIN Teacher3 WITH PASSWORD = '123'
Step3: Highlight words and press F5 to run the script
*Note: Make sure to check if LOGIN USER is already exists or not before create new
login:
IF EXISTS (SELECT Name FROM sys.server_principals sp where name='Teacher1')
BEGIN
DROP LOGIN Teacher1
END
IF EXISTS (SELECT Name FROM sys.server_principals sp where name='Teacher2')
BEGIN
DROP LOGIN Teacher2
END
IF EXISTS (SELECT Name FROM sys.server_principals sp where name='Teacher3')
BEGIN
DROP LOGIN Teacher3
END
Then re-create login
Step4:

Use these query to create new database:


IF EXISTS (Select Name from sys.databases where name='University')
BEGIN
DROP DATABASE [University]
END
CREATE DATABASE [University]
Step 5:
SQL Server use user to grant priviledges, use these query to add new user for assign
priviledges later:

Use [University]
IF EXISTS (SELECT Name FROM sysusers WHERE NAME='Priv1')
BEGIN
DROP USER Priv1
END
CREATE USER [Priv1] for LOGIN Teacher1

IF EXISTS (SELECT Name FROM sysusers WHERE NAME='Priv2')


BEGIN
DROP USER Priv2
END
CREATE USER [Priv2] for LOGIN Teacher2
IF EXISTS (SELECT Name FROM sysusers WHERE NAME='Priv3')
BEGIN
DROP USER Priv3
END
CREATE USER [Priv3] for LOGIN Teacher3
Step 6:
To create new table, use these query:

IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES


WHERE TABLE_NAME='Class1')
BEGIN
DROP TABLE Class1
END
CREATE TABLE Class1(
classID nvarchar(20),
classRoom int
);

IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES


WHERE TABLE_NAME='Class2')
BEGIN
DROP TABLE Class2
END
CREATE TABLE Class2(
classID nvarchar(20),
classRoom int
);

Step7:
Insert new row in table:
INSERT INTO Class1(classID, classRoom)
VALUES ('IA14A', '115')
INSERT INTO Class2(classID, classRoom)
VALUES ('IA14B', '117')
Step 8:
Update on SQL Server with existing value:

IF EXISTS (SELECT classID, classRoom FROM class1)


BEGIN
UPDATE Class1
SET classID = 'IA14A', classRoom = '115' WHERE classID = 'IA14A'
END
Step 9:
Grant permission to access table for each user in SQL SERVER
GRANT SELECT, UPDATE, DELETE, INSERT on class1 to Priv1
GRANT SELECT, UPDATE, DELETE, INSERT on class1 to Priv2
GRANT SELECT, UPDATE, DELETE, INSERT on class2 to Priv2
GRANT SELECT, UPDATE, DELETE, INSERT on class2 to Priv3
*Note: Use DENY to remove permission on user:
DENY INSERT on priv1
To connect to SQL with cmd, use this command:
sqlcmd -S <servername> -U <user> -p
type password to continue:
to query with cmd
we must use go in the last line to execute command:
Using CRUD to modify user

User

Teacher 1 Teacher 2 Teacher3

SCHOOL

Class 1 Class 2
1. Login to SQL Server Management Studio

2. Create a new one databse


3. Name for database.
4. Create a new Table.
5. Name for Table

6. Press Refresh to check the table you just create


 Kết quả
II. Insert, Delete, Update Table trong SQL Server
1. Thêm dữ liệu vào bảng vừa tạo

2. Xóa dữ liệu trong bảng


3. Update Infomation
III. Decentralization in sql server
1. Create 3 Users : Teacher 1, Teacher 2, Teacher 3

 Result
2. Right-click Teacher1 and select Properties. The screen below will appear
3. Choose User Mapping and select Database for User. Then select the authority
you want to grant that user in the last box.
4. Do the same for Teacher2 and Teacher3.
IV. Decentralization in Table
1. Decentralized for table. Select the table you want to assign and click

properties
2. The dialog box pops up as follows. Click on search.

3. Check the user you want to grant permission and click ok to appy.
4. Grant the FRS401 read-only permission to teacher1 and block the right to view
information with teacher3.
5. Grant control for Teacher2
6. Sign out and try to log in again with each account.
7. Result
Code Python To Connect
import pyodbc

conx_string = 'DRIVER={SQL Server}; SERVER=DESKTOP-MKA0B4K\SQLEXPRESS;


Database=University; UID=sa; pwd=1;'

with pyodbc.connect(conx_string) as conx:


cursor = conx.cursor()
cursor.execute("""
IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME='Class1')
BEGIN
DROP TABLE Class1
END
CREATE TABLE Class1(
classID nvarchar(20),
classRoom int
);
""")

with pyodbc.connect(conx_string) as conx:


cursor = conx.cursor()
cursor.execute('INSERT INTO Class1(classID, classRoom) VALUES (?,
?)', 'IA14A', 113)
cursor.execute('INSERT INTO Class1(classID, classRoom) VALUES (?,
?)', 'IA14B', 115)

with pyodbc.connect(conx_string) as conx:


cursor = conx.cursor()
cursor.execute('SELECT classID, classRoom FROM class1')
data = cursor.fetchall()

print ("VALUE BEFORE: ", data)

with pyodbc.connect(conx_string) as conx:


cursor = conx.cursor()
cursor.execute("SELECT classID, classRoom FROM class1 WHERE
ClassID = 'IA14A'")
data = cursor.fetchall()
if data:
cursor.execute("UPDATE Class1 SET classID = ?, classRoom = ?
WHERE classID = 'IA14A'", 'IA14A', 116)

with pyodbc.connect(conx_string) as conx:


cursor = conx.cursor()
cursor.execute('SELECT classID, classRoom FROM class1')
data = cursor.fetchall()

print ("VALUE AFTER UPDATE: ", data)

You might also like