Định dạng SQL online
Trình bày câu lệnh SQL với thụt lề thống nhất, từ khoá viết hoa cho MySQL, PostgreSQL, SQL Server, Oracle, BigQuery… hoặc thu gọn trên một dòng.
Kết quả
Bấm “Chạy” để xem kết quả.
API định dạng SQL viết lại câu truy vấn mỗi mệnh đề một dòng, danh sách cột và phép JOIN được thụt lề, từ khoá, kiểu dữ liệu và hàm được viết hoa hoặc thường thống nhất (keyword_case upper, lower hoặc preserve). Chọn dialect để cú pháp được hiểu đúng: mysql, mariadb, postgresql, sqlite, tsql (SQL Server), plsql (Oracle), bigquery, snowflake, redshift, spark, db2, trino, clickhouse hoặc sql chuẩn.
Tinh chỉnh bố cục với indent (1–8 dấu cách) hoặc tabs=true, expression_width (độ dài tối đa của biểu thức trong ngoặc trên một dòng), logical_operator_newline (before hoặc after AND / OR) và lines_between_queries cho script nhiều câu lệnh. Chuỗi ký tự và comment được giữ nguyên.
minify=true làm ngược lại: bỏ comment và gộp khoảng trắng bên ngoài dấu nháy, tiện cho log hay chuỗi cấu hình. Câu lệnh không phân tích được sẽ trả lỗi invalid_sql kèm vị trí lỗi.
Dùng trong code của bạn
curl -X POST "https://apibox.io.vn/v1/sql-format" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"sql":"select u.id, u.name, count(o.id) as orders from users u left join orders o on o.user_id = u.id where u.city = '\''Hà Nội'\'' and o.total > 100000 group by u.id, u.name order by orders desc limit 10","dialect":"postgresql"}'const res = await fetch('https://apibox.io.vn/v1/sql-format', {
method: 'POST',
headers: {
Authorization: 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"sql": "select u.id, u.name, count(o.id) as orders from users u left join orders o on o.user_id = u.id where u.city = 'Hà Nội' and o.total > 100000 group by u.id, u.name order by orders desc limit 10",
"dialect": "postgresql"
}),
});
const data = await res.json();
console.log(data);import requests
res = requests.post(
"https://apibox.io.vn/v1/sql-format",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={"sql": "select u.id, u.name, count(o.id) as orders from users u left join orders o on o.user_id = u.id where u.city = 'Hà Nội' and o.total > 100000 group by u.id, u.name order by orders desc limit 10", "dialect": "postgresql"},
timeout=30,
)
print(res.json())<?php
$ch = curl_init('https://apibox.io.vn/v1/sql-format');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => ['Authorization: Bearer YOUR_API_KEY', 'Content-Type: application/json'],
CURLOPT_POSTFIELDS => '{"sql":"select u.id, u.name, count(o.id) as orders from users u left join orders o on o.user_id = u.id where u.city = \'Hà Nội\' and o.total > 100000 group by u.id, u.name order by orders desc limit 10","dialect":"postgresql"}',
]);
$response = curl_exec($ch);
print_r(json_decode($response, true));Tài liệu API → Nhận API key miễn phí
Hỏi đáp
API có kiểm tra SQL đúng không?
Chỉ kiểm tra cú pháp ở mức cần cho việc định dạng; tên bảng, tên cột không được đối chiếu với cơ sở dữ liệu.
Comment có được giữ không?
Có khi định dạng. minify=true sẽ xoá comment.
Nên chọn dialect nào?
Đúng hệ quản trị CSDL bạn dùng; điều này ảnh hưởng tới cách đặt tên trong backtick, [ngoặc vuông] và các từ khoá riêng. sql dùng được cho truy vấn chuẩn.