DBMS BY INZA NAEEM Objective Connection File Create Table Query Insert Query Select Query Connection File This php code establishes a connection to MySQL database using the provided server name, user name, password and database name. The ‘mysqli_connect’ function is used for this purpose. <?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "table"; $conn = mysqli_connect($servername, $username, $password, $dbname); ?> Create Query <?php include ('dbconnection.php'); $sql = "CREATE TABLE dept( deptno INT(2), dname VARCHAR(14), loc VARCHAR(13), CONSTRAINT pk_dept PRIMARY KEY (deptno) )"; Create Query $result = mysqli_query($conn,$sql); if ($result) { echo "Table Created Successfully"; } else { echo "Error creating Table: " . mysqli_error($conn); } ?> Create Query Explanation 1. include ('dbconnection.php');`: This line includes an external PHP file (`dbconnection.php`) that presumably contains the database connection code. 2. $sql = "create table dept(...";`: Defines an SQL query to create a table named "dept" with specified columns (deptno, dname, loc) and a primary key constraint on deptno. 3. $result = mysqli_query($conn,$sql);`: Executes the SQL query using the established database connection (`$conn`) and stores the result in the `$result` variable. Create Query Explanation 4. `if(result=true){`: This line contains a typo and should be `if ($result === true) {`. It checks if the query execution was successful. 5. `echo "Table Created Successfully";`: Outputs a success message if the query execution was successful. 6. `else { echo "Error creating Table:" . mysqli_error($conn); }`: Outputs an error message along with the specific MySQL error if the query execution fails. The error message is obtained using `mysqli_error` function. Insert values in table <?php include('dbconnection.php'); // Insert values into the dept table $sql1 = "INSERT INTO dept (deptno, dname, loc) VALUES (10, 'ACCOUNTING', 'NEW YORK')"; $sql2 = "INSERT INTO dept (deptno, dname, loc) VALUES (20, 'RESEARCH', 'DALLAS')"; $sql3 = "INSERT INTO dept (deptno, dname, loc) VALUES (30, 'SALES', 'CHICAGO')"; $sql4 = "INSERT INTO dept (deptno, dname, loc) VALUES (40, 'OPERATIONS', 'BOSTON')"; Insert values in table // Execute each SQL statement $result1 = mysqli_query($conn, $sql1); $result2 = mysqli_query($conn, $sql2); $result3 = mysqli_query($conn, $sql3); $result4 = mysqli_query($conn, $sql4); // Check for successful execution if ($result1 && $result2 && $result3 && $result4) { echo "Values inserted successfully into dept table."; } else { echo "Error inserting values into dept table: " . mysqli_error($conn); } ?> Insert value emp table: "INSERT INTO emp VALUES (7839, 'KING', 'PRESIDENT', null, '" . convertDateFormat('17-11-1981') . "', 5000, null, 10)", "INSERT INTO emp VALUES (7698, 'BLAKE', 'MANAGER', 7839, '" . convertDateFormat('1-5-1981') . "', 2850, null, 30)", "INSERT INTO emp VALUES (7782, 'CLARK', 'MANAGER', 7839, '" . convertDateFormat('9-6-1981') . "', 2450, null, 10)", "INSERT INTO emp VALUES (7566, 'JONES', 'MANAGER', 7839, '" . convertDateFormat('2-4-1981') . "', 2975, null, 20)", "INSERT INTO emp VALUES (7788, 'SCOTT', 'ANALYST', 7566, '" . convertDateFormat('13-JUL-87') . "', 3000, null, 20)", "INSERT INTO emp VALUES (7902, 'FORD', 'ANALYST', 7566, '" . convertDateFormat('3-12-1981') . "', 3000, null, 20)", "INSERT INTO emp VALUES (7369, 'SMITH', 'CLERK', 7902, '" . convertDateFormat('17-12-1980') . "', 800, null, 20)", "INSERT INTO emp VALUES (7499, 'ALLEN', 'SALESMAN', 7698, '" . convertDateFormat('20-2-1981') . "', 1600, 300, 30)", "INSERT INTO emp VALUES (7521, 'WARD', 'SALESMAN', 7698, '" . convertDateFormat('22-2-1981') . "', 1250, 500, 30)", "INSERT INTO emp VALUES (7654, 'MARTIN', 'SALESMAN', 7698, '" . convertDateFormat('28-9-1981') . "', 1250, 1400, 30)", "INSERT INTO emp VALUES (7844, 'TURNER', 'SALESMAN', 7698, '" . convertDateFormat('8-9-1981') . "', 1500, 0, 30)", "INSERT INTO emp VALUES (7876, 'ADAMS', 'CLERK', 7788, '" . convertDateFormat('13-JUL-87') . "', 1100, null, 20)", "INSERT INTO emp VALUES (7900, 'JAMES', 'CLERK', 7698, '" . convertDateFormat('3-12-1981') . "', 950, null, 30)", "INSERT INTO emp VALUES (7934, 'MILLER', 'CLERK', 7782, '" . convertDateFormat('23-1-1982') . "', 1300, null, 10)" Select Query Retrieve all columns for all departments: SELECT * FROM dept; Retrieve departments located in 'NEW YORK’: SELECT * FROM dept WHERE loc = 'NEW YORK'; Retrieve all columns for all employees: SELECT * FROM emp; Retrieve specific columns (empno, ename, job, mgr, hiredate, sal, comm, deptno) for all employees: SELECT empno, deptno FROM emp; Retrieve employees with a salary greater than 3000: SELECT * FROM emp WHERE sal > 3000; Select Query <?php include ('dbconnection.php'); $sql1 = "SELECT * FROM dept"; $result1 = mysqli_query($conn, $sql1); if ($result1) { // Fetch and display results while ($row = mysqli_fetch_assoc($result1)) { print_r($row); // Or display individual columns using $row['column_name'] } Select Query } else { echo "Error executing query: " . mysqli_error($conn); } // Close the database connection mysqli_close($conn); ?> Select Query Explanation if ($result1) {: This line starts a conditional block. It checks if the result of the query ($result1) is truthy, indicating that the query was executed successfully. while ($row = mysqli_fetch_assoc($result1)) {: This line initiates a while loop that fetches each row of the query result as an associative array ($row) using mysqli_fetch_assoc. print_r($row);: This line prints the contents of the $row array, which represents a single row of the query result. It can be used to display all columns of the row. Select Query Explanation // Or display individual columns using $row['column_name']: This comment indicates an alternative way to display individual columns of the row by accessing them using their column names. } else {: This line starts the else block for the conditional statement. If the query execution failed, this block will be executed. echo "Error executing query: " . mysqli_error($conn);: This line prints an error message indicating that there was an error executing the query. It includes the specific error message obtained from mysqli_error($conn). Lab Task 1: Write a MySQL query to create a table named 'emp' with the following columns and constraints: •empno (integer, 4 digits, unsigned) •ename (varchar, maximum length 10) •job (varchar, maximum length 9) •mgr (integer, 4 digits, unsigned) •hiredate (date) •sal (decimal, 7 digits total, 2 decimal places) •comm (decimal, 7 digits total, 2 decimal places) •deptno (integer, 2 digits, unsigned) Include a primary key constraint on the 'empno' column and a foreign key constraint ('fk_deptno') referencing the 'dept' table on the 'deptno' column." Lab Task 2: Write a MySQL query to create a table named 'salgrade' with the following columns: •grade (integer) •losal (integer) •hisal (integer) Ensure the appropriate data types are used for each column. Lab Task 3: Write a PHP script to insert the following values into the 'salgrade' table: Grade 1: Minimum Salary 700, Maximum Salary 1200 Grade 2: Minimum Salary 1201, Maximum Salary 1400 Grade 3: Minimum Salary 1401, Maximum Salary 2000 Grade 4: Minimum Salary 2001, Maximum Salary 3000 Grade 5: Minimum Salary 3001, Maximum Salary 9999 Lab Task 4: Execute each of the SELECT queries provided on slide 12.