Está en la página 1de 42

L Nguyn Sinh HNUE

Cch

thc thi cc truy vn SQL Cc truy vn lm vic vi cc CSDL Cc truy vn thao tc vi bng Cc truy vn thao tc vi bn ghi

Cch

1: Truy vn thng qua PHPMyAdmin Cch 2: Truy vn qua MySQL Console Cch 3: Truy vn t xa thng qua cc cng c MySQL Client

M Bt

ca s phpmyadmin: ca s SQL cu lnh SQL v bm OK.

http://localhost/phpmyadmin Biu tng ca s SQL nm bn cnh biu tng

Home ca phpmyadmin
Nhp

M ca s Command Start->Run->cmd[Enter] Chuyn ti th mc cha file mysql.exe Cd c:\xampp\mysql\bin Gi file mysql vi 2 tham s username v

password:
Nhp

mt khu tng ng vi user Nhp cc truy vn SQL v bm Enter thc thi

Mysql u root p[Enter]

php:

Show databases;

C V

php:

Create database tn_CSDL;

d:

Create database vidu;


Ch

: to CSDL h tr ting Vit vi charset mc nh l UTF8:


CREATE DATABASE Tn_CSDL

DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

C V

php:

Drop database tn_CSDL;

d:

Drop database vidu;

php:

ALTER DATABASE Tn_CSDL


DEFAULT CHARACTER SET utf8 COLLATE tn_charset

php: :

Use tn_CSDL;
Ch

Ch dng trn MySQL Console Khng c tc dng trn phpmyadmin

Cc

ni dung chnh:

Xem danh sch bng trong CSDL To bng Chnh sa cu trc bng

To/xo trng ch s (index)


Xo bng

C V

php:

Show tables [in database] [like %tukhoa%]

d:

Show tables; Show tables in vidu; Show tables in vidu like %lop%;

php:

CREATE TABLE tn_bng ( Tn_trng_1 kiu_d_liu [( di)] [Tham s], Tn_trng_2 kiu_d_liu [( di)] [Tham s], Tn_trng_3 kiu_d_liu [( di)] [Tham s],

V d: Create table baihat (id int auto_increment primary key, name varchar(20))

php:

SHOW COLUMNS FROM tn_bng;

php:

ALTER TABLE tn_bng ADD COLUMN tn_ct

kiu_d_liu [Thng tin khc]


V

d:

Alter table bai_hat add column tac_gia varchar

(15);

php:

ALTER TABLE tn_bng CHANGE tn_ct_c

tn_ct_mi kiu_d_liu [Thng tin b sung]


V

d:

Alter table bai_hat change tac_gia tacgia varchar

(20);

php:

ALTER TABLE tn_bng DROP tn_ct;

php:

ALTER TABLE tn_bng ADD INDEX (tn_ct);

php:

ALTER TABLE tn_bng DROP INDEX tn ct

index

php:

ALTER TABLE tn_c RENAME TO tn_mi

php:

DROP TABLE tn_bng;

php: :

INSERT INTO tn_bng (trng1, trng2,) values

(gi tr1, gi tr 2)
Ch

S lng v kiu d liu ca cc gi tr phi bng s

lng v kiu d liu ca cc trng tng ng i vi cc gi tr dng chui hoc ngy thng, thi gian th cn phi bao quanh gi tr bi cp du nhy n, kiu s th khng cn i vi trng auto increment th khng cn khai bo tn trng cng nh gi tr tng ng.

php:

DELETE FROM tn_bng WHERE iu_kin

php:

UPDATE tn_bng SET ct1=gi tr1, ct 2= gi

tr 2 WHERE iu_kin

php c bn:

Select * from tn_bng Select * from tn_bng where iu_kin Select trng1, trng2, from tn_bng where

iu_kin

cc php so snh tr v true/false C th kt hp nhiu iu kin qua cc php ton and hoc or Cc iu kin c bn:
L

Cc

iu kin phc hp: s dng kt hp vi cc hm hoc cc cu truy vn lng.

So snh n gin In, not in Like Between and

L cc php ton so snh c bn: =, <>, >, <, <=,

>= V d:
Select * from nhanvien where luongcoban<3;

c s dng kim tra gi tr c nm trong

mt tp hp cho trc hay khng: Tr v true nu nh ct tm kim c gi tr nm trong (in) hoc khng nm trong (not in) tp gi tr cho trc V d:
Ly danh sch cc nhn vin c id nm trong tp 2,3,4:
Select * from nhanvien where id in (2,3,4)

Ly danh sch nhn vin c id nm ngoi tp 2,3,4:


Select * from nhanvien where id not in (2,3,4)

c dng tm kim theo t kho vi cc

trng chui. C php chung:


LIKE %t_kho% LIKE %t_kho LIKE t_kho%

Tr v cc bn ghi c cha cc t kho cc v

tr tng ng. V d:
Select * from nhanvien where hoten like %Hng;

Tr v cc bn ghi c gi tr ca trng nm gia

hai gi tr xc nh C php:
Between x and y

V d: Select * from nhanvien where manhanvien between 2 and 20;

S V

dng t kho order by sau where:

Order by trng_1 asc|desc trng_2 asc|desc

Select * from nhanvien where hoten like %Hng

order by ngaysinh desc

dng loi b cc bn ghi c cc gi tr trng nhau C php:


Select distinct trng1, trng 2 from tn_bng
V

d:

Select distinct hoten, ngaysinh from nhanvien

dng gom nhm v loi b cc bn ghi trng nhau C th kt hp vi cc hm khc C php:


Select * from tn_bng where iu_kin group by
V d: Ly danh sch cc bn ghi c user_id=1 v nhm theo

tn_trng

trng user_id (ch ra duy nht 1 bn ghi): SELECT * from user_group where user_id=1 group by user_id

Cho

php thit lp iu kin thay v dng where C th kt hp vi group by C php: having iu_kin V d:
Select * from users having id>10

php: limit x, y

X: Bn ghi bt u Y: S bn ghi cn ly
V

d:

Select * from users limit 4,20;

Select distinct From Where Group

by Having Order by Limit

Cch 1: Truy vn trc tip Cch 2: S dng php ton JOIN


2 ON iu_kin_ni WHERE cc iu kin khc
Select user.name, group.name from user inner join group

Select trng1, trng2,.. From bng1, bng 2


SELECT bng1.trng1, bng2.trng2, bng1.trng2 FROM bng1 LEFT JOIN|INNER JOIN| RIGHT JOIN bng

V d:

on group.user_id=user.id where user.name like %abc%

php t tn trng, bng bng b danh (alias) Gip cho truy vn ngn gn hn Gim thiu s nhp nhng gia cc trng trng tn trn cc bng khc nhau S dng: tn trng|bng AS b_danh V d:
Cho

Select a.name as username, b.name as

groupname from user as a inner join group as b on a.user_id=b.id where a.name like %abc%

php s dng mt cu lnh SQL trong mt cu lnh SQL khc Cu lnh SQL con phi tr kt qu ph hp vi ng cnh s dng C th dng cu lnh SQL con nh l mt bng o V d:
Cho

Select ten from (select * from users where id>4) as

bangmoi Select * from users where id in (select user_id from groups where group_id=4)

php ni nhiu cu lnh SQL li vi nhau cho ra mt tp bn ghi tr v duy nht C php:
Cho

: S lng v kiu d liu ca cc trng trong cc truy vn sql1, sql2, sql3 phi tng xng nhau VD:
Ch

Sql1 UNION sql2 UNION sql3

Select ten from users UNION select name from

groups.

Calificar