SQL beginner error creating a table - mysql

So im new to sql and im trying to create a series of tables for cant seem to get it to work. The error message that I get is.
The tables all add in individually no problem but when I want to import all of them at once it gives me this error.
Schema Creation Failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[Customer](
CustomerID int PRIMARY KEY,
CustomerName varchar(' at line 1:
CREATE TABLE IF NOT EXISTS [Customer](
CustomerID int PRIMARY KEY,
CustomerName varchar(50) NOT NULL,
BillingAddress varchar(400) default NULL,
PhoneNumber int default NULL,
User_Email varchar(128) UNIQUE
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS [Order](
OrderID int NOT NULL PRIMARY KEY,
CustomerID int,
OrderDate date NOT NULL,
PurchaseOrderNumber int
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS [OrderItem](
OrderItemID int PRIMARY KEY,
ProductID int NOT NULL,
OrderID int NOT NULL,
Quantity int NOT NULL,
PricePerUnit double NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS [Product](
ProductID int PRIMARY KEY,
ProductName varchar(100) NOT NULL,
ProductDescription varchar(100) NOT NULL,
SerialNumber int NOT NULL,
UnitesInStock int NOT NULL,
UnitsOnOrder int NOT NULL,
UnitPrice double NOT NULL
SupplierID int not NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS [SupplySchedule](
ShippingID int PRIMARY KEY,
ShippingMode varchar(50) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS [Supplier](
SupplierID int PRIMARY KEY,
SupplierName varchar(40) NOT NULL,
SupplierAddress varchar(128) NOT NULL,
ContactName varchar(128) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;

order is a reserved word in MySQL. Either use backticks to escape it or use another name.
CREATE TABLE IF NOT EXISTS `Order` (...

I have rename Order table to OrderMaster as "Order" is mysql reserved word
try below script:
drop table if exists Customer;
CREATE TABLE Customer(
CustomerID int PRIMARY KEY,
CustomerName varchar(50) NOT NULL,
BillingAddress varchar(400) default NULL,
PhoneNumber int default NULL,
User_Email varchar(128) UNIQUE
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
drop table if exists OrderMaster;
CREATE TABLE IF NOT EXISTS OrderMaster(
OrderID int NOT NULL PRIMARY KEY,
CustomerID int,
OrderDate date NOT NULL,
PurchaseOrderNumber int
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
drop table if exists OrderItem;
CREATE TABLE OrderItem(
OrderItemID int PRIMARY KEY,
ProductID int NOT NULL,
OrderID int NOT NULL,
Quantity int NOT NULL,
PricePerUnit double NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
drop table if exists Product;
CREATE TABLE IF NOT EXISTS Product(
ProductID int PRIMARY KEY,
ProductName varchar(100) NOT NULL,
ProductDescription varchar(100) NOT NULL,
SerialNumber int NOT NULL,
UnitesInStock int NOT NULL,
UnitsOnOrder int NOT NULL,
UnitPrice double NOT NULL,
SupplierID int not NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
drop table if exists SupplySchedule;
CREATE TABLE IF NOT EXISTS SupplySchedule(
ShippingID int PRIMARY KEY,
ShippingMode varchar(50) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
drop table if exists Supplier;
CREATE TABLE IF NOT EXISTS Supplier(
SupplierID int PRIMARY KEY,
SupplierName varchar(40) NOT NULL,
SupplierAddress varchar(128) NOT NULL,
ContactName varchar(128) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;

Related

Cannot create my tables due to an error from foreign key

I got this error from mysql its not creating my table as suppose since there is an error from it the code is the following
CREATE TABLE customers (
customer_number INT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
address VARCHAR(60) NOT NULL,
shipping_adress VARCHAR(60) NOT NULL,
billing_adress VARCHAR(60) NOT NULL,
balance_due DATE NOT NULL,
UNIQUE (customer_number)
);
CREATE TABLE orders (
order_number INT PRIMARY KEY NOT NULL,
customer_number VARCHAR(20) NOT NULL,
order_date DATE NOT NULL,
order_total_cost INT NOT NULL,
FOREIGN KEY (customer_number) REFERENCES customers(customer_number)
);
CREATE TABLE inventory (
product_number INT PRIMARY KEY,
product_name VARCHAR(60) NOT NULL,
product_measure FLOAT NOT NULL,
product_price FLOAT NOT NULL,
);
CREATE TABLE ordered_products (
ordered_id INT PRIMARY KEY,
order_number INT NOT NULL,
product_number INT NOT NULL,
quantity INT NOT NULL CHECK (QUANTITY > 0),
unit_price FLOAT NOT NULL,
FOREIGN KEY (order_number) REFERENCES orders(order_number),
FOREIGN KEY (product_number) REFERENCES inventory(product_number)
);
and the errror is as follows
ERROR 1064 (42000) at line 20: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 6
customer_number is a INT in customers table but VARCHAR(20) in the orders table
Ran on MySQL Compiler and it was successful
CREATE TABLE customers (
customer_number INT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
address VARCHAR(60) NOT NULL,
shipping_adress VARCHAR(60) NOT NULL,
billing_adress VARCHAR(60) NOT NULL,
balance_due DATE NOT NULL
);
CREATE TABLE orders (
order_number INT PRIMARY KEY NOT NULL,
customer_number INT NOT NULL,
order_date DATE NOT NULL,
order_total_cost INT NOT NULL,
FOREIGN KEY (customer_number) REFERENCES customers(customer_number)
);
CREATE TABLE inventory (
product_number INT PRIMARY KEY,
product_name VARCHAR(60) NOT NULL,
product_measure FLOAT NOT NULL,
product_price FLOAT NOT NULL
);
CREATE TABLE ordered_products (
ordered_id INT PRIMARY KEY,
order_number INT NOT NULL,
product_number INT NOT NULL,
quantity INT NOT NULL CHECK (QUANTITY > 0),
unit_price FLOAT NOT NULL,
FOREIGN KEY (order_number) REFERENCES orders(order_number),
FOREIGN KEY (product_number) REFERENCES inventory(product_number)
);
So the thing is I added a extra comma in the
CREATE TABLE inventory (
product_number INT PRIMARY KEY,
product_name VARCHAR(60) NOT NULL,
product_measure FLOAT NOT NULL,
product_price FLOAT NOT NULL,
);
So I just removed the comma from
product_price FLOAT NOT NULL
and it worked.
Commas can really be a pain if not using a mysql workbench

MySQL #1075 error when importing sql file?

I have code code to create tables in MySQL:
It returns the following error:
#1075 - There can be only one auto column and it must be defined as a key
What is wrong with it? It is generated by DrawSQL application.
I tried this code:
CREATE TABLE `sp_players`(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL,
`image` VARCHAR(255) NOT NULL,
`club` INT NOT NULL,
`trophies` INT NOT NULL,
`birthdate` DATE NOT NULL,
`prints` INT NOT NULL
);
ALTER TABLE
`sp_players` ADD PRIMARY KEY `sp_players_id_primary`(`id`);
CREATE TABLE `sp_prints`(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL,
`player` INT NOT NULL,
`momemt` INT NOT NULL,
`season` INT NOT NULL,
`compeitition` INT NOT NULL
);
ALTER TABLE
`sp_prints` ADD PRIMARY KEY `sp_prints_id_primary`(`id`);
ALTER TABLE
`sp_players` ADD CONSTRAINT `sp_players_prints_foreign` FOREIGN KEY(`prints`) REFERENCES `sp_prints`(`id`);
ALTER TABLE
`sp_prints` ADD CONSTRAINT `sp_prints_player_foreign` FOREIGN KEY(`player`) REFERENCES `sp_players`(`id`);
Expected to generate all the tables correctly

Add a foregin key to a different table

I'm trying to add username from my st_accounts column into my table results using mySQL.
st_accounts table
`id` int(11) NOT NULL, //Primary Key
`firstname` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
`username` varchar(50) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(100) NOT NULL,
`img` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
results table
`score_ID` int(11) NOT NULL, //Primary key
`score` int(20) NOT NULL,
`date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I think you want:
create table accounts (
account_id int auto_increment primary key,
. . .
);
create table results (
result_id int auto_increment primary key,
account_id int not null
score int NOT NULL,
score_date date NOT NULL,
constraint fk_results_accounts foreig key (accounts_id) references accounts(account_id)
) ;
Notes:
Declare the primary key explicitly, not in a comment.
My convention is to name the primary key after the name of the table (in singular) with _id after it.
The foreign key has the same name as the primary key -- self documenting.
I made the integer primary keys auto_increment, so the database can assign unique values.

Database functional dependencies

Im trying to find some functional dependencies in my database but have no idea how to do it. Would anyone be able to explain how to do it or what the functional dependencies are . Heres my code for creating my tables
CREATE TABLE Customer(
CustomerID int PRIMARY KEY,
CustomerName varchar(50) NOT NULL,
BillingAddress varchar(400) default NULL,
PhoneNumber varchar(128) NOT NULL,
UserEmail varchar(128) UNIQUE
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS OrderMaster(
OrderID int NOT NULL PRIMARY KEY,
CustomerID int,
OrderDate date NOT NULL,
PurchaseOrderNumber int
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE OrderItem(
OrderItemID int PRIMARY KEY,
ProductID int NOT NULL,
OrderID int NOT NULL,
Quantity int NOT NULL,
PricePerUnit double NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS Product(
ProductID int PRIMARY KEY,
ProductName varchar(100) NOT NULL,
ProductDescription varchar(100) NOT NULL,
SerialNumber int NOT NULL,
UnitesInStock int NOT NULL,
UnitsOnOrder int NOT NULL,
UnitPrice double NOT NULL,
SupplierID int not NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS SupplySchedule(
ShippingID int PRIMARY KEY,
ShippingMode varchar(50) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS Supplier(
SupplierID int PRIMARY KEY,
SupplierName varchar(40) NOT NULL,
SupplierAddress varchar(128) NOT NULL,
ContactName varchar(128) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;

MySQL syntax error uploading database

I am uploading an existing database to phpmyadmin, but when I try to input the data I get this error.
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'IDENTITY, acct_num varchar(25) NULL, acct_des ' at line 2
I am pretty familiar with phpmyadmin but I am not a programmer. Here is the data I am trying to input:
CREATE TABLE t_acct (
acct_id int IDENTITY,
acct_num varchar(25) NULL,
acct_des varchar(50) NULL,
status tinyint NULL,
PRIMARY KEY (acct_id)
)
go
CREATE TABLE t_asset (
asset_id int IDENTITY,
fund_id int NULL,
acct_id int NULL,
emp_id int NULL,
chkloc_id int NULL,
cond_id int NULL,
type_id int NULL,
class_id int NULL,
storloc_id int NULL,
outdte datetime NULL,
duedte datetime NULL,
bcode char(20) NOT NULL,
notes varchar(255) NULL,
des char(100) NULL,
model varchar(30) NULL,
sn varchar(50) NULL,
recdte datetime NULL,
price real NULL,
curdte datetime NULL,
curval real NULL,
dep real NULL,
deptype tinyint NULL,
image varchar(255) NULL,
status tinyint NOT NULL DEFAULT 1,
bin tinyint NULL,
bin_qty real NULL,
wardte datetime NULL,
purch_from varchar(255) NULL,
consumable tinyint NULL,
salvage_val real NULL,
reord_pt real NULL,
PRIMARY KEY NONCLUSTERED (asset_id)
)
go
CREATE INDEX XIF20t_asset ON t_asset
(
acct_id
)
go
CREATE INDEX XIF21t_asset ON t_asset
(
fund_id
)
go
CREATE INDEX XIF4t_asset ON t_asset
(
class_id
)
go
CREATE INDEX XIF5t_asset ON t_asset
(
type_id
)
go
CREATE INDEX XIF6t_asset ON t_asset
(
cond_id
)
go
CREATE INDEX XIF7t_asset ON t_asset
(
chkloc_id
)
go
CREATE INDEX XIF8t_asset ON t_asset
(
storloc_id
)
go
CREATE INDEX XIF9t_asset ON t_asset
(
emp_id
)
go
CREATE INDEX status ON t_asset
(
status
)
go
CREATE INDEX bcode ON t_asset
(
bcode
)
go
CREATE INDEX wardte ON t_asset
(
wardte
)
go
CREATE TABLE t_audit (
audit_id int IDENTITY,
audit_name varchar(255) NULL,
dte datetime NULL,
status tinyint NULL,
PRIMARY KEY NONCLUSTERED (audit_id)
)
go
CREATE INDEX dte ON t_audit
(
dte
)
go
CREATE INDEX status ON t_audit
(
status
)
go
CREATE TABLE t_audititems (
audititems_id int NOT NULL,
audit_id int NULL,
chk tinyint NULL,
ubcode char(20) NULL,
user_id int NULL,
abcode char(20) NULL,
asset_id int NULL,
ebcode char(20) NULL,
emp_id int NULL,
lbcode char(20) NULL,
loc_id int NULL,
expemp_id int NULL,
exploc_id int NULL,
dte datetime NULL,
status tinyint NULL,
bin_qty real NULL,
rec_type varchar(1) NULL,
asset_status varchar(20) NULL,
audit_reason varchar(50) NULL,
clear_table tinyint NULL,
notes varchar(255) NULL
)
go
ALTER TABLE t_audititems
ADD FOREIGN KEY (audit_id)
REFERENCES t_audit
go
CREATE TABLE t_bctype (
bctype_id int IDENTITY,
bctype varchar(50) NULL,
fntname varchar(50) NULL,
PRIMARY KEY (bctype_id)
)
go
CREATE TABLE t_class (
class_id int IDENTITY,
class char(20) NOT NULL,
des varchar(80) NULL,
status tinyint NOT NULL DEFAULT 1,
PRIMARY KEY NONCLUSTERED (class_id)
)
go
CREATE UNIQUE INDEX class ON t_class
(
class
)
go
CREATE INDEX status ON t_class
(
status
)
go
CREATE TABLE t_cond (
cond_id int IDENTITY,
cond char(20) NOT NULL,
des varchar(80) NULL,
status tinyint NOT NULL DEFAULT 1,
PRIMARY KEY NONCLUSTERED (cond_id)
)
go
CREATE UNIQUE INDEX XAK1t_cond ON t_cond
(
cond
)
go
CREATE INDEX status ON t_cond
(
status
)
go
CREATE TABLE t_curr_conns (
curr_conns_id int IDENTITY,
comp_name char(50) NULL,
tme datetime NULL,
PRIMARY KEY (curr_conns_id)
)
go
CREATE TABLE t_docs (
doc_id int IDENTITY,
asset_id int NULL,
doc_name varchar(255) NULL,
PRIMARY KEY NONCLUSTERED (doc_id)
)
go
CREATE INDEX XIF26t_docs ON t_docs
(
asset_id
)
go
CREATE INDEX doc_name ON t_docs
(
doc_name
)
go
CREATE TABLE t_emp (
emp_id int IDENTITY,
lname char(30) NULL,
fname char(80) NULL,
mname char(1) NULL,
bcode char(20) NULL,
login char(10) NULL,
pass char(10) NULL,
phone varchar(20) NULL,
altphone varchar(20) NULL,
notes varchar(255) NULL,
status tinyint NOT NULL DEFAULT 1,
ass tinyint NOT NULL DEFAULT 0,
res tinyint NOT NULL DEFAULT 0,
chk tinyint NOT NULL DEFAULT 0,
sup tinyint NOT NULL DEFAULT 0,
f_use tinyint NOT NULL DEFAULT 0,
mem tinyint NULL,
mem_ident_num varchar(20) NULL,
email varchar(80) NULL,
addr varchar(255) NULL,
category tinyint NULL,
emp_mem tinyint NULL,
PRIMARY KEY NONCLUSTERED (emp_id)
)
go
CREATE UNIQUE INDEX bcode ON t_emp
(
bcode
)
go
CREATE INDEX login ON t_emp
(
login
)
go
CREATE INDEX fname ON t_emp
(
fname
)
go
CREATE INDEX lname ON t_emp
(
lname
)
go
CREATE INDEX status ON t_emp
(
status
)
go
CREATE INDEX mem_ident_num ON t_emp
(
mem_ident_num
)
go
CREATE TABLE t_fac (
fac_id int IDENTITY,
fac varchar(40) NULL,
address varchar(100) NULL,
city varchar(40) NULL,
state varchar(2) NULL,
zip varchar(10) NULL,
notes varchar(255) NULL,
status tinyint NOT NULL DEFAULT 1,
PRIMARY KEY NONCLUSTERED (fac_id)
)
go
CREATE UNIQUE INDEX fac ON t_fac
(
fac
)
go
CREATE INDEX status ON t_fac
(
status
)
go
CREATE TABLE t_fund (
fund_id int IDENTITY,
fund_des varchar(50) NULL,
status tinyint NULL,
PRIMARY KEY (fund_id)
)
go
CREATE TABLE t_hist (
hist_id int IDENTITY,
fund_id int NULL,
acct_id int NULL,
cond_id int NULL,
fromloc_id int NULL,
loc_id int NULL,
fromemp_id int NULL,
emp_id int NULL,
asset_id int NULL,
dte datetime NULL,
type varchar(30) NULL,
bin_qty real NULL,
user_name varchar(120) NULL,
notes varchar(255) NULL,
PRIMARY KEY NONCLUSTERED (hist_id)
)
go
CREATE INDEX XIF10t_hist ON t_hist
(
asset_id
)
go
CREATE INDEX XIF11t_hist ON t_hist
(
emp_id
)
go
CREATE INDEX XIF12t_hist ON t_hist
(
loc_id
)
go
CREATE INDEX XIF13t_hist ON t_hist
(
cond_id
)
go
CREATE INDEX XIF15t_hist ON t_hist
(
fromemp_id
)
go
CREATE INDEX XIF16t_hist ON t_hist
(
fromloc_id
)
go
CREATE INDEX XIF18t_hist ON t_hist
(
acct_id
)
go
CREATE INDEX XIF19t_hist ON t_hist
(
fund_id
)
go
CREATE INDEX dte ON t_hist
(
dte
)
go
CREATE TABLE t_lbldesign_type (
design_type_id int IDENTITY,
design_name varchar(30) NULL,
design_type varchar(30) NULL,
PRIMARY KEY (design_type_id)
)
go
CREATE TABLE t_lbldesign (
design_id int NULL,
ele_type char(20) NULL,
ele_top smallint NULL,
ele_left smallint NULL,
ele_ht smallint NULL,
ele_wid smallint NULL,
font_name varchar(50) NULL,
font_bold varchar(5) NULL,
font_ital varchar(5) NULL,
font_size float NULL,
text_caption varchar(50) NULL,
text_datafld_seq smallint NULL,
graphic_fname varchar(255) NULL
)
go
CREATE INDEX XIF14t_lbldesign ON t_lbldesign
(
design_id
)
go
CREATE TABLE t_lbldesign_hdr (
design_id int IDENTITY,
design_name char(50) NULL,
design_wid float NULL,
design_ht float NULL,
design_type char(30) NULL,
design_orient varchar(1) NULL,
print_orient varchar(11) NULL,
design_type_id int NULL,
PRIMARY KEY (design_id)
)
go
CREATE TABLE t_lblref (
design_type char(20) NULL,
seq_num smallint NULL,
db_field_name varchar(50) NULL,
descript varchar(30) NULL,
sample_data varchar(50) NULL,
design_type_id int NULL
)
go
CREATE TABLE t_loc (
loc_id int IDENTITY,
emp_id int NULL,
fac_id int NULL,
loc char(50) NOT NULL,
bcode char(20) NULL,
stor tinyint NOT NULL DEFAULT 0,
notes varchar(255) NULL,
status tinyint NOT NULL DEFAULT 1,
PRIMARY KEY NONCLUSTERED (loc_id)
)
go
CREATE UNIQUE INDEX bcode ON t_loc
(
bcode
)
go
CREATE INDEX XIF1t_loc ON t_loc
(
fac_id
)
go
CREATE INDEX XIF17t_loc ON t_loc
(
emp_id
)
go
CREATE INDEX status ON t_loc
(
status
)
go
CREATE INDEX stor ON t_loc
(
stor
)
go
CREATE INDEX loc ON t_loc
(
loc
)
go
CREATE TABLE t_misc (
aclass varchar(25) NULL,
atype varchar(25) NULL,
afac varchar(25) NULL,
aloc varchar(25) NULL,
adep tinyint NULL,
apurge tinyint NULL,
afund varchar(25) NULL,
image_folder varchar(255) NULL,
auditseq int NULL,
cust_ident varchar(4) NULL,
license_key varchar(16) NULL,
bc_prefix varchar(3) NULL,
bc_seqnum varchar(20) NULL,
relocate tinyint NULL,
arepair tinyint NULL,
app_ver varchar(20) NULL,
amaint_rpt tinyint NULL,
afindpop tinyint NULL,
amem varchar(25),
mem_show tinyint NULL,
cat1_nme varchar(25),
cat2_nme varchar(25),
cat3_nme varchar(25),
mem_fldnme1 varchar(25),
mem_fldnme2 varchar(25),
mem_fldnme3 varchar(25),
mem_fldnme4 varchar(25),
smtp_host varchar(50),
smtp_addr varchar(50),
smtp_user varchar(50),
smtp_pass varchar(20),
smtp_type tinyint NULL,
srv_int int,
amodnum varchar(25),
asernum varchar(25),
acond varchar(25),
report_folder varchar(255),
logo varchar(255),
display_emp tinyint NULL,
display_acct tinyint NULL,
semester_enddte datetime NULL,
frd varchar(10),
req_chkin tinyint NULL,
areord_rpt tinyint NULL,
adep_pref tinyint NULL
)
go
CREATE TABLE t_res (
res_id int IDENTITY,
asset_id int NULL,
emp_id int NULL,
loc_id int NULL,
fdte datetime NULL,
tdte datetime NULL,
notes varchar(80) NULL,
prio tinyint NULL,
status tinyint NOT NULL DEFAULT 1,
PRIMARY KEY NONCLUSTERED (res_id)
)
go
CREATE INDEX XIF2t_res ON t_res
(
emp_id
)
go
CREATE INDEX XIF3t_res ON t_res
(
asset_id
)
go
CREATE INDEX loc_id ON t_res
(
loc_id
)
go
CREATE INDEX fdte ON t_res
(
fdte
)
go
CREATE INDEX tdte ON t_res
(
tdte
)
go
CREATE TABLE t_type (
type_id int IDENTITY,
type char(20) NULL,
des varchar(80) NULL,
dep real NULL,
status tinyint NOT NULL DEFAULT 1,
class_id int NULL,
duedte_type tinyint NULL,
duedte_num_days smallint NULL,
PRIMARY KEY NONCLUSTERED (type_id)
)
go
CREATE INDEX type ON t_type
(
type
)
go
CREATE INDEX status ON t_type
(
status
)
go
CREATE TABLE t_user_defined_data (
user_data_id int IDENTITY,
asset_id int NULL,
user_field_id int NULL,
user_data varchar(255) NULL,
dte_entered datetime NULL,
PRIMARY KEY NONCLUSTERED (user_data_id)
)
go
CREATE INDEX XIF24t_user_defined_data ON t_user_defined_data
(
user_field_id
)
go
CREATE INDEX XIF25t_user_defined_data ON t_user_defined_data
(
asset_id
)
go
CREATE INDEX user_data ON t_user_defined_data
(
user_data
)
go
CREATE TABLE t_user_defined_field (
user_field_id int IDENTITY,
type_id int NULL,
class_id int NULL,
user_field_name varchar(50) NULL,
status tinyint NULL,
PRIMARY KEY NONCLUSTERED (user_field_id)
)
go
CREATE INDEX XIF22t_user_defined_field ON t_user_defined_field
(
class_id
)
go
CREATE INDEX XIF23t_user_defined_field ON t_user_defined_field
(
type_id
)
go
CREATE INDEX user_field_name ON t_user_defined_field
(
user_field_name
)
go
ALTER TABLE t_asset
ADD FOREIGN KEY (fund_id)
REFERENCES t_fund
go
ALTER TABLE t_asset
ADD FOREIGN KEY (acct_id)
REFERENCES t_acct
go
ALTER TABLE t_asset
ADD FOREIGN KEY (emp_id)
REFERENCES t_emp
go
ALTER TABLE t_asset
ADD FOREIGN KEY (storloc_id)
REFERENCES t_loc
go
ALTER TABLE t_asset
ADD FOREIGN KEY (chkloc_id)
REFERENCES t_loc
go
ALTER TABLE t_asset
ADD FOREIGN KEY (cond_id)
REFERENCES t_cond
go
ALTER TABLE t_asset
ADD FOREIGN KEY (type_id)
REFERENCES t_type
go
ALTER TABLE t_asset
ADD FOREIGN KEY (class_id)
REFERENCES t_class
go
ALTER TABLE t_docs
ADD FOREIGN KEY (asset_id)
REFERENCES t_asset
go
ALTER TABLE t_hist
ADD FOREIGN KEY (fund_id)
REFERENCES t_fund
go
ALTER TABLE t_hist
ADD FOREIGN KEY (acct_id)
REFERENCES t_acct
go
ALTER TABLE t_hist
ADD FOREIGN KEY (fromloc_id)
REFERENCES t_loc
go
ALTER TABLE t_hist
ADD FOREIGN KEY (fromemp_id)
REFERENCES t_emp
go
ALTER TABLE t_hist
ADD FOREIGN KEY (cond_id)
REFERENCES t_cond
go
ALTER TABLE t_hist
ADD FOREIGN KEY (loc_id)
REFERENCES t_loc
go
ALTER TABLE t_hist
ADD FOREIGN KEY (emp_id)
REFERENCES t_emp
go
ALTER TABLE t_hist
ADD FOREIGN KEY (asset_id)
REFERENCES t_asset
go
ALTER TABLE t_lbldesign
ADD FOREIGN KEY (design_id)
REFERENCES t_lbldesign_hdr
go
ALTER TABLE t_loc
ADD FOREIGN KEY (emp_id)
REFERENCES t_emp
go
ALTER TABLE t_loc
ADD FOREIGN KEY (fac_id)
REFERENCES t_fac
go
ALTER TABLE t_res
ADD FOREIGN KEY (asset_id)
REFERENCES t_asset
go
ALTER TABLE t_res
ADD FOREIGN KEY (emp_id)
REFERENCES t_emp
go
ALTER TABLE t_user_defined_data
ADD FOREIGN KEY (asset_id)
REFERENCES t_asset
go
ALTER TABLE t_user_defined_data
ADD FOREIGN KEY (user_field_id)
REFERENCES t_user_defined_field
go
ALTER TABLE t_user_defined_field
ADD FOREIGN KEY (type_id)
REFERENCES t_type
go
ALTER TABLE t_user_defined_field
ADD FOREIGN KEY (class_id)
REFERENCES t_class
go
CREATE TABLE t_prov (
prov_id int IDENTITY,
prov_des varchar(50) NULL,
address varchar(100) NULL,
city varchar(40) NULL,
state varchar(2) NULL,
zip varchar(10) NULL,
notes varchar(255) NULL,
contact varchar(100) NULL,
status tinyint NOT NULL DEFAULT 1,
PRIMARY KEY NONCLUSTERED (prov_id)
)
go
CREATE INDEX prov_des ON t_prov
(
prov_des
)
go
CREATE INDEX status ON t_prov
(
status
)
go
CREATE TABLE t_maint (
maint_id int IDENTITY,
type_id int NULL,
class_id int NULL,
maint_name varchar(50) NULL,
spec_inst varchar(255) NULL,
m_interval real NULL,
status tinyint NOT NULL DEFAULT 1,
PRIMARY KEY NONCLUSTERED (maint_id)
)
go
IDENTITY is a Microsoft SQL Server keyword.
MySQL uses AUTO_INCREMENT for similar functionality.
Example:
CREATE TABLE t_acct (
acct_id int AUTO_INCREMENT,
acct_num varchar(25) NULL,
acct_des varchar(50) NULL,
status tinyint NULL,
PRIMARY KEY (acct_id)
);
Also "go" is a statement terminator in Microsoft SQL Server. MySQL uses ; or \G.