Sunteți pe pagina 1din 13

CFBC

SGBD

Grigore Vasilache

Cuprins
TEMA NR. 13.LIMBAJUL MYSQL. NOIUNI GENERALE ........................................................................ 1
1.1.

Cuvintele cheie (rezervate) n MySQL. ................................................................................................... 1

1.2.

Tipuri de date. .......................................................................................................................................... 3

1.2.1.

Tipuri de date NUMERICE .............................................................................................................. 3

1.2.2.

Tipuri de dateDATE CALENDARISTICE ...................................................................................... 3

1.2.3.

Tipuri de date IRURI DE CARACTERE ....................................................................................... 4

1.2.4.

Tipul de dateENUMERARE ............................................................................................................ 5

1.2.5.

Tipul de dateMULIME................................................................................................................... 5

1.3.

Necesarul de memorie:............................................................................................................................. 6

1.4.

Instruciuni de baz .................................................................................................................................. 6

1.4.1.

Crearea i tergerea Bazei de date .................................................................................................... 6

1.4.2.

Crearea i tergerea tabelelpor .......................................................................................................... 7

1.4.3.

Schimbarea structurii unui tabel existent .......................................................................................... 8

1.4.4.

Schimbarea numelui unui tabel ........................................................................................................ 9

1.4.5.

Crearea i tergerea indexilor ......................................................................................................... 10

1.4.6.

Instruciunile de activare................................................................................................................. 10

1.4.6.1.

USE ......................................................................................................................................... 10

1.4.6.2.

DESCRIBE .............................................................................................................................. 10

1.4.6.3.

BEGIN/COMMIT/ROLLBACK............................................................................................. 10

TEMA NR. 13. PRACTIC ............................................................................................................................. 12

TEMA NR. 13.LIMBAJUL MYSQL. NOIUNI GENERALE


1.1. Cuvintele cheie (rezervate) n MySQL.
ADD
ANALYZE
ASC
BIGINT
BOTH
CASE
CHARACTER
COLUMN

ALL
AND
BEFORE
BINARY
BY
CHANGE
CHECK
COLUMNS
Pagina1din13

ALTER
AS
BETWEEN
BLOB
CASCADE
CHAR
COLLATE
CONSTRAINT
Capitolul 13

CFBC
CONVERT
CURRENT_DATE
CURRENT_USER
DAY_HOUR
DAY_SECOND
DEFAULT
DESC
DISTINCTROW
DROP
ENCLOSED
EXPLAIN
FLOAT
FOR
FROM
GROUP
HOUR_MICROSECOND
IF
INDEX
INSERT
INT2
INT8
INTO
KEY
LEADING
LIMIT
LOCALTIME
LONG
LOW_PRIORITY
MEDIUMINT
MINUTE_MICROSECOND
NATURAL
NULL
OPTIMIZE
OR
OUTFILE
PRIVILEGES
RAID0
REFERENCES
REPLACE
REVOKE
SECOND_MICROSECOND
SET
SONAME
SQL_CALC_FOUND_ROWS
STARTING
TABLES
TINYBLOB
TO

SGBD
CREATE
CURRENT_TIME
DATABASE
DAY_MICROSECOND
DEC
DELAYED
DESCRIBE
DIV
DUAL
ESCAPED
FALSE
FLOAT4
FORCE
FULLTEXT
HAVING
HOUR_MINUTE
IGNORE
INFILE
INT
INT3
INTEGER
IS
KEYS
LEFT
LINES
LOCALTIMESTAMP
LONGBLOB
MATCH
MEDIUMTEXT
MINUTE_SECOND
NOT
NUMERIC
OPTION
ORDER
PRECISION
PROCEDURE
READ
REGEXP
REQUIRE
RIGHT
SELECT
SHOW
SPATIAL
SQL_SMALL_RESULT
STRAIGHT_JOIN
TERMINATED
TINYINT
TRAILING

Pagina2din13

Grigore Vasilache
CROSS
CURRENT_TIMESTAMP
DATABASES
DAY_MINUTE
DECIMAL
DELETE
DISTINCT
DOUBLE
ELSE
EXISTS
FIELDS
FLOAT8
FOREIGN
GRANT
HIGH_PRIORITY
HOUR_SECOND
IN
INNER
INT1
INT4
INTERVAL
JOIN
KILL
LIKE
LOAD
LOCK
LONGTEXT
MEDIUMBLOB
MIDDLEINT
MOD
NO_WRITE_TO_BINLOG
ON
OPTIONALLY
OUTER
PRIMARY
PURGE
REAL
RENAME
RESTRICT
RLIKE
SEPARATOR
SMALLINT
SQL_BIG_RESULT
SSL
TABLE
THEN
TINYTEXT
TRUE
Capitolul 13

CFBC
UNION
UNSIGNED
USE
UTC_TIME
VARBINARY
VARYING
WITH
XOR

SGBD
UNIQUE
UPDATE
USING
UTC_TIMESTAMP
VARCHAR
WHEN
WRITE
YEAR_MONTH

Grigore Vasilache
UNLOCK
USAGE
UTC_DATE
VALUES
VARCHARACTER
WHERE
ZEROFILL

1.2. Tipuri de date.


1.2.1.

Tipuri de date NUMERICE

Fixe: NUMERIC, DECIMAL, INTEGER i SMALLINT i aproximative: FLOAT,


REAL i DOUBLE PRECISION. INT- sinonim pentru INTEGER, dar DEC pentru
DECIMAL.
salary DECIMAL(5,2)Valori de la -99,99 pn la 99,99 (999,99).
DECIMAL(p) este echivalent cu DECIMAL(p,0) i echivalent DECIMAL.
INT(4)- se vor indica 4 cifre, dac se indic atributul ZEROFILL, poziiile libere se
completeaz cu zero. (INT(5) ZEROFILL, valoarea 4 se va afia 00004.
Tip
TINYINT
SMALLINT
MEDIUMINT
INT
BIGINT
1.2.2.

Octei
1
2
3
4
8

De la
-128
-32768
-8388608
-2147483648
-9223372036854775808

La
127
32767
8388607
2147483647
9223372036854775807

Tipuri de dateDATE CALENDARISTICE

DATETIME, DATE, TIMESTAMP, TIME i YEAR. Fiecare din ele poate avea
valorile respective dar i valoarea zero, care se folosete, cnd se introduce valoare
inadmisibil. Datele se introduc n formatul AN-LUN-DAT
Tipul
DATETIME
DATE
TIME
YEAR

Valoarea zero
'0000-00-00 00:00:00'
'0000-00-00'
'00:00:00'
0000
Pagina3din13

Capitolul 13

CFBC

SGBD

TIMESTAMP

Grigore Vasilache

00000000000000
(lungimea depinde de numrul de simboluri la ieire)

DATETIME cu formatul 'YYYY-MM-DD HH:MM:SS' are domeniul de la '1000-01-01


00:00:00' la '9999-12-31 23:59:59'.
DATE- 'YYYY-MM-DD''1000-01-01' - '9999-12-31'.
Tipul
TIMESTAMP(14)
TIMESTAMP(10)
TIMESTAMP(6)
TIMESTAMP(12)
TIMESTAMP(8)
TIMESTAMP(4)
TIMESTAMP(2)

Formatul
YYYYMMDDHHMMSS
YYMMDDHHMM
YYMMDD
YYMMDDHHMMSS
YYYYMMDD
YYMM
YY

Datele 'YYYY-MM-DD HH:MM:SS' sau 'YY-MM-DD HH:MM:SS' pot fi introduse:


'98-12-31 11:30:45', '98.12.31 11+30+45', '98/12/31 11*30*45' sau '98@12@31 11^30^45'.
'YYYY-MM-DD' sau'YY-MM-DD' - '98-12-31', '98.12.31', '98/12/31', '98@12@31'
'YYYYMMDDHHMMSS' sau 'YYMMDDHHMMSS' - '19970523091528' sau '970523091528'
se subnelege '1997-05-23 09:15:28', dar '971122129015' este inadmisibil, fiindc valoarea
pentru minute este absurd i va primi valoarea '0000-00-00 00:00:00'.
TIME 'HH:MM:SS' sau 'HHH:MM:SS' are domeniul '-838:59:59' pn '838:59:59'.
HHMMSS, 101112 - '10:11:12'.
YEAR YYYY valori de la1901 pn la 2155. Variante: '1901' pn la '2155', ca numr din 4
cifre 1901- 2155, ca ir '00'- '99' sau numere de 2 cifre. n acest caz valorile de la '00' pn la '69'
se transform n2000- 20692000 i de la '70' la '99' n 1970- 1999.
1.2.3.

Tipuri de date IRURI DE CARACTERE

CHAR, VARCHAR, BLOB, TEXT.

Tipul
TINYTEXT sau TINYBLOB
TEXT sau BLOB
MEDIUMTEXT sau MEDIUMBLOB
Pagina4din13

Mrimea Max
2^8-1
2^16-1 (64K-1)
2^24-1 (16M-1)

Octei
255
65535
16777215
Capitolul 13

CFBC

SGBD

LONGBLOB

2^32-1 (4G-1)

Grigore Vasilache

4294967295

CHAR i VARCHAR sunt asemntoare, dar se deosebesc prin modul de pstrare iafiare.
n CHAR lungimea cmpului este constant, cea indicat la creare, dela 1(0) la 255caractere.
La salvare la dreapta se adaug spaii.
VARCHAR iruri de lungimi arbitrare (1 255) dar se pstreaz numai cte caractere are +
1 octet pentru pstrarea lungimii.
Mrimea
''
'ab'
'abcd'
'abcdefgh'

CHAR(4)
''
'ab'
'abcd'
'abcd'

Necesar memorie
4 octei
4 octei
4 octei
4 octei

VARCHAR(4)
''
'ab'
'abcd'
'abcd'

Necesar memorie
1 octet
3 octei
5 octei
5 octei

Dac la creare nu a fost indicat atributul BINARY la sortare i la comparare nu se va


ine cont de registru
TINYBLOB, BLOB, MEDIUMBLOB i LONGBLOB pentru texte mari
TINYTEXT, TEXT, MEDIUMTEXT i LONGTEXTDiferena dintre ele este c la TEXT nu
ine cont de registru.
Tipul TEXT poate fi considerat ca VARCHAR de dimensiuni nelimitate i BLOB ca
VARCHAR BINARY.
Cmpurile de tipurile BLOB i TEXT pot fi indexate numai n versiuni MySQL 3.23.2 i mai
noi.
Nu poate fi folosit atributul DEFAULT.
1.2.4.

Tipul de dateENUMERARE

ENUM
ENUM("unu", "doi", "trei")
Mrimea Index Mrimea Index Mrimea Index Mrimea Index Mrimea Index
NULL
NULL ""
0
"Irina"
1
"Vasile" 2
"Tatiana" 3
Maximum 65535 de elemente.Sortarea dup ordinea din list.
1.2.5.

Tipul de dateMULIME

SET
SET("toc", "creion")NOT NULL poate primi valorile: "", "toc", "creion",
"toc,creion". Poate avea maximum 64 de elemente.
Dac avem SET("a","b","c","d"), atunci "a,d", "d,a", i "d,a,a,d,d" este ca "a,d".
Pagina5din13

Capitolul 13

CFBC

SGBD

Grigore Vasilache

1.3. Necesarul de memorie:


Tipul
FLOAT(X)
FLOAT
DOUBLE
DOUBLE PRECISION
DECIMAL(M,D)
REAL
NUMERIC(M,D)
DATE
TIMESTAMP
DATETIME
TIME
YEAR
CHAR(M)
VARCHAR(M)
TINYBLOB, TINYTEXT
BLOB, TEXT
MEDIUMBLOB, MEDIUMTEXT
LONGBLOB, LONGTEXT
ENUM('value1','value2',...)
SET('value1','value2',...)

Octei
4, dacX <= 24 or 8, dac 25 <= X <= 53
4
8
8
M+2, dac D > 0, M+1, dac D = 0 (D+2, dac M < D)
8
M+2, dac D > 0, M+1, dac D = 0 (D+2, dac M < D)
3
4
8
3
1
M, 1 <= M <= 255
L+1, unde L <= M i 1 <= M <= 255
L+1,L < 2^8
L+2,L < 2^16
L+3,L < 2^24
L+4,L < 2^32
1 sau 2, n dependen de numrul mrimilor enumerate
65535)
1, 2, 3, 4 sau 8, n dependen de numrul de elemente a mu
(max. 64)

1.4. Instruciuni de baz


1.4.1.

Crearea i tergerea Bazei de date

CREATE DATABASE

CREATE DATABASE [IF NOT EXISTS] db_name


Dac BD exist i nu este indicat parametrul IF NOT EXISTS, va fi eroare.
BD n MySQL sunt realizate ca mape, ce coninfiiere, ce reprezint tabelele.
Fiecare tbli are cte 3 fiiere cu extensiile.MYD, MYI i.frm.
Pagina6din13

Capitolul 13

CFBC

SGBD

Grigore Vasilache

!!!DROP DATABASE

DROP DATABASE [IF EXISTS] db_name terge toate tabelele din BD.
DROP DATABASE terge toate fiierele cu extensiile:.BAK,.DAT,.HSH,.ISD,.ISM,.ISM,
1.4.2.

Crearea i tergerea tabelelpor

CREATE TABLE

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]


[table_options] [select_statement]
Sau
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name LIKE old_table_name;
create_definition:
col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
[PRIMARY KEY] [reference_definition]
sauPRIMARY KEY (index_col_name,...)
sauKEY [index_name] (index_col_name,...)
sauINDEX [index_name] (index_col_name,...)
sauUNIQUE [INDEX] [index_name] (index_col_name,...)
sauFULLTEXT [INDEX] [index_name] (index_col_name,...)
sau[CONSTRAINT symbol] FOREIGN KEY [index_name] (index_col_name,...)
[reference_definition]
sauCHECK (expr)
type:TINYINT[(length)] [UNSIGNED] [ZEROFILL]
sauSMALLINT[(length)] [UNSIGNED] [ZEROFILL]
sauMEDIUMINT[(length)] [UNSIGNED] [ZEROFILL]
sauINT[(length)] [UNSIGNED] [ZEROFILL]
sauINTEGER[(length)] [UNSIGNED] [ZEROFILL]
sauBIGINT[(length)] [UNSIGNED] [ZEROFILL]
sauREAL[(length,decimals)] [UNSIGNED] [ZEROFILL]
sauDOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL]
sauFLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL]
sauDECIMAL(length,decimals) [UNSIGNED] [ZEROFILL]
sauNUMERIC(length,decimals) [UNSIGNED] [ZEROFILL]
sauCHAR(length) [BINARY]
sauVARCHAR(length) [BINARY]
sauDATEsau TIMEsau TIMESTAMP sau DATETIME sau TINYBLOB sau BLOB sau
MEDIUMBLOB sau LONGBLOB sau TINYTEXT sau TEXT sau MEDIUMTEXT sau
LONGTEXT sau ENUM(value1,value2,value3,...) sau SET(value1,value2,value3,...)
index_col_name:
Pagina7din13

Capitolul 13

CFBC

SGBD

Grigore Vasilache

col_name [(length)]
reference_definition:
REFERENCES tbl_name [(index_col_name,...)]
[MATCH FULL | MATCH PARTIAL]
[ON DELETE reference_option]
[ON UPDATE reference_option]
reference_option:
RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT
table_options:
TYPE = {BDB | HEAP | ISAM | InnoDB | MERGE | MRG_MYISAM | MYISAM }
sauAUTO_INCREMENT = # m sau AVG_ROW_LENGTH = # sau CHECKSUM = {0 | 1}
sauCOMMENT = "string" sauMAX_ROWS = # sauMIN_ROWS = # sau PACK_KEYS = {0
| 1 | DEFAULT} sau PASSWORD = "string" sau DELAY_KEY_WRITE = {0 | 1}
sau ROW_FORMAT= { default | dynamic | fixed | compressed }
sau RAID_TYPE= {1 | STRIPED | RAID0 } RAID_CHUNKS=#RAID_CHUNKSIZE=#
sau UNION = (table_name,[table_name...])
sau INSERT_METHOD= {NO | FIRST | LAST }
sau DATA DIRECTORY="Calea deplin"
sauINDEX DIRECTORY=" Calea deplin "
select_statement:
[IGNORE | REPLACE] SELECT...(orice expresie corect SELECT)
Cmpul numeric poate avea atributul AUTO_INCREMENT. ntr-un tabel numai un cmp poate
fi cu acest atribut.
DEFAULT trebuie s fie o constant.
DROP TABLE DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name,...] [RESTRICT |
CASCADE]
DROP TABLE terge una sau mai multe tblie

1.4.3.

Schimbarea structurii unui tabel existent

ALTER TABLE

ALTER [IGNORE] TABLE tbl_name alter_spec [, alter_spec...]


alter_specification:
ADD [COLUMN] create_definition [FIRST | AFTER column_name ]
sau ADD [COLUMN] (create_definition, create_definition,...)
sau ADD INDEX [index_name] (index_col_name,...)
Pagina8din13

Capitolul 13

CFBC

SGBD

Grigore Vasilache

sau ADD PRIMARY KEY (index_col_name,...)


sau ADD UNIQUE [index_name] (index_col_name,...)
sau ADD FULLTEXT [index_name] (index_col_name,...)
sau ADD [CONSTRAINT symbol] FOREIGN KEY [index_name] (index_col_name,...)
[reference_definition]
sau ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}
sau CHANGE [COLUMN] old_col_name create_definition
[FIRST | AFTER column_name]
sau MODIFY [COLUMN] create_definition [FIRST | AFTER column_name]
sau DROP [COLUMN] col_name sau DROP PRIMARY KEY sau DROP INDEX
index_name
sau DISABLE KEYS sau ENABLE KEYS sau RENAME [TO] new_tbl_name sau ORDER
BY col sau table_options
mysql>ALTER TABLE t1 CHANGE a b INTEGER;
mysql>ALTER TABLE t1 CHANGE b BIGINT NOT NULL;
mysql>ALTER TABLE t1 MODIFY b BIGINT NOT NULL;
mysql>CREATE TABLE t1 (a INTEGER,b CHAR(10));
mysql>ALTER TABLE t1 RENAME t2;
Pentru a modifica tipul din INTEGER peTINYINT NOT NULL (cu acelai nume) i
de a schimba tipul cmpului b din CHAR(10) pe CHAR(20) redenumindu-l c:
mysql>ALTER TABLE t2 MODIFY a TINYINT NOT NULL, CHANGE b c
CHAR(20);
Pentru a aduga un cmp TIMESTAMP cu numele d:
mysql>ALTER TABLE t2 ADD d TIMESTAMP;
Pentru a aduga index la cmpul d i de indicat la acest cmp cheia primar:
mysql>ALTER TABLE t2 ADD INDEX (d), ADD PRIMARY KEY (d);
Pentru a terge cmpul c:
mysql>ALTER TABLE t2 DROP COLUMN c;
Pentru a aduga un cmp nou numeric AUTO_INCREMENT cu numele c:
mysql>ALTER TABLE t2 ADD c INT UNSIGNED NOT NULL AUTO_INCREMENT,
ADD INDEX (c);
Cmpurile indexate nu pot fi NULL.
1.4.4.

Schimbarea numelui unui tabel

RENAME TABLE
RENAME TABLE tbl_name TO new_tbl_name[, tbl_name2 TO new_tbl_name2,...]
Pagina9din13

Capitolul 13

CFBC

1.4.5.

SGBD

Grigore Vasilache

Crearea i tergerea indexilor

CREATE INDEX

CREATE [UNIQUE|FULLTEXT] INDEX index_name


ON tbl_name (col_name[(length)],... )
mysql>CREATE INDEX part_of_name ON customer (name(10));
DROP INDEX

DROP INDEX index_name ON tbl_name


1.4.6.

Instruciunile de activare

1.4.6.1. USE
Instruciunea USE db_name- de folosit BD cu numele db_name n urmtoarele cereri prin
convenie. Aceast BD va fi activ pn la sfritul seansului de lucru sau pn la activarea
altei BD cu USE. Dac se folosesc date i din alte BD atunci adresarea la acele cmpuri se
face cu indicarea BD:mysql>USE db1;
mysql>SELECT author_name,editor_name FROM author,db2.editor
WHERE author.editor_id = db2.editor.editor_id;
mysql>USE db1;
mysql>SELECT COUNT(*) FROM mytable; # selects from db1.mytable
mysql>USE db2;
mysql>SELECT COUNT(*) FROM mytable; # selects from db2.mytable
mysql>USE db1;
mysql>SELECT author_name,editor_name FROM author,db2.editor
WHERE author.editor_id = db2.editor.editor_id;
1.4.6.2. DESCRIBE
{DESCRIBE | DESC} tbl_name [col_name | wild] afiarea informaiei despre coloanele
tabelului. Analog comenzii SHOW COLUMNS FROM.
1.4.6.3. BEGIN/COMMIT/ROLLBACK
Prin convenie MySQL funcioneaz n regim autocommit.Aceastanseamn c la
modificarea datelor automat se nscriu pe disc.
SET AUTOCOMMIT=0 deconecteaz regimul autocommit.
Pentru a nscrie modificrile pe disc trebuie de folosit comanda COMMIT sau comanda
ROLLBACK, care ne va permite s ignorm modificrile, efectuate de la nceputul acestei
tranzacii.
Pagina10din13

Capitolul 13

CFBC

SGBD

Grigore Vasilache

Select * from nume_tabel Se afieaz datele tabelului.

Pagina11din13

Capitolul 13

CFBC

SGBD

Grigore Vasilache

TEMA NR. 13. PRACTIC


1. De creat baza de date cu numeleProba_111_adugai cifra grupei Dvoastr.
2. Determinai tipurile posibile icreai tabelele:
ELEVI
ID_Elev

Nume

GRUPELE
ID_Grupa

Prenume

Grupa

SECTII
ID_Sectia

ID_Grupa

Anul_Nasterii

ID_Sectia

Sectia

3. De completat aceste tabele:


ID_Sectia
I
C
F

ID_Grupa
1
2
3
4
5

ID_Elev
1
2
3
4
5
6

Sectia
Informatica
Contabilitate
Financiar - Bancara

Grupa
INF0807G
INF0808G
INF0809G
CI0801G
FP0904G

Nume
Cau
Vrtosu
Costa
Pelivan
Vieru
Chirnev

ID_Sectia
I
I
I
C
F

Prenume
Gheorghe
Grigore
Ion
Tudor
Grigore
Maria
Pagina12din13

ID_Grupa
1
2
5
4
2
5

Anul_Nasterii
25.02.1993
12.05.1994
12.05.1992
21.11.1992
23.12.1992
28.07.1992

Capitolul 13

CFBC

7
8
9
10

SGBD

Postu
Negru
Vlas
Leca

Ion
Pavel
Victoria
Valeria

Grigore Vasilache

3
4
5
3

15.08.1992
12.09.1992
25.04.1992
27.04.1992

4. Pornind de la premisa c cu ajutorul instruciunii SELECT putem afia datele


tabelelor organizai afiarea datelor din tabele;
5. ncercai s afiai datele care satisfac unul sau mai multe criterii.

Pagina13din13

Capitolul 13