├── Components of DBMS.ppt ├── DIAGRAM PIC ├── P4.png ├── all.png ├── begin.png ├── book_table.png ├── course_table.png ├── database table.png ├── dept_table.png ├── end.png ├── error.png ├── exist.png ├── installation.png ├── join.png ├── natural_join.png ├── natural_join_with_conditon.png ├── p2.png ├── p3.png ├── pic1.png ├── relation_table.png ├── select.png ├── show_table_name.png ├── some.png ├── sqlplus.png ├── sqlplus2.png ├── subquery.png ├── subquery2.png ├── system privileges.png └── table_test.png ├── LICENSE ├── Procedure & function.pptx ├── README.md ├── Transaction Management.ppt ├── ch3.pptx ├── ch4.pptx └── complex datatypes.pptx /Components of DBMS.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/Components of DBMS.ppt -------------------------------------------------------------------------------- /DIAGRAM PIC/P4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/P4.png -------------------------------------------------------------------------------- /DIAGRAM PIC/all.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/all.png -------------------------------------------------------------------------------- /DIAGRAM PIC/begin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/begin.png -------------------------------------------------------------------------------- /DIAGRAM PIC/book_table.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/book_table.png -------------------------------------------------------------------------------- /DIAGRAM PIC/course_table.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/course_table.png -------------------------------------------------------------------------------- /DIAGRAM PIC/database table.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/database table.png -------------------------------------------------------------------------------- /DIAGRAM PIC/dept_table.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/dept_table.png -------------------------------------------------------------------------------- /DIAGRAM PIC/end.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/end.png -------------------------------------------------------------------------------- /DIAGRAM PIC/error.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/error.png -------------------------------------------------------------------------------- /DIAGRAM PIC/exist.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/exist.png -------------------------------------------------------------------------------- /DIAGRAM PIC/installation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/installation.png -------------------------------------------------------------------------------- /DIAGRAM PIC/join.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/join.png -------------------------------------------------------------------------------- /DIAGRAM PIC/natural_join.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/natural_join.png -------------------------------------------------------------------------------- /DIAGRAM PIC/natural_join_with_conditon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/natural_join_with_conditon.png -------------------------------------------------------------------------------- /DIAGRAM PIC/p2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/p2.png -------------------------------------------------------------------------------- /DIAGRAM PIC/p3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/p3.png -------------------------------------------------------------------------------- /DIAGRAM PIC/pic1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/pic1.png -------------------------------------------------------------------------------- /DIAGRAM PIC/relation_table.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/relation_table.png -------------------------------------------------------------------------------- /DIAGRAM PIC/select.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/select.png -------------------------------------------------------------------------------- /DIAGRAM PIC/show_table_name.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/show_table_name.png -------------------------------------------------------------------------------- /DIAGRAM PIC/some.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/some.png -------------------------------------------------------------------------------- /DIAGRAM PIC/sqlplus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/sqlplus.png -------------------------------------------------------------------------------- /DIAGRAM PIC/sqlplus2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/sqlplus2.png -------------------------------------------------------------------------------- /DIAGRAM PIC/subquery.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/subquery.png -------------------------------------------------------------------------------- /DIAGRAM PIC/subquery2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/subquery2.png -------------------------------------------------------------------------------- /DIAGRAM PIC/system privileges.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/system privileges.png -------------------------------------------------------------------------------- /DIAGRAM PIC/table_test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/DIAGRAM PIC/table_test.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Shahidul Shakib 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Procedure & function.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/Procedure & function.pptx -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Contents 2 | | #Lab | Topics name | 3 | | ------------- | ------------- | 4 | | 1 | ✔[Lab 1 - Installation, creating user and giving system privileges](https://github.com/shahidul034/Database-Lab-Tutorial/blob/main/README.md#lab-1) | 5 | | 2 | ✔[Lab 2 - DDL](https://github.com/shahidul034/Database-Lab-Tutorial/blob/main/README.md#lab-2) | 6 | | 3 | ✔[Lab 3 - DML](https://github.com/shahidul034/Database-Lab-Tutorial/blob/main/README.md#lab-3) | 7 | | 4 | ✔[Lab 4 - Aggregate function, group by, nested subquery, set](https://github.com/shahidul034/Database-Lab-Tutorial/blob/main/README.md#lab-4)| 8 | | 5 | ✔[Lab 5 - String operations](https://github.com/shahidul034/Database-Lab-Tutorial/blob/main/README.md#lab-5) | 9 | | 6 | ✔[Lab 6 - Join, view and referential integrity](https://github.com/shahidul034/Database-Lab-Tutorial/blob/main/README.md#lab-6) | 10 | | 7 | ✔[Lab 7 - PL/SQL (Declaration, insert, set value, rowtype and cursor)](https://github.com/shahidul034/Database-Lab-Tutorial/blob/main/README.md#lab-7)| 11 | | 8 | ✔[Lab 8 - PL/SQL (Loop, array and IF-ELSE)](https://github.com/shahidul034/Database-Lab-Tutorial/blob/main/README.md#lab-8) | 12 | | 9 | ✔[Lab 9 - Procedure and function](https://github.com/shahidul034/Database-Lab-Tutorial/blob/main/README.md#lab-9) | 13 | | 10 | ✔[Lab 10 - Trigger](https://github.com/shahidul034/Database-Lab-Tutorial/blob/main/README.md#lab-10) | 14 | 15 | ## 🎯Lab 1 16 | ### 🚀Installation process 17 | > Download the ([Oracle Database 21c Express Edition](https://www.oracle.com/database/technologies/xe-downloads.html)) or ([Oracle Database 11g Express Edition](https://www.oracle.com/database/technologies/xe-prior-release-downloads.html)) and install the software. 18 | > Remember the password during the installation because this password is used for connecting the database account. 19 | 20 | ![alt text](https://github.com/shahidul034/BookList_database-project/blob/master/DIAGRAM%20PIC/installation.png) 21 | 22 | > Open the SQL Plus. Write 'connect system' and use the password that you set in the installation process. Follow the below figure. 23 | 24 | ![alt text](https://github.com/shahidul034/Database-Lab-Tutorial/blob/main/DIAGRAM%20PIC/sqlplus.png) 25 | 26 | > You can create a new user because we use the system(administrator) as a user. Then, we will give the new user all privileges to perform all SQL tasks. Follow the figure. 27 | > Change the password of the admin(system) 28 | ``` 29 | connect / as sysdba 30 | alter user system identified by [password] 31 | ``` 32 | ### Creating user and giving system privileges 33 | ``` 34 | create user shakib034 identified by shakib034; 35 | ``` 36 | > Delete any user 37 | ``` 38 | DROP USER username; 39 | ``` 40 | > Granting system privileges 41 | > Here, we give all the privileges to the user. 42 | ``` 43 | grant all privileges to shakib034; 44 | ``` 45 | > Reboke all the privileges from the user 46 | ``` 47 | revoke all privileges from user_name 48 | ``` 49 | ![alt text](https://github.com/shahidul034/Database-Lab-Tutorial/blob/main/DIAGRAM%20PIC/sqlplus2.png) 50 | > We can give specific privileges to the user. 51 | ``` 52 | grant create session to shakib034; 53 | ``` 54 | > Creating a role(Assigned system privileges) 55 | ``` 56 | create role cse2k15; 57 | grant create table, create session to cse2k15; 58 | grant cse2k15 to shakib034; 59 | ``` 60 | > Revoking System Privileges 61 | ``` 62 | revoke create table from shakib034; 63 | ``` 64 | 65 | ![alt text](https://github.com/shahidul034/Database-Lab-Tutorial/blob/main/DIAGRAM%20PIC/system%20privileges.png) 66 | ### Set line size and page size 67 | ``` 68 | show pagesize 69 | show linesize 70 | ``` 71 | ``` 72 | set pagesize 100 73 | set linesize 200 74 | ``` 75 | ### Database project demo 76 | | ![](https://github.com/shahidul034/database-project/blob/master/DIAGRAM%20PIC/pic1.png) | 77 | |:--:| 78 | | *SCHEMA* | 79 | 80 | 81 | | ![](https://github.com/shahidul034/database-project/blob/master/DIAGRAM%20PIC/P4.png) | 82 | |:--:| 83 | | *ERD* | 84 | 85 | 86 | ### Run the SQL command 87 | > You can run SQL script by using the SQL command line. Besides, you can write your SQL command in a txt file and save the txt file as a SQL extension. Then, type the below command in the SQL command line. 88 | ``` 89 | start C:\Users\andromeda\Desktop\file.sql 90 | ``` 91 | ### Oracle data types 92 | > number(Precision,Scale) 93 | 94 | Precision: total number of digits on either side of the decimal point 95 | 96 | Scale: number of digits to the right of the decimal point 97 | 98 | > char vs varchar2 (varchar2: Variable-length character strings and char: Fixed-length character data) 99 | 100 | > Date format: DD-MON-YY 101 | ``` 102 | create table test( 103 | name varchar2(30), 104 | name2 char(3), 105 | roll number(3), 106 | GPA number(3,2), 107 | roll2 number, 108 | s_dob DATE 109 | ); 110 | ``` 111 | Here is a link about the [oracle data types.](https://www.w3resource.com/oracle/oracle-data-types.php) 112 | ### Checking current user name 113 | ``` 114 | show user 115 | ``` 116 | ### Checking the existing table in database. 117 | ``` 118 | select table_name from user_tables; 119 | ``` 120 | ## 🎯Lab 2 121 | ![alt text](https://github.com/shahidul034/database2k19/blob/main/DIAGRAM%20PIC/show_table_name.png) 122 | 123 | > It shows that we already have two tables in my database. So we need to drop those tables from the database for a fresh start. You can use this command to drop any table. 124 | ``` 125 | drop table department; 126 | drop table course; 127 | ``` 128 | > First, we create the table "dept". Here, we can see that the primary key is dept_id because it uniquely identifies each row in the table. 129 | ``` 130 | Create table dept( 131 | dept_id number(20), 132 | dept_name varchar(30), 133 | faculty varchar(30), 134 | no_of_student number(20), 135 | primary key(dept_id) 136 | ); 137 | ``` 138 | > To see the last/immediate created table. 139 | ``` 140 | describe dept 141 | 142 | ``` 143 | > We also create a course, book and relation table. 144 | ``` 145 | Create table course( 146 | course_no varchar(20), 147 | course_name varchar(50), 148 | year_semister number(3), 149 | credit number(20,4), 150 | dept_id number(20), 151 | primary key(course_no), 152 | foreign key(dept_id) references dept(dept_id) 153 | ); 154 | create table book( 155 | book_no number(20), 156 | book_name varchar(50), 157 | author varchar(50), 158 | book_edition number(4), 159 | course_offering number(6), 160 | primary key(book_no) 161 | ); 162 | create table relation( 163 | book_no number(20), 164 | course_no varchar(20), 165 | primary key(book_no,course_no), 166 | foreign key (book_no) references book(book_no), 167 | foreign key (course_no) references course(course_no) 168 | ); 169 | ``` 170 | > When you observe the course table, they have another foreign key keyword referencing the dept_id from the "dept" table. We insert something in the course table. We must check whether this dept_id exists in the "dept" table. 171 | 172 | ### DDL 173 | ### Add column in the table 174 | > We add a column in the dept table which is location. 175 | > 176 | > Command structure: alter table **table_name** add **column_name** **column_definition**; 177 | ``` 178 | alter table dept add location char(20); 179 | ``` 180 | ### Modify column definition in the table 181 | > Command structure: alter table **table_name** modify **column_name** **column_definition**; 182 | > 183 | > We modify the location data types char(20) to varchar(23); 184 | ``` 185 | alter table dept modify location varchar(23); 186 | ``` 187 | ### Rename the column name 188 | > Command structure: alter table **table_name** rename **column_name** to **column_name**; 189 | ``` 190 | alter table dept rename column location to location2; 191 | ``` 192 | ### Drop the column from table 193 | > Command structure: alter table **table_name** drop column **column_name**; 194 | ``` 195 | alter table dept drop column location2; 196 | ``` 197 | ## 🎯Lab 3 198 | ### DML 199 | ### Insert the data in our table 200 | ``` 201 | insert into dept(dept_id,dept_name,faculty,no_of_student)values(7,'CSE','EE',120); 202 | ``` 203 | > Here, "dept" refers to the table name, and we also insert values according to table columns. 204 | > We also insert values into the other tables. 205 | 206 | ### Insert the data in the table 207 | ``` 208 | insert into dept(dept_id,dept_name,faculty,no_of_student)values(3,'EEE','EE',120); 209 | insert into dept(dept_id,dept_name,faculty,no_of_student)values(1,'CE','CE',120); 210 | insert into dept(dept_id,dept_name,faculty,no_of_student)values(5,'ME','ME',120); 211 | insert into dept(dept_id,dept_name,faculty,no_of_student)values(2,'ECE','EE',60); 212 | 213 | insert into course(course_no,course_name,year_semister,credit,dept_id)values('CSE1101','discreate math',11,3.00,7); 214 | insert into course(course_no,course_name,year_semister,credit,dept_id)values('CSE3105','database systems',31,3.00,7); 215 | insert into course(course_no,course_name,year_semister,credit,dept_id)values('EEE1101','Basic electrical engineering',11,3.00,3); 216 | insert into course(course_no,course_name,year_semister,credit,dept_id)values('ME3101','solid mechanics',31,3.00,5); 217 | 218 | insert into book(book_no,book_name,author,book_edition,course_offering)values(12,'discreate math','rosen',4,2); 219 | insert into book(book_no,book_name,author,book_edition,course_offering)values(13,'database systems','korth',5,1); 220 | insert into book(book_no,book_name,author,book_edition,course_offering)values(14,'data_communication','willim stallings',6,3); 221 | insert into book(book_no,book_name,author,book_edition,course_offering)values(15,'solid mechanics','john abraham',3,2); 222 | insert into book(book_no,book_name,author,book_edition,course_offering)values(16,'electrical engineering','boylsted',8,4); 223 | 224 | insert into relation(book_no,course_no)values(12,'CSE1101'); 225 | insert into relation(book_no,course_no)values(16,'EEE1101'); 226 | insert into relation(book_no,course_no)values(15,'ME3101'); 227 | insert into relation(book_no,course_no)values(13,'CSE3105'); 228 | 229 | ``` 230 | ![alt text](https://github.com/shahidul034/database2k19/blob/main/DIAGRAM%20PIC/dept_table.png) 231 | ![alt text](https://github.com/shahidul034/database2k19/blob/main/DIAGRAM%20PIC/course_table.png) 232 | ![alt text](https://github.com/shahidul034/database2k19/blob/main/DIAGRAM%20PIC/book_table.png) 233 | ![alt text](https://github.com/shahidul034/database2k19/blob/main/DIAGRAM%20PIC/relation_table.png) 234 | > All the data are inserted. Now, we do some experiments here. We run the command below. 235 | 236 | ``` 237 | insert into course(course_no,course_name,year_semister,credit,dept_id)values('MME1101','Basic structure of materials',11,3.00,9); 238 | ``` 239 | > We inserted the course_no "MME1101" and course_id "9" in the course table. After running the command, we found this error. 240 | > The course table refers to the dept table, and course id "9" does not exist in the dept table. So, it shows this error. 241 | 242 | ![alt text](https://github.com/shahidul034/database2k19/blob/main/DIAGRAM%20PIC/error.png) 243 | 244 | ### Displaying table data using SELECT command 245 | > Now, we find the rows from the "dept" table, which have 120 students using the select command. 246 | 247 | ``` 248 | select * from dept where no_of_student=120; 249 | ``` 250 | ![alt text](https://github.com/shahidul034/database2k19/blob/main/DIAGRAM%20PIC/select.png) 251 | 252 | > We find the dept_name, which course name is "database systems". 253 | ``` 254 | select * from dept where dept_id=(select dept_id from course where course_name='database systems'); 255 | ``` 256 | ![alt text](https://github.com/shahidul034/database2k19/blob/main/DIAGRAM%20PIC/subquery.png) 257 | 258 | > Here, we add extra select command by adding an additional condition to find the dept_id in the "dept" table. This type of query is called a subquery. 259 | 260 | ### Updating the data in a table 261 | > Now we want to update the value of the course name from the course table where course_no is "EEE1101"; 262 | > Command structure: update table_name set column_name=value where condition; 263 | ``` 264 | update course set course_name='Digital Electronics' where course_no='EEE1101'; 265 | ``` 266 | ### Deleting row from a table 267 | > We add an extra row to perform the delete operation in the dept table. 268 | ``` 269 | insert into dept(dept_id,dept_name,faculty,no_of_student)values(12,'URP','CE',60); 270 | ``` 271 | > Now we delete the row from the dept table where dept_id is 12. 272 | 273 | > Command structure: delete from **table_name** where **condition**; 274 | ``` 275 | delete from dept where dept_id=12; 276 | ``` 277 | 278 | ### union, intersect, and except 279 | ``` 280 | select dept_name from dept where dept_name like 'E%' union select dept_name from dept where dept_name like '%M%'; 281 | ``` 282 | ### With clause 283 | Calculates the maximum value of the no_of_student column from the dept table. The result of this subquery is a single row with a single column (val) containing the maximum value. 284 | ``` 285 | with max_student(val) as (select max(no_of_student) from dept) 286 | select * from dept,max_student where dept.no_of_student=max_student.val; 287 | ``` 288 | ### Save the SQL command output 289 | You can save the SQL command in different format. 290 | #### Save the SQL command output in CSV file 291 | > Simply change the folder path and your sql command. 292 | ``` 293 | SET COLSEP "," 294 | SET HEADING OFF 295 | SET PAGESIZE 0 296 | SET FEEDBACK OFF 297 | SPOOL C:\Users\andromeda\Desktop\file.csv 298 | SELECT * 299 | FROM dept; 300 | SPOOL OFF 301 | ``` 302 | #### Save the SQL command output in txt file 303 | > Simply change the folder path and your sql command. 304 | ``` 305 | SPOOL C:\Users\andromeda\Desktop\file.txt 306 | SELECT * 307 | FROM dept; 308 | SPOOL OFF 309 | ``` 310 | ## 🎯Lab 4 311 | ### Aggregate function 312 | We count how many row exist in dept table. 313 | ``` 314 | select count(*) from dept; 315 | ``` 316 | We also give alias name to any output in select command. 317 | ``` 318 | select count(dept_name) as number_of_dept from dept; 319 | ``` 320 | We can count distinct department name in dept table. 321 | ``` 322 | select count(distinct dept_name) as number_of_dept from dept; 323 | ``` 324 | We can count average and total no. of students in dept table. 325 | ``` 326 | select avg(no_of_student) from dept; 327 | select sum(no_of_student) from dept; 328 | ``` 329 | We can find max and min no. of students of any department from dept table. 330 | ``` 331 | select max(no_of_student) from dept; 332 | select min(no_of_student) from dept; 333 | ``` 334 | ### Group by and Having 335 | > Find the average of student according to faculty. 336 | ``` 337 | select faculty,avg(no_of_student) from dept group by faculty; 338 | 339 | ``` 340 | ![alt text](https://github.com/shahidul034/BookList_database-project/blob/master/DIAGRAM%20PIC/group%20by.png) 341 | 342 | > Find the average of student according to faculty where average of student is greater than 60. 343 | ``` 344 | select faculty,avg(no_of_student) from dept group by faculty having avg(no_of_student)>60; 345 | ``` 346 | ![alt text](https://github.com/shahidul034/BookList_database-project/blob/master/DIAGRAM%20PIC/having.png) 347 | 348 | ### Nested subquery 349 | > Find the department name where the “rosen”(author) book is taught. 350 | ``` 351 | select dept_name from dept where dept_id=(select dept_id from course where course_no=(select course_no from relation where book_no=(select book_no from book where author='rosen'))); 352 | ``` 353 | ![alt text](https://github.com/shahidul034/database2k19/blob/main/DIAGRAM%20PIC/subquery2.png) 354 | ### Set Membership(AND, OR,NOT) 355 | Find the rows where faculty is "EE" and "CSE" string exists in course_no. 356 | ``` 357 | select * from dept where faculty='EE' and dept_id in (select dept_id from course where course_no like '%CSE%') 358 | ``` 359 | ### some/all/exists/unique 360 | ![alt text](https://github.com/shahidul034/database2k19/blob/main/DIAGRAM%20PIC/some.png) 361 | ![alt text](https://github.com/shahidul034/database2k19/blob/main/DIAGRAM%20PIC/all.png) 362 | ``` 363 | select * from book where book_no> some(select book_no from book where book_no>=14); 364 | select * from book where book_no> all(select book_no from book where book_no>=14); 365 | ``` 366 | The exists construct returns the value true if the argument subquery is nonempty. 367 | ``` 368 | select * from course where year_semister>=11 and exists(select * from dept where faculty like '%EE%'); 369 | ``` 370 | ![alt text](https://github.com/shahidul034/database2k19/blob/main/DIAGRAM%20PIC/exist.png) 371 | ## 🎯Lab 5 372 | ### String operations 373 | > percent ( % ). The % character matches any substring. 374 | 375 | > underscore ( _ ). The _ character matches any character. 376 | 377 | > Demo table. 378 | 379 | ![alt text](https://github.com/shahidul034/database2k19/blob/main/DIAGRAM%20PIC/table_test.png) 380 | 381 | > 'E%' matches any string beginning with “E”. 382 | 383 | ```SELECT * FROM TEST WHERE NAME LIKE 'E%';``` 384 | 385 | ![alt text](https://github.com/shahidul034/database2k19/blob/main/DIAGRAM%20PIC/begin.png) 386 | 387 | > '%E' matches any string ending with “E”. 388 | > 389 | ```SELECT * FROM TEST WHERE NAME LIKE '%E';``` 390 | 391 | ![alt text](https://github.com/shahidul034/database2k19/blob/main/DIAGRAM%20PIC/end.png) 392 | 393 | > '%E%E%' contains with 'EE'. 394 | 395 | ```SELECT * FROM TEST WHERE NAME LIKE '%E%E%';``` 396 | 397 | > '_ _ _' matches any string of exactly three characters. 398 | 399 | ``` 400 | SELECT * FROM TEST WHERE NAME LIKE '___'; 401 | ``` 402 | 403 | > Below command matches any string of at least three characters and at most five characters. 404 | 405 | 406 | ```SELECT * FROM TEST WHERE NAME LIKE '___' or NAME LIKE '____' or NAME LIKE '_____';``` 407 | ## 🎯Lab 6 408 | ### Join operations 409 | 410 | ``` 411 | select * from dept natural join course where dept_id=7; 412 | ``` 413 | ![alt text](https://github.com/shahidul034/database2k19/blob/main/DIAGRAM%20PIC/natural_join_with_conditon.png) 414 | ``` 415 | select * from dept natural join course; 416 | ``` 417 | ![alt text](https://github.com/shahidul034/database2k19/blob/main/DIAGRAM%20PIC/natural_join.png) 418 | 419 | ``` 420 | select dept_name,course_name from dept join course using(dept_id); 421 | select dept_name,course_name from dept join course on dept.dept_id=course.dept_id; 422 | ``` 423 | ![alt text](https://github.com/shahidul034/database2k19/blob/main/DIAGRAM%20PIC/join.png) 424 | 425 | ``` 426 | select dept_name,course_name from dept left outer join course using(dept_id); 427 | select dept_name,course_name from dept right outer join course using(dept_id); 428 | select dept_name,course_name from dept full outer join course using(dept_id); 429 | select dept_name,course_name from dept left outer join course on dept.dept_id=course.dept_id; 430 | ``` 431 | ### Views 432 | View definition is not the same as creating a new relation by evaluating the query expression. 433 | 434 | A view of dept without their faculty,no_of_student. 435 | ``` 436 | create view dept_details as select dept_id,dept_name from dept; 437 | 438 | ``` 439 | Find all course in the CSE department. 440 | ``` 441 | create view CSE_DEPT_COURSE as select course_name from course where dept_id=(select dept_id from dept where dept_name='CSE'); 442 | ``` 443 | Views Defined Using Other Views 444 | ``` 445 | create view custom as select * from dept_details where dept_id>=3; 446 | ``` 447 | ### Cascading Actions in Referential Integrity 448 | ``` 449 | Create table dept2( 450 | dept_id number(20), 451 | dept_name varchar(30), 452 | faculty varchar(30), 453 | no_of_student number(20), 454 | primary key(dept_id) 455 | ); 456 | Create table course2( 457 | course_no varchar(20), 458 | course_name varchar(50), 459 | year_semister number(3), 460 | credit number(20,4), 461 | dept_id number(20), 462 | primary key(course_no), 463 | foreign key(dept_id) references dept2(dept_id) 464 | on delete cascade 465 | ); 466 | insert into dept2(dept_id,dept_name,faculty,no_of_student)values(7,'CSE','EE',120); 467 | insert into dept2(dept_id,dept_name,faculty,no_of_student)values(3,'EEE','EE',120); 468 | insert into dept2(dept_id,dept_name,faculty,no_of_student)values(1,'CE','CE',120); 469 | insert into dept2(dept_id,dept_name,faculty,no_of_student)values(5,'ME','ME',120); 470 | insert into dept2(dept_id,dept_name,faculty,no_of_student)values(2,'ECE','EE',60); 471 | 472 | insert into course2(course_no,course_name,year_semister,credit,dept_id)values('CSE1101','discreate math',11,3.00,7); 473 | insert into course2(course_no,course_name,year_semister,credit,dept_id)values('CSE3105','database systems',31,3.00,7); 474 | insert into course2(course_no,course_name,year_semister,credit,dept_id)values('EEE1101','Basic electrical engineering',11,3.00,3); 475 | insert into course2(course_no,course_name,year_semister,credit,dept_id)values('ME3101','solid mechanics',31,3.00,5); 476 | ``` 477 | 478 | ``` 479 | delete from dept2 where dept_id=5; 480 | ``` 481 | ### Constraints on a Single Relation 482 | ``` 483 | CREATE TABLE my_table ( 484 | id INTEGER PRIMARY KEY, 485 | name VARCHAR(50) NOT NULL, 486 | email VARCHAR(100) UNIQUE NOT NULL, 487 | age INTEGER CHECK (age >= 18) 488 | ); 489 | CREATE TABLE my_table2 ( 490 | id INTEGER PRIMARY KEY, 491 | name VARCHAR(50) NOT NULL, 492 | email VARCHAR(100) UNIQUE NOT NULL, 493 | age INTEGER CHECK (age >= 18 AND age <= 120), 494 | status VARCHAR(10) CHECK (status IN ('active', 'inactive', 'pending')), 495 | start_date DATE NOT NULL, 496 | end_date DATE NOT NULL, 497 | CONSTRAINT check_age_status CHECK ( 498 | (status = 'active' AND age >= 18 AND age <= 65) OR 499 | (status = 'inactive' AND age >= 18 AND age <= 120) OR 500 | (status = 'pending' AND age >= 18 AND age <= 100) OR 501 | (end_date > start_date) 502 | ) 503 | ); 504 | 505 | ``` 506 | > Insert data 507 | ``` 508 | insert into my_table values(1,'ss','ss@gmail.com',25); 509 | insert into my_table2 values(3,'ss','ss@gmail.com',25,'active','03-APR-2007','04-APR-2009'); 510 | ``` 511 | ## 🎯Lab 7 512 | ### PL/SQL 513 | ### Toad for oracle download 514 | Download the ([Toad](https://www.quest.com/products/toad-for-oracle/software-downloads.aspx)) and install the software. 515 | ### PL/SQL variable declaration and print value 516 | ``` 517 | set serveroutput on 518 | declare 519 | dept_id dept.dept_id%type; 520 | dept_name DEPT.DEPT_NAME%type; 521 | no_of_student number; 522 | begin 523 | select dept_id,dept_name,no_of_student into dept_id,dept_name,no_of_student from dept where dept_id=7; 524 | dbms_output.put_line('DEPT_id: '||dept_id|| ' DEPT_name: '||dept_name || ' no_of_student: '||no_of_student); 525 | end; 526 | / 527 | ``` 528 | ### Insert and set default value 529 | ``` 530 | set serveroutput on 531 | declare 532 | dept_id dept.dept_id%type:=9; 533 | dept_name DEPT.DEPT_NAME%type:='MME'; 534 | faculty dept.faculty%type:='ME'; 535 | no_of_student number:=30; 536 | begin 537 | insert into dept values(dept_id,dept_name,faculty,no_of_student); 538 | end; 539 | / 540 | ``` 541 | ### Row type 542 | ``` 543 | set serveroutput on 544 | declare 545 | dept_row dept%rowtype; 546 | begin 547 | select dept_id,dept_name,no_of_student into dept_row.dept_id,dept_row.dept_name,dept_row.no_of_student from dept where dept_id=7; 548 | end; 549 | / 550 | ``` 551 | ### Cursor and row count 552 | ``` 553 | set serveroutput on 554 | declare 555 | cursor dept_cursor is select * from dept; 556 | dept_row dept%rowtype; 557 | begin 558 | open dept_cursor; 559 | fetch dept_cursor into dept_row.dept_id,dept_row.dept_name,dept_row.faculty,dept_row.no_of_student; 560 | while dept_cursor%found loop 561 | dbms_output.put_line('DEPT_id: '||dept_row.dept_id|| ' DEPT_name: '||dept_row.dept_name || ' faculty: ' ||dept_row.faculty|| ' no_of_student: '||dept_row.no_of_student); 562 | dbms_output.put_line('Row count: '|| dept_cursor%rowcount); 563 | fetch dept_cursor into dept_row.dept_id,dept_row.dept_name,dept_row.faculty,dept_row.no_of_student; 564 | end loop; 565 | close dept_cursor; 566 | end; 567 | / 568 | ``` 569 | ## 🎯Lab 8 570 | ### FOR LOOP/WHILE LOOP/ARRAY with extend() function 571 | 572 | ``` 573 | TYPE NAMEARRAY IS VARRAY(5) OF book.book_name%type; 574 | ``` 575 | > It defines a user-defined collection type named NAMEARRAY as a variable array of 5 elements, with each element being of the same data type as the book_name column in the book table. 576 | ``` 577 | A_NAME NAMEARRAY:=NAMEARRAY(); 578 | ``` 579 | > It initializes a variable A_NAME of type NAMEARRAY. 580 | ``` 581 | set serveroutput on 582 | declare 583 | counter number; 584 | book_name2 book.book_name%type; 585 | TYPE NAMEARRAY IS VARRAY(5) OF book.book_name%type; 586 | A_NAME NAMEARRAY:=NAMEARRAY(); 587 | begin 588 | counter:=1; 589 | for x in 12..16 590 | loop 591 | select book_name into book_name2 from book where book_no=x; 592 | A_NAME.EXTEND(); 593 | A_NAME(counter):=book_name2; 594 | counter:=counter+1; 595 | end loop; 596 | counter:=1; 597 | WHILE counter<=A_NAME.COUNT 598 | LOOP 599 | DBMS_OUTPUT.PUT_LINE(A_NAME(counter)); 600 | counter:=counter+1; 601 | END LOOP; 602 | end; 603 | / 604 | ``` 605 | ### ARRAY without extend() function 606 | ``` 607 | DECLARE 608 | counter NUMBER := 1; 609 | book_name2 book.book_name%TYPE; 610 | TYPE NAMEARRAY IS VARRAY(5) OF book.book_name%TYPE; 611 | A_NAME NAMEARRAY:=NAMEARRAY('Book 1', 'Book 2', 'Book 3', 'Book 4', 'Book 5'); 612 | -- VARRAY with a fixed size of 5 elements and initialized with book names 613 | BEGIN 614 | counter := 1; 615 | FOR x IN 12..16 616 | LOOP 617 | SELECT book_name INTO book_name2 FROM book WHERE book_no=x; 618 | A_NAME(counter) := book_name2; 619 | counter := counter + 1; 620 | END LOOP; 621 | counter := 1; 622 | WHILE counter <= A_NAME.COUNT 623 | LOOP 624 | DBMS_OUTPUT.PUT_LINE(A_NAME(counter)); 625 | counter := counter + 1; 626 | END LOOP; 627 | END; 628 | 629 | ``` 630 | ### ARRAY with extend() and without extend() function 631 | The A_NAME.EXTEND() method is used to dynamically increase the size of the varray A_NAME at runtime by one element. When you use A_NAME.EXTEND(), you don't need to know the size of the varray in advance, and you can add elements to the varray as needed. 632 | 633 | On the other hand, if you declare the varray with a fixed size and initialize it with values at the beginning, you can assign values to the elements of the varray using the varray_name(index) := value syntax, as shown in the previous example. However, if you try to assign a value to an index that is beyond the fixed size of the varray, you will get a PLS-00302: component 'index' must be declared error. 634 | 635 | So the main difference between using and not using A_NAME.EXTEND() is that with A_NAME.EXTEND(), you can dynamically resize the varray as needed, while without it, the varray has a fixed size and you cannot add more elements than the declared size. 636 | 637 | ### IF /ELSEIF /ELSE 638 | ``` 639 | DECLARE 640 | counter NUMBER := 1; 641 | book_name2 book.book_name%TYPE; 642 | TYPE NAMEARRAY IS VARRAY(5) OF book.book_name%TYPE; 643 | A_NAME NAMEARRAY:=NAMEARRAY('Book 1', 'Book 2', 'Book 3', 'Book 4', 'Book 5'); 644 | -- VARRAY with a fixed size of 5 elements and initialized with book names 645 | BEGIN 646 | counter := 1; 647 | FOR x IN 12..16 648 | LOOP 649 | SELECT book_name INTO book_name2 FROM book WHERE book_no=x; 650 | if book_name2='discreate math' 651 | then 652 | dbms_output.put_line(book_name2||' is a '||'CSE course'); 653 | elsif book_name2='electrical engineering' 654 | then 655 | dbms_output.put_line(book_name2||' is a '||'EEE course'); 656 | else 657 | dbms_output.put_line(book_name2||' is a '||'other dept course'); 658 | end if; 659 | END LOOP; 660 | END; 661 | ``` 662 | ## 🎯Lab 9 663 | ### Procedure 664 | ``` 665 | CREATE OR REPLACE PROCEDURE proc2( 666 | var1 IN NUMBER, 667 | var2 OUT VARCHAR2, 668 | var3 IN OUT NUMBER 669 | ) 670 | AS 671 | t_show CHAR(30); 672 | BEGIN 673 | t_show := 'From procedure: '; 674 | SELECT course_name INTO var2 FROM course WHERE course_no IN (SELECT course_no FROM relation WHERE book_no = var1); 675 | var3 := var1 + 1; 676 | DBMS_OUTPUT.PUT_LINE(t_show || var2 || ' code is ' || var1 || ' In out parameter: ' || var3); 677 | END; 678 | / 679 | 680 | ``` 681 | ``` 682 | set serveroutput on 683 | declare 684 | book_no book.book_no%type:=12; 685 | course_name course.course_name%type; 686 | extra number; 687 | begin 688 | proc2(book_no,course_name,extra); 689 | end; 690 | / 691 | ``` 692 | ### Function 693 | ``` 694 | set serveroutput on 695 | create or replace function fun(var1 in varchar) return varchar AS 696 | value dept.dept_name%type; 697 | begin 698 | select dept_name into value from dept where dept_id=var1; 699 | return value; 700 | end; 701 | / 702 | ``` 703 | ``` 704 | set serveroutput on 705 | declare 706 | value varchar(20); 707 | begin 708 | value:=fun(5); 709 | end; 710 | / 711 | ``` 712 | > A function must return a value and procedure cannot return a value. 713 | 714 | ### drop procedure and function 715 | > Stucture: drop procedure procedure_name 716 | > Stucture: drop function function_name 717 | ``` 718 | drop procedure proc2; 719 | drop function fun; 720 | ``` 721 | ## 🎯Lab 10 722 | ### Trigger 723 | #### Trigger body 724 | ``` 725 | create trigger [trigger_name] 726 | [before | after] 727 | {insert | update | delete} 728 | on [table_name] 729 | [for each row] 730 | [trigger_body] 731 | ``` 732 | 733 | > This trigger is named "try" and is set to execute before each row is deleted from the "relation" table 734 | > The REFERENCING OLD AS o NEW AS n clause specifies that the trigger will reference the "old" values (i.e. the values before the deletion) as "o" and the "new" values (which do not exist in this case because it's a delete trigger) as "n". 735 | 736 | > Inside the trigger, there are two delete statements. The first statement deletes any rows from the "book" table where the "book_no" matches the value of "o.book_no" (i.e. the book number of the row being deleted from "relation"). The second statement deletes any rows from the "course" table where the "course_no" matches the value of "o.course_no" (i.e. the course number of the row being deleted from "relation"). 737 | ``` 738 | SET SERVEROUTPUT ON 739 | CREATE OR REPLACE TRIGGER try 740 | BEFORE delete ON relation 741 | REFERENCING OLD AS o NEW AS n 742 | FOR EACH ROW 743 | BEGIN 744 | delete from book where book_no=:o.book_no; 745 | delete from course where course_no=:o.course_no; 746 | END; 747 | / 748 | ``` 749 | ``` 750 | delete from relation where book_no=16; 751 | ``` 752 | 753 | > The trigger is set to fire after an update operation is performed on the "course" table. 754 | > For each row being updated, the trigger fires and updates the "book_name" column in the "book" table with the new course name (:n.course_name) where the "book_no" is present in the "relation" table for the corresponding old course number (:o.course_no). 755 | > 756 | ``` 757 | SET SERVEROUTPUT ON 758 | CREATE OR REPLACE TRIGGER trigger2 759 | after update ON course 760 | REFERENCING OLD AS o NEW AS n 761 | FOR EACH ROW 762 | Enable 763 | BEGIN 764 | update book set book_name=:n.course_name where book_no in (select book_no from relation where course_no=:o.course_no); 765 | END; 766 | / 767 | ``` 768 | ``` 769 | update course set course_name='discrete math2' where course_no='CSE1101'; 770 | ``` 771 | > This trigger is designed to execute after an insertion occurs in the "relation" table. It will update a related "book" table by incrementing the value of the "course_offering" field by 1, where the "book_no" matches the "book_no" of the newly inserted row. 772 | 773 | > The ":n.book_no" syntax refers to the "book_no" field in the newly inserted row (referenced as "n"). The "update" statement will modify the "book" table by incrementing the "course_offering" field by 1 for the row where the "book_no" matches the "book_no" of the newly inserted row. 774 | ``` 775 | SET SERVEROUTPUT ON 776 | CREATE OR REPLACE TRIGGER trigger_new 777 | after insert ON relation 778 | REFERENCING OLD AS o NEW AS n 779 | FOR EACH ROW 780 | Declare 781 | bok integer; 782 | BEGIN 783 | update book set course_offering=course_offering+1 where book_no=:n.book_no; 784 | END; 785 | / 786 | ``` 787 | 788 | ``` 789 | show errors; 790 | select * from user_triggers; 791 | drop trigger TRIGGER_NEW; 792 | ``` 793 | 794 | ## Reference 795 | **Silberschatz, A., Korth, H. F., & Sudarshan, S. (2023). Database System Concepts (7th ed.).** 796 | -------------------------------------------------------------------------------- /Transaction Management.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/Transaction Management.ppt -------------------------------------------------------------------------------- /ch3.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/ch3.pptx -------------------------------------------------------------------------------- /ch4.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/ch4.pptx -------------------------------------------------------------------------------- /complex datatypes.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shahidul034/Database-Lab-Tutorial/648a10a37c225e1fdfbfae151c021e6810978a77/complex datatypes.pptx --------------------------------------------------------------------------------