Website Development Prices

Search Blog

Saturday, May 9, 2015

Objasnjenje - Izrada tabela baze podataka (Explanation - Creating database tables)

create table kupci
( customerid int unsigned not null auto_increment primary key,
ime char(50) not null,
adresa char(100) not null,
grad char(30) not null
);


Kada pravite novu tabelu morate da izaberete tipove podataka za nju.

Tabela Kupci ima 4 kolone. Prva kolona SifraKupca, primarni je kljuc. Tip podatka je celobrojni (int) i kolona ne prihvata negativne vrednosti - unsigned. U svim ostalim kolonama vrednost ce biti znakovnog tipa char (polja imaju fiksnu sirinu navedenu u zagradama). 

Char tip podatka uvek dodeljuje 50 znakova za ime, cak i ako se ime sastoji od manje znakova. MySQL ce taj podataka dopuniti razmacima do odgovarajuce velicine.

Postoji i drugi tip podatka, a to je varcharVarchar tip podatka zauzima samo stvrana broj bajtova (plus jedan 1 bajt). 

Varchar zauzima manje prostora, a char se brze ucitava.

create table porudzbine
( brojporudzbine int unsigned not null auto_increment primary key,
sifrakupca int unsigned not null,
iznos float(6,2),
date date not null
);


Kolona Iznos treba da sadrzi realne brojeve s pokretnim zarezom, tipa float.  Kolona Date sadrzi datume tipa date. 

create table motori
( brojmotora char(13) not null primary key,
proizvodjac char(50),
imemotora char(100),
cena float(4,2)
);


U tabeli Motori ne treba da pravimo vestacki kljuc zato sto se brojevi BrojMotora dodeljuju s drugog mesta. U ostalim poljima prihvatlljiva je vrednost NULL zato sto prodavnica moze da zna BrojMotora pre nego sto sazna ImeMotora, Proizvodjac i Cenu.

create table stvari_porudzbina
( brojporudzbine int unsigned not null,
brojmotora char(13) not null,
kolicina tinyint unsigned,

primary key (brojporudzbine, brojmotora)

);


Tabela StvariPorudzbina pokazuje kako se prave primarni kljucevi s vise kolona. Broj kupljenih motora belezi se u u koloni Kolicina, tipa TINYINT UNSIGNED kao celobrojna vrednost od 0 do 255. 

Primarni kljucevi s vise kolona definisu se rezervisanom reci PRIMARY KEY, a koju smo upotrebili za ovu tabelu (StvariPorudzbina).  

create table pregled_motora
( brojmotora char(13) not null primary key,
pregled text

);


Tip podatka text koristi se za duze tekstove, poput clanaka. 

create table customers
( customerid int unsigned not null auto_increment primary key,
name char(50) not null,
address char(100) not null,
city char(30) not null
);

When you create a new table you must select data types for it.

Customers table has 4 columns. The first column CustomerID, is the primary key. The data type is whole integer (int) and column does not accept negative values - unsigned. In all other columns value will be the data type char (fields have a fixed width indicated in parentheses).

Char data type always allocates 50 characters for the name, even if the name is composed of fewer characters. MySQL will supplement with the data spaces to the right size.

There is a second type of data, which is varchar. Varchar data type occupies only real number of bytes (plus one 1 byte).

Varchar takes less space, and char will load faster.

create table orders
( ordersid int unsigned not null auto_increment primary key,
customerid int unsigned not null,
amount float(6,2),
date date not null
);

The column Amount should contain real numbers with floating point, data type float. Date column contains dates of data type date.

create table motobikes
( motobikeid char(13) not null primary key,
manufacturer char(50),
motobike_name char(100),
price float(4,2)
);


Table Motobikes we don't need to make an artificial key because the numbers, motobikeID are added from second place. In other fields acceptable is value a NULL because the store can know MotobikeID before knowing MotobikeName, Manufacturer and Price.

create table orders_items
( ordersid int unsigned not null,
motobikeid char(13) not null,
quantity tinyint unsigned,

primary key (ordersid, motobikeid)

);


Table OrdersItems shows how to create a primary key with more columns. Number of purchased motobikes is registered in column Quantity, type TINYINT UNSIGNED as whole integer value from 0 to 255.

Primary keys with more columns are defined with the reserved word PRIMARY KEY, and which was used for this table (OrdersItems).

create table motobike_review
(motobikeid char(13) not null primary key,
review text

);


The data type text is used for longer texts, such as articles.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.