Archive for the ‘SQL’ Category

Internship, Day 29 (30 May 2012)

Posted on May 31st, 2012 in computer, in English, in Thai, personal, programming, SQL | No Comments »


(Photo: More “proper food” my friend eats.)

ใกล้ครบกำหนดฝึกงานขั้นต่ำแล้ว (ของผมต้องถึง Day 32 เพราะลางานไป 10 ชั่วโมง) วันนี้ผมไปทำงานโดยมีเจตนาเอาฐานข้อมูลที่มีอยู่แล้วมาใช้ล้วนๆ เลย สำหรับงานที่ได้ในตอนนี้คือทำให้ระบบบันทึกการใช้งาน IDC บันทึกการนำอุปกรณ์เข้าและออกได้สมบูรณ์แล้ว แต่ยังไม่สามารถยกเลิกงานบางส่วนระหว่างทำงานได้ในบางกรณี (การยกเลิกงานอาจเกิดขึ้นในกรณีที่ไม่ต้องการทำงานนั้นๆ อีกต่อไป เช่น ตรวจพบทีหลังว่าเซิร์ฟเวอร์ที่นำมาใช้ไม่ได้ หรือไม่ต้องการนำของออกแล้ว เป็นต้น) เนื่องจากยังติดปัญหาว่าโค้ดเก่าพึ่งพา PHP Session มากเกินไป แต่กลับไม่ยอมเก็บตัวแปรที่อ้างถึงผู้ใช้ไว้เลย

So I’m nearing the minimum hours for internship (actually I need 32 days because I took 10 hours of leave). Today I came to work with pure intention to utilize the old database. Today I got the IDC visitor system working right with equipment installation and returns, but still cannot cancel jobs half-way in some cases (It might occur due to server installation error, or customer no longer wishes to remove the piece, for example.) owing to the dependency of the old code on PHP Session system, but lack of any actual reference to database.

Delete from multiple tables

 DELETE table1, table2
FROM table1, table2
WHERE
  table1.pk = table2.pk
  AND (your_condition_here); 

Select neighbors

If you have some tuple (x1,y) where you know only the value of x1, and you wish to select (x1,y), (x2,y), …, (xn,y), this is way to go.

 SELECT X AS REFX, Y AS REFY
FROM your_table
WHERE EXISTS
  (
  SELECT * FROM your_table
  WHERE Y = REFY
    AND X = $x1
  ) 

Internship, Day 28 (29 May 2012)

Posted on May 29th, 2012 in computer, CPE, in English, in Thai, personal, PHP, programming, SQL | No Comments »


(Photo: Guess where we had our lunch today!)

วันนี้กลายเป็นว่างานไม่ค่อยเดินเท่าไหร่ เนื่องจากผมพยายามสร้าง Task List ที่เอาไว้เก็บว่าลูกค้าแต่ละคนเข้ามาทำอะไรบ้าง ความซับซ้อนมันอยู่ที่การสร้างระบบฐานข้อมูลที่รองรับรายการหลายๆ ชนิดได้ ซึ่งตรงนี้ผมก็ทำโดยใช้ generalization และ specialization และหลังจากที่นั่งมั่วกับโค้ดเป็นเวลานาน ในที่สุดผมก็เริ่มเขียน PHP แบบ MVC เป็นขึ้นมาบ้าง แต่ก็ยังไม่ค่อยเข้าใจเท่าไหร่ ที่บอกว่างานไม่เดินเป็นเพราะว่าที่ทำลงไปจริงๆ ไม่ต้องทำก็ได้ เพราะโค้ดที่มีมามันก็มี Task List อยู่แล้ว แค่เก็บไว้อีกที่นึง อ้อ วันนี้พี่พาไปเลี้ยงมือเที่ยงด้วย ขอบพระคุณมากครับ (ถ้าพี่ตามมาอ่านในนี้นะ ^ ^”)

So I didn’t progress much today. I tried to build a Task List system which keeps track of what each customer does at a particular time. The complexity lies within the database code that needs to support multiple types of entries for the Task List, which can be accomplished using generalization and specialization techniques. After messing around for some time, I started to grasp the concepts of PHP and MVC and can now code a bit more properly. The lack of progress was due to the fact that there’s already a Task List system in place. I was reinventing the wheel all along. Finally, special thanks to the senior that treated me lunch!

The Wheel I did reinvent


Note: Tables authorized_on_customer and equipment and column data in temp_equipment shown here are simplified versions of the “real thing” on the live server.

Internship, Day 27 (28 May 2012)

Posted on May 29th, 2012 in computer, in English, in Thai, personal, PHP, programming, SQL | No Comments »


(Photo: What my friend eats for lunch. Not always like this, but always better than mine.)

เข้าสู่สองสัปดาห์สุดท้ายของการฝึกงานแล้ว ผมก็ยังไปทำงานชิ้นเดิมต่อ โดยวันนี้ผมไปขอพี่สร้างตารางใหม่ลงในฐานข้อมูลได้แล้ว โดยสร้างเป็นตารางขึ้นมาเพื่อใช้เก็บข้อมูลชั่วคราวของ Session ส่วนปัญหาเรื่องโค้ดที่ไม่มีการอธิบายนั้นตอนนี้ผมใช้วิธีสร้างไฟล์ติดต่อฐานข้อมูลแยกไว้ต่างหากออกมาจากตัว MVC ก่อน แต่ก็พยายามใช้ของเดิมเท่าที่ทำได้ ไม่สร้างใหม่ (ปัญหานอกจากการไม่มี document คือเขาเอาปลั๊กอิน JS มาใส่ ทำให้ไล่หน้าต่างๆ ไม่ถูก เพราะต้องมาไล่ทั้ง MVC ไล่ทั้ง JS call) ผลที่ได้วันนี้เลยไม่ก้าวหน้าเท่าที่ควร พี่ก็เร่งให้งานเสร็จ เพราะจะได้ไปทำงานอื่นอีกสองอัน

And now I’ve entered the last two weeks. I still work on the same piece today. I got permission to create new tables necessary to store temporary variables from sessions. The undocumented code problem forced me to build a new (hack) procedural code separate from the MVC architecture provided by CodeIgniter; I still attempted to preserve the old code however. Apart from lack of documentation, there are also JS plugins and they really throw me off balance because I spend too much time hunting each piece down, tracing both JS and MVC. In the end, I have not progressed much despite the expedition given by the senior because they wanted me to do two more pieces.

Internship, Day 26 (25 May 2012)

Posted on May 28th, 2012 in computer, in English, in Thai, personal, PHP, programming, SQL | No Comments »


(Photo: Handling two computers at once…)

สรุปว่าส่วนที่ต้องทำจริงๆ คือ การเก็บพวก session การใช้งานต่างๆ ลงไปพักไว้ในฐานข้อมูลก่อน โอเคครับ ไม่ยาก แต่นาน 555+ ที่ต้องเก็บลงฐานข้อมูลเป็นเพราะว่ามันนานเกินกว่าจะเก็บไว้ด้วย PHP session ครับ

So it’s just storing sessions into the database. It’s not that hard but will take some time to code. The reason for this is because the said session is a real-world session which is longer than a PHP session.

SQL: Insert with respect to reference table

Since there’s not much in the blog today, let’s write another knowledge scrap. A reference table is an enumerated table with IDs against “facts” in a one-to-one manner such as:

 
userclass(id INT, name VARCHAR(20));
+------+----------+
| id   | name     |
+------+----------+
|    1 | normal   |
|    2 | silver   |
|    3 | gold     |
|    4 | platinum |
+------+----------+

usertype(id INT, name VARCHAR(20));
+------+------------+
| id   | name       |
+------+------------+
|    1 | individual |
|    2 | sme        |
|    3 | corporate  |
+------+------------+
 

Say, if we want to insert a “silver sme” user into a table, rather than, with PHP variables, using

 
// Get the reference ID from two tables
$q1 = "SELECT id FROM userclass WHERE name='$class'";
$q2 = "SELECT id FROM usertype WHERE name='$type'";

$r1 = mysql_query($q1);
$r2 = mysql_query($q2);

$s1 = mysql_fetch_assoc($r1);
$s2 = mysql_fetch_assoc($r2);

$classid = $s1 ['id'] ;
$typeid = $s2 ['id'] ;

// Now insert into new table
$q3 = "INSERT INTO user (uname, uclass, utype) VALUES('$username', $classid, $typeid)";
$r3 = mysql_query($q3) or die(mysql_error());
 

we have a “PHP-clean but more SQL-complex” way to do it:

 
$q = "INSERT INTO user (uname, uclass, utype)
       (SELECT '$username', userclass.id, usertype.id
        FROM userclass, usertype
        WHERE userclass.name = '$class'
          AND usertype.name = '$type'
       )";
$r = mysql_query($q) or die(mysql_error());
 

It does not hurt to study SQL. You can in fact extend this to as many reference tables as necessary. This is a generalized version of the SQL code:

 
INSERT INTO target_table (
  fixed_col_1, fixed_col_2, ..., fixed_col_m,
  ref_col_1, ref_col_2, ..., ref_col_n)
  (SELECT fixed_input_1, fixed_input_2, ..., fixed_input_m,
          ref_id_1, ref_id_2, ..., ref_id_n
   FROM ref_table_1, ref_table_2, ..., ref_table_n
   WHERE
     ref_table_1.ref_val_1 = ref_input_1 AND
     ref_table_2.ref_val_2 = ref_input_2 AND
     ... AND
     ref_table_n.ref_val_n = ref_input_n
  );
 

Where:

  • fixed_col_i are target columns you want your fixed inputs to go into
  • ref_col_j are target columns you want your inputs to be referenced into IDs and go into
  • fixed_input_i is the data itself. Usually a string or integer or something else.
  • ref_id_j is the id column from each table j. For brevity I’ll force this column to be int.
  • ref_table_j is the table itself. Each table is defined by ref_table_j (ref_id_j INT, ref_val_j SOME_DATA_TYPE)
  • ref_val_j is the value (or “name”) column from each table j.
  • ref_input_j is the user input, usually supplied from PHP or something else.

It will take some time to understand all this due to my bad English, but I think the code can be studied more easily due to the fact that the code does not belong to any human language — they are just symbols derived from English.

Internship, Day 25 (24 May 2012)

Posted on May 25th, 2012 in computer, in English, in Thai, personal, PHP, programming, SQL | No Comments »


(Photo: Finally got this bun!)

วันนี้ผมก็ทำงานต่อจากเมื่อวานเฉยๆ ครับ ไม่ค่อยมีอะไรมาก แต่สิ่งที่เป็นปัญหาอย่างหนักในการทำงานคือฐานข้อมูลที่ไม่มี Foreign Key เลย แม้ว่าเราจะหาความสัมพันธ์ได้จาก query ต่างๆ ที่มี แต่เป็นเรื่องยากพอสมควรเมื่อเทียบกับการทำฐานข้อมูลให้ชัดเจนตั้งแต่แรก และไม่ทราบว่าตรงไหนเป็นบั๊กเพราะไม่ทราบตั้งแต่แรกว่าโปรแกรมควรจะทำงานอย่างไร ทำให้ผมได้แต่จัดการกับ error message ที่ขึ้นมาชัดๆ เท่านั้น (เปิดจาก log ก็ไม่ได้เพราะเซิร์ฟเวอร์ไม่มีการเก็บ error_log ไว้เลย)

I continued on yesterday’s work and not much else. The big problem exists here again because there are no foreign keys at all in the tables I work with. Even though it’s possible to infer from the other queries, it’s still harder than reading a documented or properly FK’d database. Additionally, I didn’t know which part is bugged because I didn’t know how the program is supposed to work, so I could only fix the real, visible errors. (No logs because the server doesn’t have error_log configured.)

Bonus

By the way, this:

 SELECT x,y
FROM A,B
WHERE A.x = B.y; 

is as fast as:

 SELECT x,y
FROM A
  INNER JOIN B ON A.x = B.y; 

Reference: http://stackoverflow.com/questions/121631/inner-join-vs-where