Wednesday, May 24, 2017

SQL COUNT(), AVG() and SUM() Functions

The SQL COUNT(), AVG() and SUM() Functions

The COUNT() function returns the number of rows that matches a specified criteria.
The AVG() function returns the average value of a numeric column.
The SUM() function returns the total sum of a numeric column.

COUNT() Syntax

SELECT COUNT(column_name)
FROM table_name
WHERE condition;

AVG() Syntax

SELECT AVG(column_name)
FROM table_name
WHERE condition;

SUM() Syntax

SELECT SUM(column_name)
FROM table_name
WHERE condition;

How to get first character of a string in SQL?

LEFT(colName, 1) will also do this, also. It's equivalent to SUBSTRING(colName, 1, 1).
I like LEFT, since I find it a bit cleaner, but really, there's no difference either way.

converts the value of a field to uppercase.

The UCASE() Function

The UCASE() function converts the value of a field to uppercase.

SQL UCASE() Syntax

SELECT UCASE(column_name) FROM table_name;

Syntax for SQL Server

SELECT UPPER(column_name) FROM table_name;

Tuesday, May 23, 2017

converts the value of a field to lowercase.

The LCASE() Function

The LCASE() function converts the value of a field to lowercase.

SQL LCASE() Syntax

SELECT LCASE(column_name) FROM table_name;

Syntax for SQL Server

SELECT LOWER(column_name) FROM table_name;

SQL CONCATENATE (appending strings to one another) (cộng các string)

String concatenation means to append one string to the end of another string. SQL allows us to concatenate strings but the syntax varies according to which database system you are using. Concatenation can be used to join strings from different sources including column values, literal strings, output from user defined functions or scalar sub queries etc.
SQL Server and Microsoft Access use the + operator. The example below appends the value in the FirstName column with ' ' and then appends the value from the LastName column to this. The resulting string is given an Alias of FullName so we can easily identify it in our resultset.
-- SQL Server / Microsoft Access
SELECT FirstName + ' ' + LastName As FullName FROM Customers
Oracle uses the CONCAT(string1, string2) function or the || operator. The Oracle CONCAT function can only take two strings so the above example would not be possible as there are three strings to be joined (FirstName, ' ' and LastName). To achieve this in Oracle we would need to use the || operator which is equivalent to the + string concatenation operator in SQL Server / Access.
-- Oracle
SELECT FirstName || ' ' || LastName As FullName FROM Customers
MySQL uses the CONCAT(string1, string2, string3...) function. The above example would appear as follows in MySQL
-- MySQL
SELECT CONCAT(FirstName, ' ', LastName) As FullName FROM Customers

Conclusion

In this article we have seen how to append strings to one another using string concatention functions provided in SQL. We hope you will find many uses for using these string functions in your databases.

MySQL

Mọi câu truy vấn: mặc định là case insensitive

Hướng dẫn cài đặt và cấu hình MySQL Community

1- Giới thiệu
2- Sơ lược về các phiên bản của MySQL
3- Download MySQL
3.1- Download: MySQL Community Server (GPL)
3.2- Kết quả download
4- Cài đặt
4.1- Cài đặt các thư viện đòi hỏi
4.2- Cài đặt MySQL Community
5- Cấu hình MySQL
6- Sử dụng MySQL Workbench
7- Hướng dẫn học SQL với MySQL

1- Giới thiệu

Tài liệu này được viết dựa trên:
  • Window 7 (64bit)

  • MySQL Community 5.6.21

2- Sơ lược về các phiên bản của MySQL

Có 2 phiên bản MySQL:
  • MySQL Cummunity
  • MySQL Enterprise Edition
MySQL Cummunity: Là  phiên bản miễn phí. (Chúng ta sẽ cài đặt phiên bản này).
MySQL Enterprise Edition: Là phiên bản thương mại.

3- Download MySQL

Chúng ta sẽ download và sử dụng gói MySQL miễn phí.
  • MySQL Community Server
MySQL Community, sau khi download và cài đặt đầy đủ sẽ bao gồm các phần như hình minh họa dưới đây.
Trong đó có 2 cái quan trọng nhất là:
  1. MySQL Server
  2. MySQL Workbench   (Công cụ trực quan để học và làm việc với MySQL)
Trong đó MySQL Workbench đòi hỏi phải cài đặt trước 2 thư viện mở rộng: Vì vậy bạn phải download 2 thư viện này về và cài đặt trước khi bắt đầu cài SQL Community.
Để download MySQL Community, vào địa chỉ:

3.1- Download: MySQL Community Server (GPL)

3.2- Kết quả download

4- Cài đặt

4.1- Cài đặt các thư viện đòi hỏi

Trước hết bạn phải cài đặt 2 thư viện mở rộng như đã nói ở trên.

4.2- Cài đặt MySQL Community

Chọn cài đặt tất cả, bao gồm cả các Database ví dụ (Cho mục đích học tập).
Bước này bộ cài đặt kiểm tra các thư viện đòi hỏi.  Nó thông báo thiếu:
  • Visual Studio Tools for Office ... & Python 3.4.
Tuy nhiên có thể bỏ qua (vì không quan trọng)
Bộ cài hiển thị danh sách các gói sẽ được cài vào.
Bộ cài đặt tiếp tục tới phần cấu hình MySQL Server.
Tiếp tục cấu hình database ví dụ:
Nhập vào password và nhấn Check để kiểm tra việc kết nối với MySQL.
Nhấn Finish để hoàn thành cài đặt.

5- Cấu hình MySQL

Việc kết nối vào MySQL từ một máy khác có thể bị chặn lại. Bạn cần phải cấu hình cho phép máy tính khác kết nối vào MySQL.
Mục tiêu chúng ta là gán quyền truy cập vào MySQL cho một user từ bất cứ một địa chỉ IP nào.
?
1
2
3
4
5
6
-- Cú pháp là:
GRANT ALL ON *.* to myuser@'%' IDENTIFIED BY 'mypassword';
 
-- Ví dụ gán quyền truy cập vào User root, từ bất cứ một địa chỉ IP nào.
-- Chú ý: root là user có sẵn sau khi cài đặt MySQL.
GRANT ALL ON *.* to root@'%' IDENTIFIED BY '1234';
Việc cấp quyền thành công.

6- Sử dụng MySQL Workbench

Mở MySQL Workbench:
Hình ảnh MySQL Workbench với một vài cơ sở dữ liệu mẫu.
Chúng ta tạo một cơ sở dữ liệu riêng với tên: mytestdb.
Sét đặt SCHEMA này là mặc định, để làm việc.
Tiếp theo tạo một bảng, và trèn một dòng dữ liệu vào bảng đó.
?
1
2
3
4
5
6
-- Tạo bảng
Create table My_Table (ID int(11), Name Char(64)) ;
 
-- Trèn một dòng dữ liệu vào bảng.
Insert into My_Table (id,name)
values (1, 'Tom Cat');

7- Hướng dẫn học SQL với MySQL

Bạn có thể xem tiếp tài liệu hướng dẫn học MySQL tại đây: