CREATE TABLE sales (
ID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
[Item Type] VARCHAR(30) NULL,
[Sales Channel] VARCHAR(30) NULL,
[Order Priority] VARCHAR(30) NULL,
[Order Date] DATE NULL,
[Order ID] DECIMAL NULL,
[Ship Date] DATE NULL
)
Dear guys, please with how to use as right syntax for the table columns: e.g. Item Type - here "type" already existing in mysql, Sales Channel - here "channel" already exists in mysql as operator names. but i need to use as column names for the table. Thanks!
There are different ways to do this.
You could use double quotes (") to escape the reserved keyword.
You could use back-ticks (`)
You could prepend with schema name (only for tables) e.g.
myschema.Table.
A good reference is here
If the table name or column name is a keyword or it contains spaces, and you still want to use that name, quote them between backticks '`'. So your query should look like:
CREATE TABLE sales (
ID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`Item Type` VARCHAR(30) NULL,
`Sales Channel` VARCHAR(30) NULL,
`Order Priority` VARCHAR(30) NULL,
`Order Date` DATE NULL,
`Order ID` DECIMAL NULL,
`Ship Date` DATE NULL
);
Try it yourself at SQL Fiddle: http://sqlfiddle.com/#!9/0332f0
Fix your column names so they do not have to be escaped! This will make all subsequent work much simpler:
CREATE TABLE sales (
SalesID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
ItemType VARCHAR(30) NULL,
SalesChannel VARCHAR(30) NULL,
OrderPriority VARCHAR(30) NULL,
OrderDate DATE NULL,
OrderID DECIMAL NULL,
ShipDate DATE NULL
);
I recommend using the table name for the id. In addition, you are using DECIMAL with no precision or scale. Highly UNRECOMMENDED. Don't depend on database defaults (do you even know what they are?).
You should also include foreign key references. So you should have a foreign key constraint to the orders table.
Related
I have two tables one is a customer table and the second is sales table.
I need to create a query to display customer name, customer_id and number of Gadgets bought (write two queries using different syntaxes of JOIN). Example : “John Barry - 111 bought 5 gadgets”.
CUSTOMERS_JS
create table CUSTOMERS_JS (
CUSTID smallint not null,
CUSTNAME char(50) not null,
primary key(CUSTID)
);
STORE_SALES_JS
create table STORE_SALES_JS (
SALEID smallint not null,
SALETS datetime not null,
GADGETID smallint not null,
EMPID smallint not null,
CUSTID smallint not null,
primary key(SALEID),
foreign key(GADGETID) references ELEC_items_JS(GADGETID),
foreign key(EMPID) references Store_EMPS_JS(EMPID),
foreign key(CUSTID) references CUSTOMERS_JS(CUSTID)
);
I did this query
select concat(CUSTNAME,' - ',STORE_SALES_JS.CUSTID,' bought ',count(STORE_SALES_JS.GADGETID),' gadgets') as result
from CUSTOMERS_JS,STORE_SALES_JS
where STORE_SALES_JS.CUSTID = CUSTOMERS_JS.CUSTID
group by STORE_SALES_JS.CUSTID,CUSTNAME
order by STORE_SALES_JS.CUSTID
but there is too much space between the name and the '-'. I tried to change the name field to varchar and it worked as it supposed to work but I need it to work with char(50) as well.
Thanks to scaisEdge help I managed to fix this issue while using rtrim function
select concat(rtrim(CUSTNAME),' - ',STORE_SALES_JS.CUSTID,' bought ',count(STORE_SALES_JS.GADGETID),' gadgets') as result
from CUSTOMERS_JS,STORE_SALES_JS
where STORE_SALES_JS.CUSTID = CUSTOMERS_JS.CUSTID
group by STORE_SALES_JS.CUSTID,CUSTNAME
order by STORE_SALES_JS.CUSTID
if you must use char and not varchar but need a trimmed result in your select you could trim ( or rtrim or ltrim) your custname for remove the spaces
select concat(rtrim(CUSTNAME),' - '
,STORE_SALES_JS.CUSTID,' bought '
,count(STORE_SALES_JS.GADGETID),' gadgets') as result
from CUSTOMERS_JS,STORE_SALES_JS
where STORE_SALES_JS.CUSTID = CUSTOMERS_JS.CUSTID
group by STORE_SALES_JS.CUSTID,CUSTNAME
order by STORE_SALES_JS.CUSTID
SELECT candidate_num FROM candidate ORDER BY candidate_num
The results of the query above is:
We can see candidate_num is not in order. Following is the structure of this table. I cannot figure why the "2" is after "19".
CREATE TABLE `candidate` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`candidate_num` varchar(10) NOT NULL,
`name` varchar(50) NOT NULL,
`age` int(3) NOT NULL,
`major` varchar(50) NOT NULL,
`company` varchar(50) NOT NULL,
`department` varchar(50) NOT NULL,
`native_place` varchar(50) NOT NULL,
`ethnicity` varchar(50) NOT NULL,
`highest_education` varchar(50) NOT NULL,
`group` varchar(50) DEFAULT NULL,
`is_elected` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `candidate_num` (`candidate_num`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=61 DEFAULT CHARSET=utf8;
Here "candidate_num" is a string (varchar field), so it gets ordered lexically. If it is so, you can probably order it by:
SELECT `candidate_num` FROM `candidate` ORDER BY convert(`candidate_num`, decimal) ASC;
Note: For varchar it orders the data in lexical order, i.e. first it put the value having 1 in first place and then the value having 2 on second place and so on.
Data type of candidate_num is varchar type. You cannot order by values of this columns like a numeric types.
Solution:
`candidate_num` int(6) NOT NULL,
Note:
SELECT `candidate_num` FROM `candidate` ORDER BY convert(`candidate_num`, decimal) ASC;
If you keep varchar datatype for this column and convert values when using order by, the performance will completly down
Er, candidate number IS in order, it is in value number order rather than number quantity order. [as better described by Mayank; it is sorting in lexical order]
By Default in MySQL numeric columns are naturally sorted (the sorting you want), so simply change your ORDER BY column from VARCHAR to a numerical column using CAST(). Below is setting to an int column:
SELECT candidate_num FROM candidate ORDER BY CAST(candidate_num AS UNSIGNED)
see also:
- Natural Sort in MySQL
- Cast from VARCHAR to INT - MySQL
As others have said, if your column contents will always be numeric, you really should change the column to an INT, or DECIMAL, etc... numerical value, rather than trying to CAST the value on evey SQL call to it.
If the candidate number will always remains as Number then you should have to convert the data type of candidate number As integer instead of keeping as Varchar.
Once you change the data type of candidate_number to integer you will get the the number in sorted order.
I am creating the table in SQLite and want to CHECK the syntax of the date. It should be 'YYYY-MM-DD'. When I insert data into this table it should give me error when I type something else when inserting the date, e.g. Insert into Member(DoB) values('2013-120qw'). It should be only for instance '2013-12-04'.
Here is my table:
CREATE TABLE Member(
email VARCHAR(100) NOT NULL,
name VARCHAR(100) NOT NULL,
DoB TEXT(10) NOT NULL CHECK (length(DoB) IN (10) AND date(DoB) BETWEEN date('1900-01-01') AND date(CURRENT_DATE)),
telephone VARCHAR(11) NOT NULL CHECK (length(telephone) IN (11)),
PRIMARY KEY (email)
);
The problem is that date() returns NULL for invalid date strings, and NULL does not make the check constraint fail.
Add a separate check that date() is happy:
...
DoB TEXT(10) NOT NULL CHECK (date(DoB) IS NOT NULL AND
length(DoB) = 10 AND
DoB BETWEEN '1900-01-01' AND CURRENT_DATE),
...
i thank DATE type is useful because MySql doesn't have Constraints
CREATE TABLE Member(
email VARCHAR(100) NOT NULL,
name VARCHAR(100) NOT NULL,
DoB date NOT NULL,
telephone VARCHAR(11) NOT NULL CHECK (length(telephone) IN (11)),
PRIMARY KEY (email)
);
if you need help this link will help you
https://dev.mysql.com/doc/refman/5.0/en/datetime.html
While trying to initialise a database in MySQl, we have ran into the same errors (1064 & 1146) numerous times and are out of ideas on how to correct it.
Here is what we have so far:
any help would be greatly appreciated.
Thank you.
You are using strings for table names,use back ticks.Also you have foreign keys referencing different column types,they must be the same type and size.Also referenced column must have primary or unique key.
Here it is,but I don't think this is a correct design.
SQL Fiddle
Just missing commas:
CREATE TABLE 'Customer' (
customerCode VARCHAR(5) PRIMARY KEY,
firstName VARCHAR(20) NOT NULL,
lastName VARCHAR(20)NOT NULL,
pointsTotal VARCHAR(5)
)ENGINE=INNODB;
CREATE TABLE 'GameList' (
gameCode INT PRIMARY KEY AUTO_INCREMENT,
gameName VARCHAR(25) NOT NULL,
consoleName VARCHAR(25) NOT NULL,
pointsValue VARCHAR(25) NOT NULL
)ENGINE=INNODB;
And last select should be like:
SELECT custCode,
SUM(points) as pointsTotal from CustomerHistory
GROUP BY custCode;
Your last SELECT has too many commas, it should be something like
SELECT custCode, SUM(points) as pointsTotal
FROM CustomerHistory
GROUP BY custCode;
I'm new to PHP and MySQL and ran into a little trouble with a learning project I'm working on.
Whenever I try to create a table
CREATE TABLE transactions(
id int NOT NULL AUTO_INCREMENT,
location varchar(50) NOT NULL,
description varchar(50) NOT NULL,
category varchar(50) NOT NULL,
amount double(10) NOT NULL,
type varchar(6) NOT NULL,
notes varchar(512),
receipt int(10),
)
I get the following error message:
#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 ') NOT NULL, type varchar(6) NOT NULL, notes varchar(512),
receipt int(10), ' at line 6**
Here is some info on what I'm working with
Server type: MySQL
Server version: 5.5.32 - MySQL Community Server(GPL)
phpMyAdmin: 4.0.4.1, latest stable version: 4.1.7
I've spent a day knocking my head against the wall over this and now I think its time to ask for help.I was wondering if anyone can tell me what I'm doing wrong?
Remove the comma
receipt int(10),
And also AUTO INCREMENT should be a KEY
double datatype also requires the precision of decimal places so right syntax is double(10,2)
One obvious thing is that you will have to remove the comma here
receipt int(10),
but the actual problem is because of the line
amount double(10) NOT NULL,
change it to
amount double NOT NULL,
In MySQL, the word 'type' is a Reserved Word.
I see two problems:
DOUBLE(10) precision definitions need a total number of digits, as well as a total number of digits after the decimal:
DOUBLE(10,8)
would make be ten total digits, with 8 allowed after the decimal.
Also, you'll need to specify your id column as a key :
CREATE TABLE transactions(
id int NOT NULL AUTO_INCREMENT,
location varchar(50) NOT NULL,
description varchar(50) NOT NULL,
category varchar(50) NOT NULL,
amount double(10,9) NOT NULL,
type varchar(6) NOT NULL,
notes varchar(512),
receipt int(10),
PRIMARY KEY(id) );
I've faced this problem before, the reason behind that for me was the last comma in the schema definition should be deleted if there's no extra columns would be added.
CREATE TABLE users
(
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL, // this comma should be deleted
);
Rule 1: You can not add a new table without specifying the primary key constraint[not a good practice if you create it somehow].
So the code:
CREATE TABLE transactions(
id int NOT NULL AUTO_INCREMENT,
location varchar(50) NOT NULL,
description varchar(50) NOT NULL,
category varchar(50) NOT NULL,
amount double(10,9) NOT NULL,
type varchar(6) NOT NULL,
notes varchar(512),
receipt int(10),
PRIMARY KEY(id));
Rule 2: You are not allowed to use the keywords(words with predefined meaning) as a field name.
Here type is something like that is used(commonly used with Join Types).
So the code:
CREATE TABLE transactions(
id int NOT NULL AUTO_INCREMENT,
location varchar(50) NOT NULL,
description varchar(50) NOT NULL,
category varchar(50) NOT NULL,
amount double(10,9) NOT NULL,
transaction_type varchar(6) NOT NULL,
notes varchar(512),
receipt int(10),
PRIMARY KEY(id));
Now you please try with this code.
First check it in your database user interface(I am running HeidiSQL, or you can try it in your xampp/wamp server also)and make sure this code works. Now delete the table from your db and execute the code in your program.
Thank You.