using spaces in columns names in sql to create table - sql

I am trying to create table by sqlyog
i want to use spaces in columns names but i still got errors
for example id number
i just can do it like this id_number
i searched in this website i found two ways
[id number] or "id number"
i tried it but i still have errors
this is the code
CREATE TABLE project(
ProjectID VARCHAR(10),
Project NAME VARCHAR(50),
Group_Name VARCHAR(20),
BeginDate VARCHAR(10),
EndDate VARCHAR(10)
);
and these are the errors that i got them
1 queries executed, 0 success, 1 errors, 0 warnings
Query: CREATE table project( ProjectID varchar(10), Project Name varchar(50), Group_Name varchar(20), BeginDate varchar(10), EndDate va...
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Name varchar(50),
Group_Name varchar(20),
BeginDate varchar(10),
EndDate varc' at line 3
i hope some one helping me

The SQL Standard defines double quote character " to delimit identifiers.
Speaking of MariaDB and MySQL this requires that the sql_mode was set to ANSI:
mysql> set sql_mode=ANSI;
Query OK, 0 rows affected (0.00 sec)
mysql> create table project("project name" varchar(50));
Query OK, 0 rows affected (0.02 sec)
Another option (as mentioned in the previous answer) is to use backticks. However this solution will not be portable.

You can create tables and columns with spaces in the name using backticks (`)
Egg:
CREATE TABLE `project project`
(
`ProjectID` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`Project NAME` VARCHAR(255) NOT NULL
.
.
.
);

try like below
CREATE TABLE project(
ProjectID VARCHAR(10),
`Project NAME` VARCHAR(50),
Group_Name VARCHAR(20),
BeginDate VARCHAR(10),
EndDate VARCHAR(10)
);

Related

Presto Hive SQL Error: mismatched input 'PARTITIONED'. Expecting: 'COMMENT', 'WITH', <EOF>

I am trying to create a Hive table with partitions but getting the above error. What am I doing wrong?
CREATE TABLE IF NOT EXISTS schema.table_name
(
ID varchar(20),
name varchar(50)
)
PARTITIONED BY (part_dt varchar(8), system varchar(5));
The code works without the partitioning clause. Something gives up during partitioning.
Statement is working in hive. Pls find below screenshot.
Its possible that some of column names are reserved keywords and that is throwing error. if yes, you can use below SQL too.
CREATE TABLE IF NOT EXISTS schema.table_name
(
`ID` varchar(20),
`name` varchar(50)
)
PARTITIONED BY (`part_dt` varchar(8), `system` varchar(5));

Adding Columns to Multiple Tables in SQL

I just created a database and then added a couple of hundred tables with a script like this:
CREATE TABLE CapBond
(
[timestamp] varchar(50),
[Reward] varchar(50),
[Award] varchar(50),
[Fact] varchar(50)
)
CREATE TABLE Values
(
[timestamp] varchar(50),
[Name] varchar(50),
[Test] varchar(50),
[Read] varchar(50),
[Parameters] varchar(50)
)
I realize I forgot to add two columns to each table. One for the PK and one for an FK that points back to a 'master' table.
Is there an easy way to insert columns without having to drop the DB and recreate it? Preferably with the columns inserted as the first two columns in the table?
Yes. In mysql you have the alter table command for this purpose. Check out this page for more detailed explanation
https://www.sqlservertutorial.net/sql-server-basics/sql-server-alter-table-add-column/ .
And here is the solution for the ordering of the columns
https://www.mysqltutorial.org/mysql-add-column/

Invalid table name error when creating a table

Very new to SQL, but I thought that I had at least mastered how to make tables. I am trying to create the following table and get the error 'ORA-00903: invalid table name'. I'm not sure what is wrong.
Create table order (
order_id int,
item_type varchar(50),
item_name varchar(50),
item_price decimal(10,2),
primary key(order_id)
);
I am testing this on Oralce Live SQL and it is ok as well as on my Oracle 12c Database EE, all you need to add are "". But even so, I would not recommend it to use reserved words for naming tables.
Create table "order" (
order_id int,
item_type varchar(50),
item_name varchar(50),
item_price decimal(10,2),
primary key(order_id)
);
insert into "order" values (1, 'Item', 'Name', '20.2');
select * from "order";

i want to create a table but bot able to create it

i want to create a table in hive with NOT NULL property but i am not able to create it . it will says "ParseException line 1:44 mismatched input 'NOT' expecting ) near ')' in create table statement".
i have tried with primary key option but it will give same error
create table AGENTS(agent_code varchar(10) NOT NULL ,
agent_name varchar(40),
working_area varchar(30),
commission double(10,5),
phone_no int(15),
country varchar(25));
I don't think Hive supports double with scale and precision arguments. And, I don't think there is a length for int Perhaps you intend numeric:
create table AGENTS (
agent_code varchar(10) NOT NULL ,
agent_name varchar(40),
working_area varchar(30),
commission decimal(10, 5),
phone_no decimal(15),
country varchar(25)
);
NOT NULL constraints are only enforced relatively recently, so that might also be a problem.
Check your Hive version. Older versions of Hive do not support NOT NULL constraint like most databases. NOT NULL constraint was introduced from 3.0.0 version onwards.
Reference: https://issues.apache.org/jira/browse/HIVE-16575

Order of rows changes when creating table

I've a weird issue.
DROP table IF EXISTS ipl;
CREATE TABLE ipl(
match_id VARCHAR(50),
batting VARCHAR(50),
bowling VARCHAR(50),
overn VARCHAR(50),
batsman VARCHAR(50),
bowler VARCHAR(50),
super_over VARCHAR(50),
bat_runs VARCHAR(50),
extra_runs VARCHAR(50),
total_runs VARCHAR(50),
player_out VARCHAR(50),
how VARCHAR(50),
fielder VARCHAR(50));
BULK INSERT ipl
FROM 'F:\Study\Semesters\4th
Sem\COL362\HomeWork\1\Dataset\deliveries.csv'
WITH(FIELDTERMINATOR= ',');
SELECT * FROM ipl;
This is the code I'm using to make the table in SSMS. match_id goes from 1 to about 290, in increasing order in the csv file. When I executed this query once, everything was ok. But, when I did that again, some rows from the middle were moved to the last.
You can see that below:
(Note that jump from 4 to 49)
I don't know what's wrong. Please help me resolve this issue. Thanks!
SQL tables represent unordered sets. If you want rows in a particular order, you need an order by. How can you do this with a bulk insert? Well, you need an identity column. The idea is to create the table with an identity and use a view for the bulk insert:
create table ipl (
ipl_id int identity(1, 1) primary key,
. . .
);
create view vw_ipl as
select match_id, batting, bowling, . . .
from ipl
bulk insert vw_ipl
from 'F:\Study\Semesters\4th Sem\COL362\HomeWork\1\Dataset\deliveries.csv'
with (fieldterminator= ',' );
select *
from ipl
order by ipl_id;
As a relational database SQL server do not guarantee a particular order of returned data. If you need ordered data, specify order by clause.