[go: up one dir, main page]

Skip to content

arman324/Database-Lab

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Database Lab

First of all, I created a database and then created and filled tables. After that I answered 3 questions:

  1. For a student who has a student number 123, show the details of the department where he or she is studying.
  1. Write a 'select' that adds a score to each student's grade in each lesson.
  1. Write a query to display students who have not taken the DB course.

In this lab, I used the AdventureWorks2012 database and answered 5 questions:

  1. For the AdventureWorks2012, write a query that returns the general information of the orders being processed, which are between 100000 and 500000, and the place of order is from France or one of the countries in the North American region.
  1. For the AdventureWorks2012, write a query that shows the sales order identification number, customer identification number, order amount(TotalDue), order date, and the name of the region where the order was registered, for each order.
  1. For the AdventureWorks2012, write a query that shows which area had the most orders for each product.
  1. For the AdventureWorks2012, create a table called NAmerica_Sales, which is in accordance with question number 1. Add a column to this table and add a constraint to have only Low, High, or Mid values. Then update this column for each tuple to 'High' when the order cost(TotalDue) is higher than the average total cost (The cost of all orders) on the North American region. Update the column to 'Mid' if the order cost(TotalDue) is equal to the average total cost on the North American region. Finally, update the column to 'Low' when the order cost(TotalDue) is lower than the average total cost on the North American region.
  1. increase all employee salaries for each hour of work by 20%, 15%, 10% or 5%. Then give them a level in accordance with their salaries.

In this lab, I worked with "User" and "Login" in the SQL Server.

  • SQL "Login" is for Authentication:
    • Authentication can decide if we have permission to access the SERVER or not.
    • A "Login" grants the principal entry into the SERVER.
  • SQL server "User" is for Authorization:
    • Authorization decides what are different operations we can do in a DATABASE.
    • A "User" grants a login entry into a single DATABASE.

I answered these questions:

  1. First, create a "Login" in the SQL Server.
  • You can see my answer on line number 1 in the sa_admin.sql file.
  1. Create a role that has the ability to manage the database.
  • You can see my answer on line number 5 in the sa_admin.sql file.
  1. Connect the role to the "Login" created in the first question.
  1. With the "login" created in Question 1, log in to the SQL server and then create a table in the AdventureWorks2012 database, Insert data in this table, and select data from it.
  1. In the AdventureWorks2012 database, create a role called it role2 that has no access to the tables but can only manage permissions.
  1. Grant DataReader permission to role 2.

In this lab, I used the AdventureWorks2012 database and answered 2 questions:

  1. For the AdventureWorks2012, write a query that has the number of orders and the total value of the orders for each Territory along with its Territory area. Show the total number and value of orders for all Territories in one area and at the end of the report for all areas.
  • The output should be the same as the following image:

Screen Shot 2020-05-05 at 3 50 43 PM

  1. For the AdventureWorks2012, write a query that shows the number of orders and the total value of orders for each subcategory of goods along with its category. Show the number and value of orders for all subcategories in one category and at the end of the report for all categories.
  • The output should be the same as the following image:

Screen Shot 2020-05-05 at 4 03 37 PM

In this lab, I used the AdventureWorks2012 database and answered 5 questions:

  1. For the AdventureWorks2012, write a query that shows all the products' names along with the number of sales of each item (Order Qty) in each region as a separate column for each product.
  1. For the AdventureWorks2012, modify the following code, to have the output as shown below:
    select Person.BusinessEntityID, PersonType, Gender
    from Person.Person 
        join HumanResources.Employee
         on (Person.BusinessEntityID = Employee.BusinessEntityID)

Screen Shot 2020-05-18 at 7 33 39 PM

  1. For the AdventureWorks2012, write a query that returns all the products’ names which names are less than 11 characters long and which the 2 left character of name to the end is 'e'. (Like Freewheel)
  1. For the AdventureWorks2012, write a function that gives an 11 character word as input, and if the form of the input is the same as YYYY/MM/DD (Like 2019/09/17) from, it returns 'September 17 2019' otherwise it returns 'Wrong format input'
  1. For the AdventureWorks2012, write a function that gives three inputs for the year, month, and the products’ names, and returns territories that have sold the product at least once on the desired date.

In this lab, I used the AdventureWorks2012 database and answered 3 questions:

  1. Write a trigger for AdventureWorks2012 to save all non-structural changes (update, delete, and insert) of products in the ProductLogs table. This table has an additional field that indicates the type of change (like update, insert, delete)
  1. Make a copy of the ProductLogs table and change some records.
  1. Write a procedure that compares Table of question 1 and Table of question 2 and inserts records from Table 1 that are different from table 2 into a new table.

In this lab, I used the AdventureWorks2012 database and answered 4 questions:

  1. Using the bcp command, first, make output in txt format from the SalesTerritory table so that the column separator is "l". Then use the Bulk Insert command to enter this file into a table called SalesTerritoryNew.
  1. Using the xp_cmdshell or bcp command, enter a list of names and IDs from the SalesTerritory table into a text file.
  1. With the bcp command, enter the Production.Location table information into a file called location.dat.
  1. Write a query for the Sales.Store table that displays the names of AdventureWorks's product stores along with 3 fields of annual sales, store opening year, and store staff. Save the result to a text file. (except the store name, other fields should be displayed in XML.)

In this lab, I used the AdventureWorks2012 database and answered 2 questions:

  1. For the AdventureWorks2012, write a query that shows exclusive locks are not compatible with the shared locks.
  • First, you need to run the first script (exclusive lock), after that need to run the second script (shared lock). As you notice that the second script doesn't run therefore these two scripts are not compatible.
  1. part 1: For the AdventureWorks2012, design a scenario for showing the Dirty Read problem.
  1. part 2: For the AdventureWorks2012, design a scenario for showing the Non Repeatable Read problem.

Requirements

Support

Reach out to me at riasiarman@yahoo.com

About

My database lab codes - Using SQL Server

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published