Using SQL Server through Command Prompt Lab 3 Database Engineering
Connecting to SQL Server Command: Sqlcmd –S Computername\instanceName.
Create New Login Create login user_name with password = ‘password'; Use GO statement to execute the query
Server level roles
Grant privileges To add server role: EXEC sp_addsrvrolemember ‘user_name', ‘server_role';
Create User for specific database USE database_name CREATE USER [User_name] FOR LOGIN [login_name]
Database Level Role
Grant privileges to the user EXEC sp_addrolemember, ‘database_role’ ‘user_name’
Creating database Command: Create database database_name
Opening specific database Command: Sqlcmd –S Computername\instanceName -d database- Name
Create table Command: create table table_name (column_name datatype, column_name datatype) Table with primary key
Supported data types in SQL server
Composite primary key 1> CREATE TABLE Customer ( 2> FirstName VARCHAR(15), 3> LastName VARCHAR (15), 4> CONSTRAINT pkey PRIMARY KEY (FirstName, LastName)
Index An index is a copy of selected columns of data from a table that can be searched very efficiently that also includes a low- level disk block address or direct link to the complete row of data it was copied from
Create index Create index index_name ON table_name (column-name)
Insert values into the table Insert into table_name values (value1, value2)
Select statement Select * from Table_Name
Using input file Sqlcmd –S Computername\instanceName – d database-name -i inputfile_path
Using input and output files Sqlcmd –S Computername\instanceName – d database-name -i input_filepath – o output_filepath
Directly run Query Note: using capital Q will run the query and exit SQLCMD
EXIT SQLCMD To end the sqlcmd session, type EXIT at the sqlcmd prompt.
Exercise Let's assume you are designing a flight reservation system. At its simplest, the application requires database tables for the flights, the customers, and the reservations. 1) Create the database and required tables using SQLCMD. Note: All tables should have primary key. Use composite primary key for customer table
Exercise 2) Create login for the server and give privileges of system administrator to it. 3) Create two users account for your database. Give the privileges of Database owner and read only to them. 4) Login with the user having privilege of database owner 5) Insert at least five records in each table 6) Create index for the flight and reservation table’s column .
Exercise 7) Show all tables data in command prompt 8) Write the select query for all created tables in script file and execute it through SQLCMD 9) Save the output of the select queries in output files using SQLCMD 10) Run direct query for selecting columns of tables in SQLCMD.