Database

You are currently browsing the articles from KomKid.Net matching the category Database.

Determing SQL Server Database and Table Size

Example 1 (SQL Server table)

Run the following SQL statement from Teratrax Database Manager, Query Analyzer, or SQL Server Management Studio. Replace the names in bold with your own:

USE db1
GO
EXEC sp_spaceused N'dbo.orders'
GO

Results
* name: Table name for which space usage information was requested
* rows: Number of rows existing in the table
* reserved: Total amount of reserved space for table data and indexes
* data: Amount of space used by table data
* index_size: Amount of space used by table indexes
* unused: Total amount of space reserved for table but no yet used

Example 2 (SQL Server database)

You can also run sp_spaceused without any parameters to display information about the whole database. Replace the names in bold with your own:

USE db1
GO
EXEC sp_spaceused
GO

Results
First Recordset:
* database_name: Name of the current database
* database_size: Size of the current database in megabytes. database_size includes both data and log files
* unallocated space: Space in the database that has not been reserved for database objects

Second Recordset:
* reserved: Total amount of space allocated by objects in the database
* data: Total amount of space used by data
* index_size: Total amount of space used by indexes
* unused: Total amount of space reserved for objects in the database, but not yet used

From : http://www.teratrax.com/articles/table_size_sp_spaceused.html

Written by Komkid on September 1st, 2009 with no comments.
Read more articles on Admin and Database.

Delete all record in SQL Server table

TRUNCATE TABLE name

vs

DELETE FROM TABLE name

TRUNCATE TABLE is functionally identical to DELETE statement with no WHERE clause: both remove all rows in the table. But TRUNCATE TABLE is faster and uses fewer system and transaction log resources than DELETE.

The DELETE statement removes rows one at a time and records an entry in the transaction log for each deleted row. TRUNCATE TABLE removes the data by deallocating the data pages used to store the table’s data, and only the page deallocations are recorded in the transaction log.

TRUNCATE TABLE removes all rows from a table, but the table structure and its columns, constraints, indexes and so on remain. The counter used by an identity for new rows is reset to the seed for the column. If you want to retain the identity counter, use DELETE instead. If you want to remove table definition and its data, use the DROP TABLE statement.

You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint; instead, use DELETE statement without a WHERE clause. Because TRUNCATE TABLE is not logged, it cannot activate a trigger.

TRUNCATE TABLE may not be used on tables participating in an indexed view.

Written by Komkid on August 18th, 2009 with no comments.
Read more articles on Database.

Convert ASCII to Unicode

แปลง ASCII ให้เป็น Unicode

พอดีได้รู้หลักการทำ SEO (Search engine optimization) จากกูรูหลายสาขาว่า ให้ทำ Web Directory ก็เลยลองทำดูที่ http://directory.komkid.net

หลังจากปรับแต่งแล้วก็สร้าง Category ไปได้ซัก 20 กว่าตัว ก็รู้สึกว่า มันไม่ใช่ นี่มันไม่ใช่ผม… ต้องมานั่งทำทีละอันอย่างนี้ ไม่ใช่แล้ว ยัดใส่ database ด้วย SQL เลยดีกว่า

เริ่มด้วย Excel คือ copy Category ชาวบ้านเค้ามา บวกกับดู table category ของเราแล้วใส่สูตรเพื่อสร้างคำสั่ง SQL ดังรูป

excel_for_sql

พอลอง run ดูปรากฏว่าไม่สำเร็จ เพราะ database เราเก็บเป็นแบบ Unicode นี่เอง

phpmyadmin_sql

ถ้าอย่างงั้นก็ต้องเปลี่ยน ASCII ของเราให้เป็น Unicode ก่อนยัดลง database ซึ่งไหน ๆ ก็ ไหน ๆ แล้ว สร้าง tools ไว้ใช้ถาวรซะเลย โดยสร้าง html ขึ้นมาไฟล์นึง โดยสร้าง form ไว้ ตาม code

1
2
3
4
5
6
7
8
9
10
11
12
13
<form name=”form1″ action=">
<br />
<b>Input Text:</b><br />
<BR><textarea name=”inputtext” rows=10cols=50″></textarea>
<BR><input type=”button” value=”Convert to Unicode” onclick=”Ascii2Unicode()/>
<input type=”button” value=”Convert to ASCII” onclick=”Unicode2Ascii()/>
<br />
Output:<BR>
<span id=”output”></span>
<BR><textarea name=”outputtext” rows=10cols=50″></textarea>
<br /><br /><br />
<input type=”reset” value=Clearonclick=”document.getElementById(’output’).innerHTML = ”” />
</form>

ได้หน้าตาตามรูป
ascii_to_unicode_form
ส่วน Java Script สำหรับทำงานก็ตามนี้

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function Ascii2Unicode(){
  if(document.forms[0].inputtext.value != ''){
     var strInput = document.forms[0].inputtext.value;
     var strOutput = toUnicode(strInput);
     document.forms[0].outputtext.value = strOutput;
  }
}
function Unicode2Ascii(){
  if(document.forms[0].inputtext.value != ''){
     var strInput = document.forms[0].inputtext.value;
     var strOutput = toAscii(strInput);
     document.getElementById("output").innerHTML =strOutput;
  }
}
function toUnicode(strInput) {
  result = '';
  for (i=0; i<strInput.length; i++) {
    if(strInput.charCodeAt(i) == 10)
        result += '\n';
    else
        result += '&#' + strInput.charCodeAt(i) + ';';
  }
  return result;
}
function toAscii(strInput) {
     replace = '';
      var text = escape(strInput);
      if(text.indexOf('%0D%0A') > -1){
            replace = /%0D%0A/g ;
      }else if(text.indexOf('%0A') > -1){
            replace = /%0A/g ;
      }else if(text.indexOf('%0D') > -1){
            replace = /%0D/g ;
      }
      return unescape( text.replace(replace,'<br />') );
}

ผลที่ได้ก็หน้าตาแบบนี้
ascii_to_unicode_result
โดยลองได้ ที่นี่
พอได้ก็เอาไปใส่ใน Excel ได้ SQL มาก็เอาไป run ใน Database โลด (ประหยัดเวลาได้มิใช่น้อย)

Written by admin on December 3rd, 2008 with 2 comments.
Read more articles on Database and Internet and Programming.

Get record count for each table in a database

อยากรู้ว่าแต่ละ table ใน database ของ MS SQL Server มีกี่ record แล้วเนี่ย จะทำอย่างไรดีน้อ
วิธีแรก

1
SELECT Count(*)

แต่ถ้ามีหลาย 100 table ล่ะ จะทำที่ละ table คงจะไม่ไหว

วิธีที่สอง Store Procedure

1
exec sp_msforeachtable @command1="SELECT '?' AS TableName, COUNT(*) as TblRowCount FROM ? ORDER BY TableName"

แต่ก็นานอยู่ดีกว่าจะได้

วิธีที่สาม เป็นวิธีใช้ประยุกต์ใช้ System Table ให้เกิดประโยชน์ครับ (มีเค้าอยู่ก็เอามาใช้ประโยชน์หน่อย)

1
2
3
4
5
6
7
8
9
ELECT CONVERT(char(10), t.TABLE_SCHEMA) AS Owner,
CONVERT(char(25), t.TABLE_NAME) AS 'TABLE Name',
MAX(i.[rows]) AS 'Record COUNT'

FROM sysindexes i INNER JOIN INFORMATION_SCHEMA.TABLES t ON OBJECT_NAME(i.id) = t.TABLE_NAME

WHERE (t.TABLE_TYPE = 'BASE TABLE')
GROUP BY t.TABLE_SCHEMA, t.TABLE_NAME
ORDER BY [Record COUNT] DESC

Written by admin on October 6th, 2008 with no comments.
Read more articles on Database and Programming.

PHP และ MySQL กับการแสดงผลภาษาไทย

หลังจาก upgrade version ก็เกิดปัญหาการแสดงผลภาษาไทย
ตายละวา Database เราเจ๊งรึป่าวน้อ

ไม่นะ! ภาษาอังกฤษ ยังแสดงผลได้นี่นา
พอเปิดดู Database ก็โล่งอก เฮ้อ ยังอยู่ดีจริง ๆ ด้วย

แสดงว่าปัญหาน่าจะอยู่ที่การแสดงผล
ใช่จริง ๆ ด้วย Firefox มันแสดงผลเป็น UTF-8 นี่เอง

งั้นลองเปลี่ยนเป็น TIS-620 ซิ

โธ่เอ๊ย แค่นี้เอง จิ๊บ ๆ

แต่หลังจากนั้นก็มีโทรศัพท์มาทั้งวัน
“น้อง web มีปัญหารึป่าวเนี่ย อ่านไม่ออกเลย”
“เปล่าครับพี่ แต่ต้องเปลี่ยน Encoding ครับ ทำแบบนี้ครับ …”

โอ้ว บ่อยครั้งอย่างนี้สายัณห์รับไม่ได้ครับ หาวิธีแก้ดีกว่า google google นั่นไงล่ะ เจอเพียบ
บ้างก็บอกให้แก้ที่ MySQL ( http://gotoknow.org/blog/krunapon/69399 )
- เปิด my.ini ขึ้นมาโดยกดที่ Start -> run พิมพ์ my.ini แล้วกด Enter
ในส่วนของ [client] ให้เพิ่ม

1
default-character-set = tis620

ในส่วนของ [mysqld] หลัง database directory ให้เพิ่ม

1
2
3
4
5
default-character-set = tis620
character-set-server = tis620
collation-server = tis620_thai_ci
init_connect = 'SET collation_connection = tis620_thai_ci'
init_connect = 'SET NAMES tis620'

จากนั้น Restart MySQL
บ้างก็บอกให้แก้ที่ PHP

1
2
3
4
5
6
7
8
9
10
mysql_select_db($DatabaseName,$Conn) or die ("ไม่สามารถติดต่อกับฐานข้อมูลได้");

$cs1 = "SET character_set_results=tis620";
mysql_query($cs1) or die('Error query: ' . mysql_error());

$cs2 = "SET character_set_client = tis620";
mysql_query($cs2) or die('Error query: ' . mysql_error());

$cs3 = "SET character_set_connection = tis620";
mysql_query($cs3) or die('Error query: ' . mysql_error());

ทำไมของเราไม่ work หว่า

หลังจากลองอยู่พักใหญ่ ก็ต้องตัดใจ ใช้วิธีลูกทุ่ง ตรงไปตรงมา

1
header('Content-type: text/html; charset=tis620');

แต่มันต้องทำทุก file นี่สิ ใครมีวิธีดี ๆ ชัวร์ ๆ ช่วยบอกด้วยนะครับ

Written by admin on July 31st, 2008 with no comments.
Read more articles on Admin and Database and Programming.