Skrypty SQL

Skrypty SQL generujące bazę danych przedstawioną na rysunku 30 pokazano poniżej. Fragment kodu tworzący bazę danych o nazwie poligrafia:

CREATE DATABASE 'poligrafia’; USE 'poligrafia’;

Fragment kodu tworzący tabelę order, zawierającą atrybuty: orderid, order_name, user_id, count, status, price, itemdata, productid gdzie order id jest kluczem głównym i żaden z atrybutów, oprócz product id, nie może posiadać wartości NULL. Wartość atrybutu orderid jest unikalna i generowana automatycznie:

CREATE TABLE 'order’ (

'order_id’ int(11) NOT NULL auto_increment,

'order_name’ varchar(255) NOT NULL default 'user_id’ int(1) NOT NULL default '0′,

'count’ int(11) NOT NULL default '0′,

'status’ int(11) NOT NULL default '0′,

'price’ varchar(255) NOT NULL default ”,

'itemdata’ text NOT NULL,

'product_id’ int(11) default NULL,

PRIMARY KEY (’order_id’),

KEY 'user id’ (’user id’),

KEY 'product_id’ (’product_id’)

) TYPE=InnoDB ROW FORMAT=DYNAMIC AUTO INCREMENT=5 ;

Fragment kodu tworzący tabelę order_options, zawierającą atrybuty: option_id, order_id, atrybuty option idi orderiddomyślnie posiadają wartość NULL:

CREATE TABLE 'orders_options’ (

'option_id’ int(11) default NULL,

'order_id’ int(11) default NULL,

KEY 'option_id’ (’option_id’),

KEY 'order_id’ (’order_id’)

) TYPE=InnoDB ROW FORMAT=DYNAMIC;

Fragment kodu tworzący tabelę products, zawierającą atrybuty: id, name, price, discount, fields, description, template, sample. Kluczem głównym jest id i żaden z atrybutów nie może posiadać wartości NULL. Wartość atrybutu id jest unikalna i generowana automatycznie:

CREATE TABLE 'products’ (

'id’ int(11) NOT NULL auto_increment,

'name’ varchar(255) NOT NULL default ”,

'price’ float NOT NULL default '0′,

'discount’ text NOT NULL,

'fields’ text NOT NULL,

'description’ text NOT NULL,

'template’ varchar(255) NOT NULL default ”,

'sample’ varchar(255) NOT NULL default ”,

PRIMARY KEY (’id’),

KEY 'price’ (’price’)

) TYPE=InnoDB ROW FORMAT=DYNAMIC AUTO INCREMENT=2 ;

Fragment kodu tworzący tabelę users, zawierającą atrybuty: id, type, name, password, address, email, company. Kluczem głównym jest id i żaden z atrybutów nie może posiadać wartości NULL, Wartość atrybutu id jest unikalna i generowana automatycznie, a klucz email musi być unikalny:

CREATE TABLE 'users’ (

'id’ int(11) NOT NULL auto_increment,

'type’ varchar(32) NOT NULL default 'user’,

'name’ varchar(255) NOT NULL default ”,

'password’ varchar(255) NOT NULL default 'address’ varchar(255) NOT NULL default ’email’ varchar(255) NOT NULL default 'company’ varchar(255) NOT NULL default PRIMARY KEY (’id’),

UNIQUE KEY 'user_email_uniq’ (’email’)

) TYPE=InnoDB ROW FORMAT=DYNAMIC AUTO INCREMENT=2 ;

Fragment kodu tworzący tabelę reports, zawierającą atrybuty: id, user_id, report_data, operation_type, date_completed, date steted, odrer id. Kluczem głównym jest id i żaden z atrybutów nie może posiadać wartości NULL. Wartość atrybutu id jest unikalna i generowana automatycznie:

CREATE TABLE 'reports’ (

'id’ int(11) NOT NULL auto_increment,

'user_id’ int(11) NOT NULL,

'report_data’ text NOT NULL,

'operation_type’ int(11) NOT NULL,

'date_completed’ int(11) NOT NULL,

'date_started’ int(11) NOT NULL,

'order_id’ int(11) NOT NULL,

PRIMARY KEY (’id’),

KEY 'user_id’ (’user_id’),

KEY 'order_id’ (’order_id’)

) TYPE=InnoDB AUTO INCREMENT=12 ;

5/5 - (1 vote)