Work-built custom vending machine SQL assistance - sql

At work, we have build a vending machine and i have been asked as a side-project to attempt to create a SQL database for it. It will not be used with the vending machine, but will be used for me to practise as i'm very new to SQL (1 month in).
The vending machine will use a keyfob to touch on. Once that's done, you open up the door and grab what you like. The machine has weight sensors, so whatever you take will be added to your fob amount.
Below is what i have produced. I am currently only creating the tables. Please send me your constructive critisism and corrections as it will help me greatly!
CREATE TABLE Vender (
Name varchar (50),
Description varchar (50)
);
CREATE TABLE Bay (
StockItem uniqueidentifier,
Channel integer
);
CREATE TABLE Users (
NameFirst varchar (50),
NameLast varchar (50),
Title varchar (50),
FobID uniqueidentifier,
Credential_TakeStock integer,
Credential_Addstock integer,
Credential_Admin integer
);
CREATE TABLE Reasons (
Name uniqueidentifier
);
CREATE TABLE Machine (
Name uniqueidentifier
);
CREATE TABLE StockItems (
Code integer,
Description varchar (50),
Vendor varchar (50),
LeadTime time,
QtyCurrent integer,
QtyMax integer,
QtyMin integer,
QtyCritical integer,
WeightInGrams integer,
Bay ???
);
CREATE TABLE Purchase (
DateTime datetime,
UserName varchar,
StockItem uniqueidentifier,
Reason varchar,
Machine uniqueidentifier,
Qty integer,
DepletedItemReturned bit
);
One thing to also mention is i'm not sure of the data value for Bay in StockItems.
Thank you in advance.

Related

Yes/No column in SQL 2018 studio

Can anyone explain how to create a yes/no column in SQL Studio 2018? BOOL or bit(1) are not working.
Create Table Diseases
(
P_ID int not null,
NameofDis varchar(100),
DateofDis date,
Condition bit(1),
);
You can use simple BIT datatype or you can use BOOLEAN data type like this
Create Table Diseases(
P_ID int not null,
NameofDis varchar(100),
DateofDis date,
Condition BIT,
);
or
Create Table Diseases(
P_ID int not null,
NameofDis varchar(100),
DateofDis date,
Condition BOOLEAN,
);
Try using BIT instead of BIT(1). This will create the column as a yes/no field aka true/false field where 1 is yes/true and 0 is no/false.
Your code:
Create Table Diseases(
P_ID int not null,
NameofDis varchar(100),
DateofDis date,
Condition bit
);

Guessing Data Type of columns stored as VarChar in SQLServer

I'm just wondering if anyone has created or heard of a function to guess the appropriate data type of data stored in a SQLServer table as VarChar.
I'm working on an SSIS package that you can point at a directory, and it will loop through and create tables / import data for every CSV that exists it. I'm having trouble with specifying the data types before import, so as a work around I would like to import all the data as VarChar(50) into a temporary table and then run some sort of function to analyze each column for the appropriate data type (int, decimal, float, etc) so I can use that to script the create table and insert statements.
So for example I'd like to be able to point a function or query at this temp table
CREATE TABLE [#Data]
(
[ProductCode] varchar(50),
[ProductName] varchar(50),
[Year] varchar(50),
[Total_volume] varchar(50),
[Total_Quantity] varchar(50),
[PercentSold] varchar(50)
)
to read through the data and determine what data type / length is most appropriate - much like the 'Suggest Data Type' tool in Excel Connection Manager does, only something I can tie into a variable to be done dynamically. It should end up looking something like
CREATE TABLE [Data]
(
[ProductCode] varchar(6),
[ProductName] varchar(11),
[Year] int,
[Total_volume] int,
[Total_Quantity] int,
[PercentSold] decimal(3,2)
)
Any thoughts?
Thanks!

How can I reseed a table for Merge replication when it has reach its max identity value. Software is hard coded as int so cannot change datatype?

I have a client using SQL 2008R2 and they have Merge replication in place. They have hit the max range on a few of their tables because the subscriptions were being removed and readded multiple times. The table on the publisher only contains 175000 row and is replicating to 7 other sites so they should be no where close to hitting the max.
How can I reseed the table and keep all the data intact? I tried copying the table and then dropping the table and renaming it but the identity ranges values stay the same. I cannot change the data type because the uses of Int as a datatype is hard coded into our software.
Any help would be appreciated.
Table looks like this and the rownum is the id column.
rowguid, uniqueidentifier,
,contactid, float,
,RowNum, int,
,type, int,
,createdby, varchar(30),
,assignedto, varchar(30),
,createddate, int,
,modifieddate, int,
,startdate, int,
,duedate, int,
,completedate, int,
,duration, int,
,tickler, int,
,priority, smallint,
,therule, int,
,status, int,
,private, tinyint,
,flags, int,
,subject, int,
,notes, text

Selecting data from a database via a joined table

I'm currently making a website that is used to advertise car sharing for festivals. I need to list all trips which are currently assigned to a user but seeing as the database relationship would be many to many I have had to make a client_trip table.
My question:
How would I select trips from the trip table based on the information in my client_trip table?
I'm currently using PostgreSQL and Java servlets. Thanks very much for any help. :)
CREATE TABLE users
(
user_id SERIAL,
user_username VARCHAR (20),
user_firstname VARCHAR(20),
user_surname VARCHAR(20),
user_password VARCHAR(50),
user_email VARCHAR(100),
user_role VARCHAR(20),
PRIMARY KEY(user_id)
);
CREATE TABLE trips
(
trip_id SERIAL,
trip_name VARCHAR (100),
trip_user_username VARCHAR (50),
trip_festival_id SERIAL REFERENCES festivals(festival_id),
trip_festival_name VARCHAR(100),
trip_depart_date DATE,
trip_return_date DATE,
trip_spaces INT,
trip_cost Decimal (19,2),
trip_desc VARCHAR,
PRIMARY KEY(trip_id)
);
how would I select trips from the trip table based on the information in my client_trip
With a given user_id:
SELECT t.*
FROM trips t
JOIN client_trip ct USING (trip_id)
WHERE ct.user_id = ??

auto_increment causing errors with CREATE TABLE

I have been using netbeans as a tool for my java, and i have a problem. I read this tutorial and then i tried to create a table using this SQL:
CREATE TABLE CUSTOMERS (
ID INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
FIRST_NAME VARCHAR(20),
LAST_NAME VARCHAR(30),
ADDRESS VARCHAR(30),
CITY VARCHAR(30),
STATE_ VARCHAR(30),
ZIP VARCHAR(15),
COUNTRY_ID INTEGER,
PHONE VARCHAR(15),
EMAIL_ADDRESS VARCHAR(50)
)ENGINE=INNODB;
When i tried to run it, I got this error message:
sql state 42X01 : Syntax error :
encountered "AUTO_INCREMENT" at line 2
column 29
and when i delete the AUTO_INCREMENT, another error:
detected ENGINE=INNODB;
can someone help me? Thanks.
You seem to be using MySQL syntax with another database engine. The parts it complained about are precisely the MySQL-specific ones.
my sugestion would be the following
CREATE TABLE CUSTOMERS
( ID INTEGER NOT NULL auto_increment,
FIRST_NAME VARCHAR(20),
LAST_NAME VARCHAR(30),
ADDRESS VARCHAR(30),
CITY VARCHAR(30),
STATE_ VARCHAR(30),
ZIP VARCHAR(15),
COUNTRY_ID INTEGER,
PHONE VARCHAR(15),
EMAIL_ADDRESS VARCHAR(50),
PRIMARY KEY (ID));
Dunno what the engine=innodb is for, have you tried without it?
The "engine=innodb" part specifies the database engine that gets used in the database. With MySQL you can specify different engines like "InnoDB", "MyISAM", etc. They have different properties and features - some allow foreign indexes, some do not. Some have different locking mechanisms, some have different atomicity/rollback properties. I don't know the details but if you need a really high-performance database setup you should investigate which engine is best for each type of table you're creating. Also, all my database experience has been with MySQL and I'm not sure if that's what you're using.
Been a long time but if anybody else stumbles on this like I did, a solution that worked for me is instead of using auto_increment, describe the ID column as
ID INTEGER GENERATED ALWAYS AS IDENTITY, WHATEVER VARCHAR(20), ETC ETC...