Kinh Dịch, Tử Vi, Phong Thủy, Đông y Thiết kế website chuyên nghiệp
  • Tài khoản
  • Mật khẩu
 
Reply to this topicStart new topicStart Poll

MySQL Commands

mothoidenho2002
post Oct 25 2003, 06:37 AM
Gửi vào: #1


Bé mới sinh
*

Nhóm: Thành Viên Mới
Bài viết: 4
Tham gia: 15-October 03

Thành viên thứ.: 1,707
Tài Khoản: $ 1500



In this article, I assumed that you can connect to mySQL server. I am about to describe some commands which are commonly used in MySQL.

MySQL is a open source database which is very world wide. THe language i am using here is English, because i want to preserve the meaning of the so called "techincal meaning".
    Connect to and Disconnect from server
In order to connect to MySQL server, you are supposed to have a user name and password. If you are remotely connecting to server, certainly you have to have the hostname
Once you know all of them, you can connect to mySQL server by using this command:

mysql -u [username] -p
Enter password: *********

If that information you entered are correct, you should see something like this:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 459 to server version: 3.22.12a-log

Type 'help' for help.

mysql>


That prompt tells you that mysql is ready for your commands.

In order to quit from mySQL typing QUIT at that mysql> prompt.
mysql>quit
Bye


You can also disconnect by pressing Control-D
    Entering queries

This section will be describing the very basic principles of entering command or queries.

Here is the simple command that asks the server to tell you its version number and the current date. Typing it in as shown following the mysql> prompt and press enter.

mysql>SELECT VERSION(), CURRENT_DATE;

You will see a table that looks like:

|--------------|--------------------|
| VERIONS() CURRENT_DATE
|--------------|--------------------|
|3.22.20a-log 1999-03-19
|-----------------------------------|
1 row in set (0.01 sec)


There are some conlcusions about the commands in mySQL
1. Commands must be end with a semicolon ( ; ). There were, however, some commands that don't need to be ended by ;, we will reach to them later.
2. mysql displays the result as a format of a table.
3. mysql does not concern whether the command is uppercase or lowercase. The following commands are the same:
SELECt VErsion(), CUrrent Date;
seLECT verSIon(), Current dATE;
select version(), current date;

Here is another query. It shows that you can use mysql as a simple calculator:
[b]mysql>select sin(pi/4), (4+1)*5;[/b]

You will see something like this:
|---------------|-------------|
SIN(pi/4) (4+1)*5
|-----------------------------|
0.707107 25
|-----------------------------|

You can even enter multiple commands on a single line. Just end each one with a semicolon ( ; ).
mysql> SELECT VERSION(); SELECT NOW();

A command need not to be given all on a single line, so leghthy commands that require serveral lines are not a problem. mysql determines where your statement ends by looking for the terminating semicolon ( ; ). You can also do like this: (press enter after each line)
mysql> SELECT
->user()
->,
-> current date;[/i]

If you don't want to execute a command, cancel it by typing [i]\c
:
mysql> SELECT
-> USER()
-> \c
mysql>


In mysql, there are some prompts that contain their own meaning:
mysql> Ready for your command
-> Ready for next line of multiple line command
'> Ready for next line, collecting a string that begins with '
"> Ready for next line, collecting a string that begines with "

    Creating and Using Database


In order to know what databases now you own, type this command:
mysql> SHOW DATABASES;
As the result, you will see what datebases are under your control now.
If you are an adminstrator, you can access all databases from any users as well as an important database whose name is mysql. That databases contain every information about users, host, privileges, databases......
If you are not an adminstrator, simply a user, you only can access your own databases and one databases whose name is test.

Now, I assume you are a user. you can create database and the number of datebases you can create is depend on your plan of hosting. If you buy a plan of 3 Mysql, you can create 3 databases. By that way, you buy a plan of 30 Mysql, you can create up to 3 databases. Certainly, you are only accessible to your own databases not to others' databases.

In order to create databases, typing this command:
mysql> create database [data name];

You want to test the existence of that database, use SHOW command again, you will see your created database.
In order to use a datase so that you can apply others commands, you have to type:
mysql>use [data name];

Once when you use a datbase you can show tables from that datbase by using this command:
mysql>show tables;

Top
User is offlinePMMini ProfileEmail Poster
Quote Post
langtucodoc
post Oct 25 2003, 11:19 AM
Gửi vào: #2


Thượng Tá
*****

Nhóm: Thành Viên Ưu Tú
Bài viết: 863
Tham gia: 4-February 03
Đến từ: Bốn Phương Là Nhà

Thành viên thứ.: 29
Tài Khoản: $ 114733



Đây là các lệnh liên quan đến hệ thống của MySQL, không phải là lệnh để thao tác trên dữ liệu. Còn nữa không vậy bạn???


--------------------
Người quân tử tranh cãi để tìm chân lý. Kẻ tiểu nhân tranh cãi để giành hơn thua.
Chỉ là cá vàng trong bể...Dẫu cho khốn cùng chẳng nể mấy ai...

-----------------------------------------------------------------
Tài sản hiện đang sở hữu:

  
Top
User is offlinePMMini ProfileEmail Poster
Quote Post
spykid
post Oct 25 2003, 11:57 AM
Gửi vào: #3


SOLARIS
***

Nhóm: Thành Viên Ưu Tú
Bài viết: 236
Tham gia: 24-June 03
Đến từ: CA

Thành viên thứ.: 552
Tài Khoản: $ 23186



of course la còn nữa rùi!?!?!? hih1i1hi
thông cảm nhen....bạn tui hơi bận ...cho nênn chỉ post được dzị thui!?!?!?
will be back soon!?!?!?!?!


--------------------
user posted image

YOU'LL NEVER KNOW WHAT CAN YOU DO UNTIL YOU TRY
SHOW-OFF always SHOW UP in a SHOWDOWN
<----- Remember this EVERYBODY
Top
User is offlinePMMini ProfileEmail Poster
Quote Post
mothoidenho2002
post Oct 25 2003, 12:20 PM
Gửi vào: #4


Bé mới sinh
*

Nhóm: Thành Viên Mới
Bài viết: 4
Tham gia: 15-October 03

Thành viên thứ.: 1,707
Tài Khoản: $ 1500



hi bạn...
Đó là những commands dùng cho hệ thốn mysql...
Muốn dùng những command đó..bạn phải connect or telnet vao server ...or dùng một chương trình tên là SSH....
Khi telnet vào được máy chủ...bạn phải biết..Mysqlserver đạ ở thư mọuc nào..
ví dụ /usr/local/mysql
bạn phải cd vào mysql/bin...thì mới dùng được những command nói trên/1!?!?
Ngòai...ra...cũng còn có những commands cho database only....
Bạn thông cảm?..mình hơi busy...mình sẽ post bài lên sau
Top
User is offlinePMMini ProfileEmail Poster
Quote Post
javandk
post Oct 25 2003, 12:21 PM
Gửi vào: #5


Trung Úy
****

Nhóm: Thành Viên Ưu Tú
Bài viết: 386
Tham gia: 25-June 03
Đến từ: Senior Developer

Thành viên thứ.: 562
Tài Khoản: $ 33030



Post như vậy, newbie hiểu sao nổi.
Nếu nó về các câu lệnh hệ thống trong SQL Server thì còn nhiều lắm, nhưng thực tế ít khi nào xài đến.
Trọng tâm vẫn là Select, Rule, Trigger & Store Procedure thui.
Thân !


--------------------

Dong Kim Software Technology & Advertising Co., Ltd
- Web Design
- E-Commerce Solutions (use DongKimPortal System)
- Graphic Design
- Offset Printing
- Digital Jet Printing
- Art Frame
Contact:
- Nguyen Dong Khoa
- Tel: 0908.472.476
- Email: javandk@yahoo.com | dongkimcoltd@yahoo.com.vn
- Website: www.dongkim.net | www.westcom.com.vn

Top
User is offlinePMMini ProfileEmail Poster
Quote Post
mothoidenho2002
post Oct 26 2003, 02:21 AM
Gửi vào: #6


Bé mới sinh
*

Nhóm: Thành Viên Mới
Bài viết: 4
Tham gia: 15-October 03

Thành viên thứ.: 1,707
Tài Khoản: $ 1500



There are some commands which should be used for the database:

Create ans select a database:

mysql>create database [database_name];

mysql>show databases;

Create table:
A Database must contain a lot of tables. Certainly, a table must contain some kinds of fields. Each of fields must be a kind of values.

mysql> create table pet (name VARCHAR(20), owner VARCHAR(20),
-> species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);

Table;s name is pet
The fields' name are: name, owner, species, sex, birth, death
The kinds of values are attached to each fields are VARCHAR, CHAR, DATE.
You should make some choices for values of the fields. If you make such a bad choice, mysql provides command of ALTER TABLE to change it. We will deal with it later.

After completing all of those commands, you can check the existence of your table:
mysql>show tables;

You also can check the fields and their values:
mysql>describe pet;

If everything is all right, you should see a table describing the values of each fields.

Retrieve inforamation from a table

The SELECT statement is commonly used to get information from a table. The syntax is:
SELECT what_you_want_to_select
FROM which_table
WHERE conditions_to_satisfy


For example, I am going to take table whose name is ibf_members from ktcn forum. It has some fields which are: id, name, mgroup, password, email, joined, ipaddress...

mysql>select * from ibf_members;
you will select all date from that table.

mysql>select * from ibf_members where name = "spykid";
you will select all data from a particular row of spykid.

mysql>select * from ibf_members where (........) AND (........);
You can combine conditions by that command.

Boolean operators are AND, OR.
Operators: >=, <=, +, -, *

mysql>select name, id from ibf_members;
You can select particular columns.

mysql>select namd, id from ibf_members order by id;
You can use this command to order the id column. Usually, the order is ASCENDING. You can reverse that order by this command:
mysql>select name, id, from ibf_members order by id DESC;

to be continued!!!!........................

Bài viết đã được chỉnh sửa nội dung bởi spykid vào lúc Oct 26 2003, 03:12 AM
Top
User is offlinePMMini ProfileEmail Poster
Quote Post
BabyWolf
post Oct 26 2003, 03:50 AM
Gửi vào: #7


Đứa con của Rừng Xanh
*******

Nhóm: Webmaster
Bài viết: 2,005
Tham gia: 28-January 03
Đến từ: Rừng già Amazon

Thành viên thứ.: 1
Tài Khoản: $ 2147483647



Okie! Bài hướng dẫn trên rất cụ thể và chi tiết, mỗi lệnh được giải thích rất cặn kẻ, chỉ có đều là bằng tiếng Anh nên các bạn không rành tiếng Anh hơi bị khó khăn.

QUOTE
Create table:
A Database must contain a lot of tables. Certainly, a table must contain some kinds of fields. Each of fields must be a kind of values.

mysql> create table pet (name VARCHAR(20), owner VARCHAR(20),
-> species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
Table;s name is pet
The fields' name are: name, owner, species, sex, birth, death
The kinds of values are attached to each fields are VARCHAR, CHAR, DATE.


Ở phần create table này bạn sẽ có thể thắc mắc những chữ như VARCHAR(20), CHAR(1), DATE,... là gì?? Đây chính là kiểu dữ liệu lưu trữ. Trong SQL được chia làm 3 loại kiểu dữ liệu cơ bản (có thể nói là 3 group) đó là: số (number), ngày giờ (date & time), string (chuỗi ký tự). Tùy theo dạng dữ liệu lưu trữ mà bạn có thể chọn kiễu dữ liệu cho phù hợp. Trong mỗi group thì lại được chia nhỏ ra thành nhiều kiểu dữ liệu với nhiều tính năng lưu trữ khác nhau.

Sau đây tôi xin giới thiệu với các bạn các kiểu dữ liệu:

A> NUMBER GROUP:
1. Các kiểu biểu diễn số nguyên:

* TINYINT: đây là kiểu dữ liệu có kích thước nhỏ nhất.
- Tầm vực biểu diễn giá trị: - 128 đến 127.
Ví dụ: Tôi muốn tạo một table lưu trữ điểm của sinh viên bao gồm tên, điểm (vì điểm nếu tính theo hệ 10 thì chỉ nằm trong khoảng 0 đến 10 nên dùng kiểu dữ liệu này là hợp lý).

CODE
CREATE TABLE diemsv (Ten VARCHAR(32),Diem TINYINT(2))


Các bạn có thể thắc mắc là số 2 được đặt trong () là gì??? Đó chính là độ rộng (width) của giá trị được thể hiện. Vì từ 0 đến 10 thì tối đa cũng chỉ dùng 2 ký tự để biểu diễn.

Tương tự VARCHAR(32) tức là cho biết cột Ten có độ rộng thể hiện tối đa là 32 ký tự.
Riêng phần này tôi nói kỹ, nhưng từ kiểu dữ liệu trở về sau sẽ nói tóm tắt.

* BIT - BOOL - BOOLEAN :

3 Kiểu này tương đương với kiểu dữ liệu TINYINT(1) và chỉ lưu trữ các ký tự 0 và 1.

* SMALLINT:
- Giá trị: -32768 đến 32767.

* MEDIUMINT: -8388608 đến 8388607
* INT - INTEGER: -2147483648 đến 2147483647
* BIGINT: -9223372036854775808 đến 9223372036854775807 (lớn quá 04.gif )
Thông thường chúng ta không nên sử dụng kiểu dữ liệu này để lưu một con số quá lớn. Trong một vài trường hợp có thể dẫn đến phép tính của bạn bị sai. Mặt khác lại tốn bộ nhớ. Vì vậy giải pháp sẽ là lưu dưới dạng chuỗi ký tự, sau đó chúng ta sẽ dùng các function để convert từ chuỗi ký tự sang số để tính toán.

2. Các kiểu biểu diễn số thập phân:
Đối với các kiểu này thì có thể khai báo khác với ở trên. Ví dụ:
CODE
FLOAT(W,D)

- với W là độ rộng như ở trên.
- D là số ký tự phần thập phân.

Ví dụ: để khai báo một cột chỉ chứa các số dạng như 1,2 - 123,4 - 44,5 - 3,004 thì sẽ khai báo như sau:
CODE
FLOAT(5,3)


* FLOAT: -3.402823466E+38 đến -1.175494351E-38, 0, và 1.175494351E-38 đến 3.402823466E+38.

* DOUBLE: -1.7976931348623157E+308 đến -2.2250738585072014E-308, 0, và 2.2250738585072014E-308 đến 1.7976931348623157E+308

* REAL: Tương tự DOUBLE.
* DECIMAL: Một kiểu số không bao gồm phần dấu thập . Kiểu này được lưu trữ tương tự như một cột có dữ liệu với kiểu CHAR: nghĩa là số được lưu trữ như là một chuỗi, sử dụng một ký tự cho mỗi ký số của giá trị. Những dấu biểu thị trong con số như , - sẽ không được tính vào độ rộng biểu diễn dữ liệu ở đây. Kiểu dữ liệu này có giá trị lớn nhất tương đương với DOUBLE, nhưng nó linh động hơn vì tùy thuộc vào giá trị biểu diễn phần thập phân và độ rộng (width) của dữ liệu. Nếu lúc khai báo mà bạn không khai báo 2 giá trị W & D thì W sẽ mặc định là 10, D sẽ là 0.

* DEC - NUMERIC - FIXED: Tương tự DECIMAL, riêng FIXED chỉ có trong MySQL version 4.1.0.

Xin tạm dừng tại đây. Những kiểu dữ liệu khác sẽ trình bày ở những bài viết kế tiếp. Thân!

Bài viết đã được chỉnh sửa nội dung bởi BabyWolf vào lúc Oct 26 2003, 03:54 AM


--------------------
Gặp bất kỳ người nào tôi cũng tìm được cái để học từ họ
Liên hệ:
- YM: ktcn_net
- My Blog: http://360.yahoo.com/ktcn_net
Thiết kế website chuyên nghiệp - Quảng bá website - Cung cấp tên miền - Lưu trữ web - hosting

-----------------------------------------------------------------
Tài sản hiện đang sở hữu:

        
Top
User is offlinePMMini ProfileEmail Poster
Quote Post
spykid
post Oct 26 2003, 06:25 AM
Gửi vào: #8


SOLARIS
***

Nhóm: Thành Viên Ưu Tú
Bài viết: 236
Tham gia: 24-June 03
Đến từ: CA

Thành viên thứ.: 552
Tài Khoản: $ 23186



Next....
i am going to introduce some commands which can be applied for searching through the database:

To find names beginning with "b":
mysql>select * from ibf_members where name LIKE "b%";

To find names ending with "b":
mysql>select * from ibf-members where name LIKE "%b";

To find names containing a "b":
mysql>select * from ibf_members where name LIKE "%b%";

To find names containing exactly n characters, use the _ n times
mysql>select * from ibf_members where name LIKE "_____";
here i am using 5 _ ...so that the result should be the names containing 5 characters.

To count the number of rows;
mysql>select count(*) from ibf_members;

An example: let's say you need to know how many posts for each members. You need to know some informations, such as, the table for posting is ibf_posts, the field for the name is author_name. Typing this command:
mysql>select author_name, count(*) from ibf_posts group by author_name

There are some more functions for statement SELECT, We will deal with them later.
to be continued...............

Bài viết đã được chỉnh sửa nội dung bởi BabyWolf vào lúc Oct 26 2003, 12:35 PM


--------------------
user posted image

YOU'LL NEVER KNOW WHAT CAN YOU DO UNTIL YOU TRY
SHOW-OFF always SHOW UP in a SHOWDOWN
<----- Remember this EVERYBODY
Top
User is offlinePMMini ProfileEmail Poster
Quote Post
 

1 Thành viên đang xem chủ đề (1 khách và 0 người giấu mặt)
0 Thành viên:

Reply to this topicTopic OptionsStart new topic

 


Quảng cáo: Muaban | Quảng bá website | Thiết kế website | Kệ bếp gỗ | Nội thất Lỗ Ban

Bản rút gọn: MySQL Commands
Time is now: 8th January 2009 - 05:39 PM