Search This Blog

Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts

Friday, June 10, 2016

האם תוכל לכתוב לי מהן הפקודות הבסיסיות ביותר ב - SQL בבקשה?

חבר שאל את השאלה "האם תוכל לכתוב לי מהן הפקודות הבסיסיות ביותר ב - SQL? ", כהכנה ליום עיון. גירדתי מעט בראש, ומאחר ולא היה לי זמן להתחיל לחפש מדריך בסיסי טוב באמת לSQL ברשת, חשבתי שיהיה הכי מהר לשרבט משהו בעצמי, אז שלחתי לו את התשובה הבאה (ואחרי ששלחתי, חשבתי שעדיף לשמור אותה כאן, למקרה הצורך):

1. אני ממליץ שתוריד ותתקין את הכלי הבא - SQLITE - מאוד פשוט לתפעול.
(מהקישור, מה שאתה צריך זה תחת Precompiled Binaries for Windows
את sqlite-tools-win32-x86-3130000.zip )

2. אחרי שתוריד, unzip אותו, תוכל להריץ את התוכנית sqlite3 .

3. בתמצית, SQL, שהיא כלי לעיבוד מידע, נחלקת לפקודות הגדרה, פקודות שליפה (יותר נכון, פקודת שליפה) ופקודות מניפולציה.  (זו אמירה גדולה ולא מדוייקת אבל מספיקה לשלב הראשוני הזה). מי שמבקש יותר יכול להציץ בערך ויקיפדיה של SQL.

הגדרה: create.
למשל בsqlite שהגדרנו, הקש את הפקודה הבאה:
create table t1 (n1 number, v1 varchar2(1000));

עיבוד: insert, update וdelete
למשל, בsqlite שהגדרנו, הקש את הפקודה הבאה:
insert into t1 values (1,'hello');

שליפה:  select
למשל, בsqlite שהגדרנו, הקש את הפקודה הבאה:
select * from t1;

ואם נצטט מתוך ויקיפדיה:
"משפט SELECT, מאחזר נתונים מתוך טבלה (או מספר טבלאות), אבל לא מבצע בהם שינויים קבועים.
DML - שפת מניפולציה בנתונים (Data Manipulation Language) - שפה המשמשת לטיפול בנתונים עצמם
משפט INSERT מוסיף שורות חדשות לטבלה.
משפט UPDATE מעדכן נתונים בשורות הקיימות בטבלה.
משפט DELETE מוחק שורות נתונים מטבלה".

4. כדאי להציץ בויקיפדיה, לראות כמה דוגמאות בסיסיות קצת יותר מתקדמות שמובאות שם. ובכל זאת, למען התמציתיות, כמה דוגמאות יחד עם הפלט מsqlite:

sqlite> insert into t1 values (2,'there');
sqlite> select * from t1;
1|hello
2|there

sqlite> update t1 set n1=3 where n1=1;
sqlite> select * from t1;
3|hello
2|there

sqlite> delete from t1 where v1='there';
sqlite> select * from t1;
3|hello

sqlite> insert into t1 values (4,'b la bla ');
sqlite> select * from t1;
3|hello
4|b la bla

sqlite> select * from t1 where n1=4;
4|b la bla

sqlite> select * from t1 order by n1 desc;
4|b la bla
3|hello

דברים שיכולים לעזור




Thursday, October 15, 2015

A new certification for an old qualification:

I've been contemplating
in recent months
about future directions...

One thing lead to another
and all of a sudden
I find myself
getting a new certification
for an old qualification:

Sunday, September 29, 2013

Oracle tidbits: IN vs. EXISTS and more...

As I was trying to recall how does oracle execute a correlated subquery I ran into several discussions by tom kyte about in, exists, and more ... 

Not every day you get a chance to see a discussion spanning more than 12 years, exemplifying how the development of the Oracle Optimizer changes technical answers over time... 

Friday, June 28, 2013

Some Oracle DB cursors examples

Steven Feuerstein had an excellent article on Working with Cursors on the March 2013 Oracle Magazine. It is a good article for newbies or those who somehow never got to work with this aspect of the DB.

For me, it has been a while since I've delved in Oracle development (mostly non-db web related technologies nowadays), and as I've had my share of returning into Oracle in the past (and can assume with confidence that will also have similar experiences in the future), I think it would be wise to keep around some examples (quoted from that article), to make those head-scratching, 'now, how does this syntax go' moments less annoying...

Simple Fetch:

DECLARE 
   l_employee employees%ROWTYPE; 
BEGIN 
   SELECT * 
   INTO l_employee 
   FROM employees 
   WHERE employee_id = 138; 
   DBMS_OUTPUT.put_line ( l_employee.last_name); 
END;

Working with an explicit Cursor definition:

DECLARE
  CURSOR c1 IS SELECT department_id FROM departments;
  deptid departments.department_id%TYPE;
BEGIN
  OPEN c1;
  FETCH c1 INTO deptid;
  CLOSE c1;
  /* if handled more than one, could put the fetch in loop:
  OPEN c1;
  LOOP
     FETCH x into l_variable; EXIT WHEN c1%NOTFOUND;
     -- do something with l_variable;
  END LOOP;
  CLOSE c1;
  -- don't forget to handle other EXCEPTIONS
  -- see more basic examples
  */ 
 END;

Working with the For Loop and an implicit Cursor definition :
(my personal favorite)

DECLARE
  NULL;
BEGIN
   FOR employee_rec IN (
      SELECT *
      FROM employees
      WHERE department_id = 10)
   LOOP
       DBMS_OUTPUT.put_line (
       employee_rec.last_name); 
   END LOOP;
END;

The Article has much more to offer, and it is a part of an excellent series on PL/SQL that Oracle Magazine is running. Worth the time for those wishing to get acquainted, refresh their memory, or get deeper...