Tambayoyin Tambayoyi don Masu Haɓaka SQL: Tambayoyin SQL gama gari Q&A- Part 2

Yadda ake share bayanai daga tebur ta amfani da DELETE sanarwa a cikin SQL

Amsa: Yi amfani da DELETE bayanin don cire bayanai daga tebur

Misali:

DELETE FROM Customers WHERE CustomerID = 1;

 

Bayyana manufar wani Index da fa'idodin amfani da Fihirisa a cikin SQL

Amsa: An Index shine tsarin bayanan da ke inganta saurin dawo da bayanai a cikin ma'ajin bayanai. An ƙirƙira shi akan ɗaya ko fiye da ginshiƙan tebur kuma yana taimakawa wajen rage lokacin da ake buƙata don nema da rarraba bayanai. Fa'idodin amfani da Fihirisar sun haɗa da ingantattun aikin tambaya da maido da bayanai cikin sauri.

 

Yadda ake amfani da CREATE TABLE bayanin don ƙirƙirar sabon tebur a cikin SQL

Amsa: Yi amfani da CREATE TABLE bayanin don ƙirƙirar sabon tebur a cikin bayanan.

Misali:

CREATE TABLE Customers( 
    CustomerID INT PRIMARY KEY,  
    CustomerName VARCHAR(50),  
    ContactName VARCHAR(50),  
    Country VARCHAR(50)  
);  

 

Yadda ake amfani da ALTER TABLE bayanin don ƙara sabon shafi zuwa tebur a cikin SQL.

Amsa: Yi amfani da ALTER TABLE bayanin don ƙara sabon shafi zuwa tebur mai gudana.

Misali:

ALTER TABLE Customers ADD Email VARCHAR(100);

 

Yadda ake amfani da DROP TABLE bayanin don share tebur a cikin SQL

Amsa: Yi amfani da DROP TABLE bayanin don cire tebur daga ma'ajin bayanai.

Misali:

DROP TABLE Customers;

 

Bayyana yadda ake amfani da bayanan UNION da UNION ALL maganganun a cikin SQL

Amsa:

  • UNION: Yana haɗa sakamakon SELECT tambayoyi biyu ko fiye zuwa saitin sakamako ɗaya kuma yana cire kwafi.
  • UNION ALL: Kama da UNION, amma yana riƙe da kwafin layuka.

 

Yadda ake amfani da LIKE bayanin da haruffa na musamman a cikin yanayin bincike a cikin SQL

Amsa: Yi amfani da bayanin LIKE don yin daidaitaccen tsari don binciken rubutu. Akwai haruffa na musamman guda biyu da aka saba amfani da su tare da LIKE:

  • %: Yana wakiltar kowane jeri na haruffa, gami da sifili ko fiye da haruffa.
  • _: Yana wakiltar hali guda ɗaya.
SELECT * FROM Customers WHERE CustomerName LIKE 'A%';

 

Yi bayani daban-daban tambayoyin dawo da bayanai: SELECT, SELECT DISTINCT, SELECT TOP a cikin SQL

Amsa:

  • SELECT: Maido bayanai daga tebur ɗaya ko fiye.
  • SELECT DISTINCT: Yana dawo da keɓaɓɓun bayanai daga ginshiƙi, yana cire ƙima mai kwafi.
  • SELECT TOP: Yana dawo da takamaiman adadin layuka daga sakamakon tambaya.
SELECT DISTINCT Country FROM Customers;  
SELECT TOP 10 * FROM Orders;  

 

Yadda ake amfani da GROUP BY, HAVING, ORDER BY maganganun tare a cikin SQL

Amsa: Ta hanyar haɗa GROUP BY, HAVING, ORDER BY bayanan, za mu iya tattara bayanai, tace ƙungiyoyi, da tsara sakamakon.

Misali:

SELECT Country, COUNT(*) AS TotalCustomers  
FROM Customers  
GROUP BY Country  
HAVING COUNT(*) > 5  
ORDER BY TotalCustomers DESC;  

 

Bayyana manufar a transaction da kuma yadda ake amfani da BEGIN TRANSACTION, COMMIT, ROLLBACK maganganun a cikin SQL.

Amsa: Ma'amala shine jerin ayyuka guda ɗaya ko fiye da ake kula da su azaman raka'a ɗaya. Idan kowane ɗayan ayyukan da ke cikin ma'amala ya gaza, gabaɗayan ma'amalar za ta koma baya kuma duk canje-canjen an soke su.

  • BEGIN TRANSACTION: Fara sabon ciniki.
  • COMMIT: Ajiye da kuma tabbatar da canje-canjen da aka yi a cikin ma'amala zuwa bayanan bayanai.
  • ROLLBACK: Yana soke ciniki kuma yana soke duk wani canje-canje da aka yi a cikin ciniki
BEGIN TRANSACTION;  
UPDATE Accounts SET Balance = Balance- 100 WHERE AccountID = 123;  
UPDATE Accounts SET Balance = Balance + 100 WHERE AccountID = 456;  
COMMIT;