0% found this document useful (0 votes)
4 views27 pages

HaKieuOanh_LAB2

Uploaded by

longa3729
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views27 pages

HaKieuOanh_LAB2

Uploaded by

longa3729
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

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 17
Download and install SSMS
Step 19
Try to Connect to SQL Server 2022

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.
Create a new Table.
Name for Table
6. Press Refresh to check the table you just create
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
Right-click Teacher1 and select Properties. The screen below will appear

Choose User
Mapping and select Database for User. Then select the authority you want to grant that user in the last
box

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
Sign out and try to log in again with each
account.

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