SQL: DDL Create and Alter, DCL Grant and Revoke, Data Manipulation, Database Testing
Verified
Added on 2023/04/25
|13
|2239
|63
AI Summary
This article covers SQL concepts such as DDL create and alter, DCL grant and revoke, data manipulation, and database testing. It includes SQL statements and examples for each concept.
Contribute Materials
Your contribution can guide someone’s learning journey. Share your
documents today.
COVER PAGE
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
Task 1: Structure Query Language (SQL) 1.Describe DDL create and alter CREATE Create statement is a data definition language statement that is used to create a new table in a database. The correct syntax for create table statement is shown below; CREATE TABLE name_of_table( Column1 datatype, Column2 datatype, …. ); An example of create table statement can be demonstrated by creating a customers table in the example below; CREATE TABLE customers ( Customerid integerprimarykey, Firstname varchar(50)notnull, Lastname varchar(50)nonull. Email varchar(50)notnullunique, Address varchar(50)null ); When executed, the query above will create a customers table with customerid column as the primary key. This mean that the customer id should be unique for every customer record. The email is also supposed to be unique for every customer. ALTER The alter statement is data definition language that is used to add, delete and modify columns and constraints in a table. The statement is used for an existing table. The correct syntax for altering a table is; ALTER TABLE name_of_table ADD column_name datatype; For example considering the customers table created in the sub-section above, a mobile number column can be added using the alter command ash shown below; ALTER TABLE customers ADD mobile_number varchar(25)notnull; The alter statement can also be used to drop a column and the syntax for dropping a column is shown below;
ALTER TABLE name_of_table DROP COLUMN name_of_column; An example of dropping a column is shown below; ALTER TABLE customers DROP COLUMN mobile_number; The Alter statement can also be used to modify a column as shown in the example below; ALTER TABLE customers ALTER COLUMN mobile_numbervarchar(25)null; When the statement above is executed, it alters the mobile_number from not null to null. For constraints, the ALTER statement can be used to add, modify or drop constraints as shown in the examples below; ALTER TABLE customers ADD CONSTRAINT customers_fk(customerid)referencesperson (personID); The statement above adds a foreign key to the customers table assuming there exists a person table with a personID column which is the primary key. 2.Describe DCL Grant and Revoke GRANT Grant is a data control language statement that is used to grant privileges to users and roles for database objects including tables or views. For example the database administrator can grant update privilege to a user db_user1 for the customers table using the code below; GRANT UPDATE ON customersTOdb_user1; The statement above gives update privilege to db_user1 on the customers table. REVOKE Revoke is a data control language statement that is used to revoke privileges or rights from users or roles. For example, the database administrator can revoke update privilege from db_user1 on the customers table using the code below; REVOKE UPDATE ON customersFROMdb_user1;
When executed, the statement above revokes the update right from db_user1 on table customers. Task 2: Data Manipulation 1.Create a SQL statement to list all managers and their titles. selectconcat (first_name,' ',last_name),titlefromdepartmentsinner joindept_managerondept_manager.dept_no=departments.dept_noinnerjoin employeesonemployees.emp_no=dept_manager.emp_noinnerjointitleson titles.emp_no=employees.emp_no; 2.Create a SQL statement to show the salary of all employees and their department name. selectconcat(first_name,' ',last_name),salary,dept_namefromemployees innerjoindept_empondept_emp.emp_no=employees.emp_noinnerjoin salariesonsalaries.emp_no=employees.emp_noinnerjoindepartmentson dept_emp.dept_no=departments.dept_nogroupbyemployees.emp_no; 3.Create a SQL statement to show the hire date and birth date who belongs to HR department selectconcat(first_name,' ',last_name),hire_date, birth_Datefrom employeesinnerjoindept_empondept_emp.emp_no=employees.emp_noinner joindepartmentsondept_emp.dept_no=departments.dept_nowhere departments.dept_name='Human Resources';
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
4.Create a SQL statement to show the all departments and their department’s managers. selectdept_name, concat(first_name,' ',last_name) from departments inner join dept_manager on dept_manager.dept_no=departments.dept_no inner join employees on employees.emp_no=dept_manager.emp_no group by dept_name order by to_date desc; 5.Create a SQL statement to show a list of HR’s employees who were hired after 1986 selectconcat(first_name,' ',last_name),hire_datefromemployeesinner joindept_empondept_emp.emp_no=employees.emp_noinnerjoindepartments ondept_emp.dept_no=departments.dept_nowhere departments.dept_name='Human Resources'andyear(hire_date)>=1986; 6.Create a SQL statement to increase any employee’s salary up to 2%.Assume the employee has just phoned in with his/her last name. Assuming the employee with the last name Maliniak called; Before update Update statement
updateemployees,salariessetsalary=salary+salary*0.02where employees.emp_no=salaries.emp_noandlast_name='Maliniak'; After Update 7.Createa SQLstatementtodeleteemployee’srecordwhobelongstomarketing department andname start with A deletefromemployeeswhereemp_noin(selectemployees.emp_nofrom departments,dept_empwheredept_emp.dept_no=departments.dept_noand departments.dept_name='Marketing')andleft(last_name,1)='A'; 8.Create a databaseviewto list the full names of all departments’ managers, and their salaries. createviewemployee_detailsasselectconcat(first_name,' ',last_name),dept_name,salaryfromemployeesinnerjoindept_empon dept_emp.emp_no=employees.emp_noinnerjoindepartmentson departments.dept_no=dept_emp.dept_noinnerjoinsalarieson salaries.emp_no=employees.emp_no;
Getting data from the view;
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
9.Create a databaseviewto list all departments and their department’s managers, who were hired between 1980 and 1990. createviewdepartment_managersasselect dept_name,concat(first_name,'',last_name),hire_datefrom departmentsinnerjoindept_manageron dept_manager.dept_no=departments.dept_noinnerjoinemployeeson employees.emp_no=dept_manager.emp_nowherehire_datebetween '1980-01-01'and'1990-12-31'; 10.Create a SQL statement to increase salaries of all department’s managers up to 10% who are working since 1990. updatesalariessetsalary=salary+(salary*0.01)whereemp_noin(select emp_nofromdept_managerwhereyear(from_date)>=1990);
Task 3: Database Testing 1.Describe database testing and why its important Database testing is the process of testing different aspects and components of the database to make sure they work the way they are supposed to. Testing in a database is done in levels or layers. The layers involved in testing a database are; User interface layer- testing at this level is done to make sure that the database will work correctly with the end application designed to use the database. Business layer- Testing at this level is done to make sure the database meets all the business requirements of the business on which the database is based on. Data access layer – Testing at this level is done to make sure the database is accessible and the data stored in the database is useful Database layer- Testing at this level is done on the database itself and includes testing ACID properties and data integrity in the database. Database testing is very important as it helps ensure that the forms used in the front end part of the application can be correctly mapped to the respective database fields thus ensuring data mapping is successful. Database testing is also done to validate the ACID properties of the database. Testing ACID properties involves testing for; Atomicity- Atomicity specifies that database transactions should either pass or fail such that if a transaction passes then changes should reflect in the database but if the transaction then no changes should reflect in the database. A transaction should either pass or fail and there is nothing else(Sharma et al., 2019). Consistency- Consistency is done to make sure a database transaction leaves the database in a consistent state. Isolation- Isolation ensures that database transactions execute independently and concurrently while at the same time maintaining database consistency. This means that concurrent execution of many transactions should happen as if the transactions are executing in order. Durability- Durability testing ensures that a database transaction that is committed leads to permanent changes in the database which are written on disk. However if a transaction is rolled back, no changes should be done on the database(Rouse, 2006). 2.Testing process with screenshots Field size validation Field size validation involves testing that the size specified for a column in the database is not violated. For example if a column is only supposed to take only 5 characters, if an insert or update transaction is performed on the table, then the database should automatically trim the data to 5 characters or throw an error depending on the configuration parameters.
For example, field size validation can be tested by inserting data that exceeds the specified length for dept_no column in the departments table. According to the structure of the departments table, the dept_no is supposed to take 4 characters as shown below; Thus an insert with more tha 4 characters should fail or trim the characters to 4 characters; insert into departments(dept_no,dept_name)values ('d123456789','My department'); Executing the query above results to a trimmed value. Null values Testing for null values means that a column with null attribute can take or not take a value. Columns that are defined as not null cannot take a null value. To test the null and not null values in a database, a null value can be inserted into a field defined as not null and the database should throw an error as shown below; Considering the insert query below which is supposed to insert a record in the department table; insert into departments(dept_no,dept_name)values('d923',null); The query is executed successfully but throws an error because a null value cannot be inserted into a column defined as not null.
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Check constraint A check constraint is enforced to ensure that the value inserted or updated in a column meets the condition of the check constraint. For example in the employees table, the gender is supposed to be either M for male or F for Female. Violating this should throw an error. Thus adding a check constraint in employees table can be done using the code below; alter table employees addconstraintcheck(genderin('F','M')); Inserting a value that is not F or M in the gender column should result to an error or insert a null value depending on the server configuration; insert into employees (emp_no,birth_Date,first_name,last_name, gender,hire_date) values(1000244,'1990-01- 02','John','Doe','K','2008-09-09'); This results to a null value in the gender column because the value inserted was not F or M. Referential Integrity
Testing referential integrity involves testing that relationships between primary keys and foreign keys always maintain referential integrity(Poolet, 2009). This means that when inserting or updating a foreign key, there must exist a foreign key with that value in the parent table. For example trying to insert a record in the child table for a primary key that does not exist should result to an error as shown below; insertintotitles (emp_no,title,from_date,to_date)values (1000043,'Lead developer','2012-12-12','2018-12-12'); When the query above is executed, it results to an error because the emp_no supplied does not exist in the parent thus violates referential integrity; References Poolet, M. (2009).SQL By Design: The Four Integrities. [online] IT Pro Today. Available at: https://www.itprotoday.com/sql-server/sql-design-four-integrities [Accessed 13 Mar. 2019].
Rouse, M. (2006).What is ACID (atomicity, consistency, isolation, and durability)? - Definition from WhatIs.com. [online] SearchSQLServer. Available at: https://searchsqlserver.techtarget.com/definition/ACID [Accessed 13 Mar. 2019]. Sharma, R., Mohamed, A., Aryan, M., kuchana, r. and Ramesh, M. (2019).Database Testing Complete Guide (Why, What, and How to Test Data). [online] Softwaretestinghelp.com. Available at: https://www.softwaretestinghelp.com/database-testing-process/ [Accessed 13 Mar. 2019].