HaKieuOanh_LAB2
HaKieuOanh_LAB2
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
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
User
Teacher 1 Teacher 2 Teacher3
SCHOOL
Class 1 Class 2
Choose User
Mapping and select Database for User. Then select the authority you want to grant that user in the last
box
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()