✏️ Explanatory Question

SQL এ কিভাবে প্যাটার্ন ম্যাচিং করা হয়? উদাহরণ সহযোগে বর্ণনা কর। কিভাবে তুমি অন্য একটি টেবিলের স্ট্রাকচার একই রেখে খালি নতুন টেবিল তৈরি করতে পারবে?

👁 1 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

SQL Pattern Matching and Creating Empty Table

Exam-ready answer with definition, LIKE operator, wildcard examples, SQL commands, and table structure copying.

Question 15

প্রশ্ন

SQL এ কিভাবে প্যাটার্ন ম্যাচিং করা হয়? উদাহরণ সহযোগে বর্ণনা কর। কিভাবে তুমি অন্য একটি টেবিলের স্ট্রাকচার একই রেখে খালি নতুন টেবিল তৈরি করতে পারবে?

Answer: Pattern Matching in SQL and Creating Empty Table with Same Structure

ভূমিকা

SQL-এ অনেক সময় আমাদের এমন data খুঁজতে হয় যেখানে exact value জানা থাকে না, কিন্তু value-এর কোনো নির্দিষ্ট pattern জানা থাকে। যেমন কোনো নাম A দিয়ে শুরু হয়েছে, কোনো email gmail.com দিয়ে শেষ হয়েছে, অথবা কোনো শব্দের মধ্যে নির্দিষ্ট character আছে।

এই ধরনের searching করার জন্য SQL-এ Pattern Matching ব্যবহার করা হয়। Pattern Matching সাধারণত LIKE operator এবং wildcard character-এর সাহায্যে করা হয়।

সহজ ভাষায়: Pattern Matching হলো SQL-এ এমন একটি পদ্ধতি যার মাধ্যমে exact value না জেনেও কোনো নির্দিষ্ট pattern অনুযায়ী data search করা যায়।

SQL Pattern Matching কী?

Pattern Matching হলো SQL-এর এমন একটি technique, যার মাধ্যমে character বা string data-এর মধ্যে কোনো নির্দিষ্ট pattern খুঁজে বের করা যায়।

SQL-এ Pattern Matching করার জন্য সাধারণত LIKE operator ব্যবহার করা হয়। LIKE operator-এর সঙ্গে wildcard character ব্যবহার করে আমরা flexible searching করতে পারি।

Definition: Pattern Matching in SQL is used to search data based on a specific character pattern using the LIKE operator and wildcard symbols.

LIKE Operator

LIKE operator SQL-এর WHERE clause-এর সঙ্গে ব্যবহার করা হয়। এটি কোনো column-এর value নির্দিষ্ট pattern-এর সঙ্গে match করছে কিনা তা পরীক্ষা করে।

Syntax


SELECT column1, column2
FROM table_name
WHERE column_name LIKE pattern;
            

এখানে pattern অংশে wildcard character ব্যবহার করা হয়।

Wildcard Characters in SQL

SQL Pattern Matching-এ wildcard character খুব গুরুত্বপূর্ণ। এগুলির সাহায্যে আমরা partial matching করতে পারি।

Wildcard Meaning Example Pattern Result Meaning
% Zero বা more characters match করে 'A%' A দিয়ে শুরু হওয়া values
_ Exactly one character match করে 'R_ya' R এবং ya-এর মাঝে একটি character থাকবে
[] Specified set বা range-এর একটি character match করে '[AR]%' A বা R দিয়ে শুরু values
[^] Specified set বা range বাদে অন্য character match করে '[^A]%' A দিয়ে শুরু নয় এমন values

Note: % এবং _ প্রায় সব SQL database-এ commonly ব্যবহৃত হয়। [] এবং [^] মূলত SQL Server-এ বেশি ব্যবহৃত হয়।

Sample Table: Student

নিচের Student table ব্যবহার করে pattern matching বোঝানো হলো:

Roll_No Name Email City
101 Rahul rahul@gmail.com Kolkata
102 Riya riya@yahoo.com Delhi
103 Karim karim@gmail.com Kolkata
104 Anita anita@hotmail.com Mumbai
105 Arjun arjun@gmail.com Delhi

Example 1: A দিয়ে শুরু হওয়া নাম খুঁজে বের করা

যেসব student-এর নাম A দিয়ে শুরু হয়েছে তাদের বের করতে:


SELECT *
FROM Student
WHERE Name LIKE 'A%';
            

Output

Roll_No Name Email City
104 Anita anita@hotmail.com Mumbai
105 Arjun arjun@gmail.com Delhi

এখানে 'A%' মানে A দিয়ে শুরু এবং তার পরে যেকোনো number of characters থাকতে পারে।

Example 2: gmail.com দিয়ে শেষ হওয়া Email খুঁজে বের করা

যেসব student-এর email gmail.com দিয়ে শেষ হয়েছে তাদের বের করতে:


SELECT Name, Email
FROM Student
WHERE Email LIKE '%gmail.com';
            

Output

Name Email
Rahul rahul@gmail.com
Karim karim@gmail.com
Arjun arjun@gmail.com

এখানে '%gmail.com' মানে gmail.com-এর আগে যেকোনো characters থাকতে পারে, কিন্তু শেষে gmail.com থাকতে হবে।

Example 3: Name-এর মধ্যে “ri” আছে এমন record বের করা

যেসব student-এর নামের মধ্যে ri আছে তাদের বের করতে:


SELECT *
FROM Student
WHERE Name LIKE '%ri%';
            

এখানে '%ri%' মানে ri নামের শুরুতে, মাঝখানে বা শেষে কোথাও থাকতে পারে।

Example 4: Underscore (_) Wildcard ব্যবহার

যদি এমন name খুঁজতে হয় যেখানে প্রথম character R, দ্বিতীয় character যেকোনো একটি character এবং পরে ya থাকে:


SELECT *
FROM Student
WHERE Name LIKE 'R_ya';
            

এই query Riya match করবে, কারণ এখানে R এবং ya-এর মাঝে একটি character আছে।

Important: % multiple characters match করে, কিন্তু _ শুধুমাত্র exactly one character match করে।

NOT LIKE Operator

NOT LIKE ব্যবহার করা হয় এমন records খুঁজতে যেগুলি নির্দিষ্ট pattern-এর সঙ্গে match করে না।

Example


SELECT *
FROM Student
WHERE Email NOT LIKE '%gmail.com';
            

এই query সেই students দেখাবে যাদের email gmail.com দিয়ে শেষ হয় না।

ESCAPE Character

যদি কোনো actual data-এর মধ্যে % বা _ character থাকে এবং আমরা সেটিকে wildcard হিসেবে নয়, normal character হিসেবে search করতে চাই, তাহলে ESCAPE ব্যবহার করা যায়।


SELECT *
FROM Product
WHERE Product_Code LIKE 'A\_%' ESCAPE '\';
            

এখানে underscore (_) wildcard হিসেবে নয়, actual underscore character হিসেবে match হবে।

Pattern Matching Summary

Pattern Meaning Example
'A%' A দিয়ে শুরু Anita, Arjun
'%a' a দিয়ে শেষ Riya, Anita
'%ri%' মাঝে ri আছে Arjun নয়, Karim-এ ri নেই; Riya-তে Ri pattern আছে case অনুযায়ী
'R_ya' R এবং ya-এর মাঝে একটি character Riya
NOT LIKE '%gmail.com' gmail.com দিয়ে শেষ নয় Yahoo বা Hotmail email

অন্য Table-এর Structure একই রেখে Empty Table তৈরি করা

অনেক সময় আমাদের existing table-এর মতো একই structure নিয়ে একটি নতুন table তৈরি করতে হয়, কিন্তু পুরোনো table-এর data copy করতে হয় না।

অর্থাৎ নতুন table-এ column name এবং data type একই থাকবে, কিন্তু table খালি থাকবে।

সহজ ভাষায়: পুরোনো table-এর structure copy হবে, কিন্তু data copy হবে না।

Method 1: MySQL-এ CREATE TABLE ... LIKE ব্যবহার

MySQL-এ কোনো existing table-এর structure copy করে empty table তৈরি করতে CREATE TABLE ... LIKE statement ব্যবহার করা হয়।

Syntax


CREATE TABLE new_table_name LIKE old_table_name;
            

Example


CREATE TABLE Student_Backup LIKE Student;
            

এখানে Student_Backup নামে একটি নতুন empty table তৈরি হবে, যার structure Student table-এর মতো হবে।

Important: এই পদ্ধতিতে table structure copy হয়, কিন্তু data copy হয় না।

Method 2: SELECT Query ব্যবহার করে Empty Table তৈরি করা

অনেক database system-এ SELECT query ব্যবহার করে structure copy করা যায়। এই ক্ষেত্রে condition false দেওয়া হয় যাতে কোনো row copy না হয়।

Generic Concept


CREATE TABLE new_table_name AS
SELECT *
FROM old_table_name
WHERE 1 = 0;
            

এখানে WHERE 1 = 0 condition সবসময় false। তাই কোনো data copy হবে না, কিন্তু column structure copy হবে।

Example


CREATE TABLE Student_Empty AS
SELECT *
FROM Student
WHERE 1 = 0;
            

এই query Student table-এর মতো structure নিয়ে Student_Empty table তৈরি করবে, কিন্তু Student table-এর data copy করবে না।

Method 3: SQL Server-এ SELECT INTO ব্যবহার

SQL Server-এ অনেক সময় SELECT INTO ব্যবহার করে নতুন table তৈরি করা হয়। Empty table তৈরি করতে false condition ব্যবহার করা যায়।


SELECT *
INTO Student_Empty
FROM Student
WHERE 1 = 0;
            

এখানে Student_Empty table তৈরি হবে, কিন্তু কোনো row insert হবে না।

Table Structure Copy করার পদ্ধতির Summary

Database / Method Command Result
MySQL CREATE TABLE new_table LIKE old_table; Same structure সহ empty table তৈরি করে
Generic SQL Style CREATE TABLE new_table AS SELECT * FROM old_table WHERE 1 = 0; Column structure copy করে, data copy করে না
SQL Server SELECT * INTO new_table FROM old_table WHERE 1 = 0; Empty table তৈরি করে old table-এর selected structure অনুযায়ী

Complete Exam-Oriented Example

ধরা যাক Student table আছে:


Student(Roll_No, Name, Email, City)
            

Pattern Matching: যেসব student-এর নাম A দিয়ে শুরু তাদের বের কর:


SELECT *
FROM Student
WHERE Name LIKE 'A%';
            

Empty Table with Same Structure: Student table-এর মতো structure নিয়ে empty table তৈরি কর:


CREATE TABLE New_Student LIKE Student;
            

অথবা:


CREATE TABLE New_Student AS
SELECT *
FROM Student
WHERE 1 = 0;
            

Pattern Matching-এর গুরুত্ব

  • Partial information দিয়ে data search করা যায়।
  • Name, email, city, product code ইত্যাদি text-based data filter করা যায়।
  • Exact value না জানলেও similar records খুঁজে পাওয়া যায়।
  • Reporting এবং searching feature তৈরি করতে সাহায্য করে।
  • LIKE operator ব্যবহার করে flexible searching করা যায়।

Exam Writing Tips

  • প্রথমে Pattern Matching-এর definition লিখবে।
  • LIKE operator-এর syntax লিখবে।
  • % এবং _ wildcard-এর কাজ অবশ্যই লিখবে।
  • প্রতিটি wildcard-এর example দেবে।
  • অন্য table-এর structure copy করার জন্য CREATE TABLE ... LIKE বা WHERE 1 = 0 method লিখবে।
  • শেষে conclusion লিখবে।

উপসংহার

সুতরাং, SQL-এ Pattern Matching করার জন্য LIKE operator এবং wildcard characters ব্যবহার করা হয়। % wildcard zero বা more characters match করে এবং _ wildcard exactly one character match করে। এর মাধ্যমে exact value না জেনেও pattern অনুযায়ী data search করা যায়।

অন্যদিকে, কোনো existing table-এর structure একই রেখে empty table তৈরি করার জন্য MySQL-এ CREATE TABLE ... LIKE ব্যবহার করা যায়। এছাড়াও WHERE 1 = 0 condition ব্যবহার করে table structure copy করে data ছাড়া নতুন table তৈরি করা যায়।

Quick Revision Points

  • Pattern Matching SQL-এ LIKE operator দিয়ে করা হয়।
  • % zero বা more characters match করে।
  • _ exactly one character match করে।
  • LIKE 'A%' মানে A দিয়ে শুরু।
  • LIKE '%gmail.com' মানে gmail.com দিয়ে শেষ।
  • NOT LIKE pattern match না করা records বের করে।
  • CREATE TABLE new_table LIKE old_table; empty table তৈরি করে same structure সহ।
  • WHERE 1 = 0 ব্যবহার করলে data copy হয় না, শুধু structure copy হয়।