Is it possible to have a % symbol in my column name? - sql

I'm trying to add a new column to an existing table, but when I try to create this column name I'm getting an invalid character error.
SQL Error: ORA-00911: invalid character
00911. 00000 - "invalid character"
*Cause: identifiers may not start with any ASCII character other than
letters and numbers. $#_ are also allowed after the first
character. Identifiers enclosed by doublequotes may contain
any character other than a doublequote. Alternative quotes
(q'#...#') cannot use spaces, tabs, or carriage returns as
delimiters. For all other contexts, consult the SQL Language
Reference Manual.
This is my code:
Alter Table Calculations
Add WEIGHTED_% Number;
What am I doing wrong?

I really don't recommend using the % symbol in you name I would recommend typing Percent instead. But as the error says you must en-capsule you column name in double quotes.
Alter Table Calculations Add "WEIGHTED_%" Number;
Not sure then it would still work because % is a reserved character for Doing like query and including them in exposed query is considered a vulnerability because it allows SQL injection.

Yes. Possible. Use double code for column name. Check the attached screen shot.
But it's not good to use % in the column name. Not recommended.

Related

Where is my invalid character?

Every time I want to create a table in sql developer:
create table tabelle(
dgeh emp.sal%type)
I get this error:
00000 - "invalid character"
*Cause: identifiers may not start with any ASCII character other than
letters and numbers. $#_ are also allowed after the first
character. Identifiers enclosed by doublequotes may contain
any character other than a doublequote. Alternative quotes
(q'#...#') cannot use spaces, tabs, or carriage returns as
delimiters. For all other contexts, consult the SQL Language
Reference Manual.
*Action:
It's driving my crazy! Where is my invalid character? Thanks in advance!

set alias Name for Table visually

I want set a second name for my tables which is not in English letter. and when I want insert Table Name to a Table as a record, I use its alias name.
I know in each query we can use as to set second name, but I don't want do it by this way. I want set it as a property of a table.
Also please tell me how can I access to this property (alias name) in query?
I found this article, but I didn't found the alias field.
You can do that with a view:
CREATE VIEW EasyName
AS
SELECT * FROM LongComplicatedTableName
So long as the view only represents one table underneath, you can use all CRUD operations on the view.
According to MSDN, the naming restrictions for table names is as follows:
The first character must be one of the following:
A letter as defined by the Unicode Standard 2.0. The Unicode definition of letters includes Latin characters from a through z and
from A through Z, in addition to letter characters from other
languages.
The underscore (_), "at" sign (#), or number sign (#). Certain symbols at the beginning of an identifier have special meaning in SQL
Server. An identifier beginning with the "at" sign denotes a local
variable or parameter. An identifier beginning with a number sign
denotes a temporary table or procedure. An identifier beginning with
double number signs (##) denotes a global temporary object.
Some Transact-SQL functions have names that start with
double at signs (##). To avoid confusion with these functions, it is
recommended that you do not use names that start with ##.
Subsequent characters can be:
Letters as defined in the Unicode Standard 2.0.
Decimal numbers from either Basic Latin or other national scripts.
The "at" sign, dollar sign ($), number sign, or underscore.
The identifier must not be a Transact-SQL reserved word. SQL Server reserves both the uppercase and lowercase versions of reserved words.
Embedded spaces or special characters are not allowed.
Supplementary characters are not allowed.
When used in Transact-SQL statements, identifiers that fail to comply
with these rules must be delimited by double quotation marks or
brackets.

SQL Check Statement for Zip Code using escape character

I have tried looking in other areas, but I can't seem to figure out what is wrong with what I am doing. I am trying to create a check constraint on a column that is supposed to contain a 9 digit zip code formatted as "00000-0000". This is for an assignment, so the only thing I am allowed to do is create the check constraint.
I have already used the check statement successfully for other columns, but for some reason the statement that I found here on Stack Overflow does not work. The only allowed characters are numbers and the hyphen character ('-').
alter table Student
add constraint student_zip_ck
check (Zip not like '%[0-9\-]%' escape '\');
Since this check constraint was modelled after another (positively-rated) question on Stack Overflow, I don't know what could be wrong. This is the error I receive.
Error starting at line 751 in command:
alter table Student
add constraint student_zip_ck
check (Zip not like '%[0-9\-]%' escape '\')
Error report:
SQL Error: ORA-00604: error occurred at recursive SQL level 1
ORA-01424: missing or illegal character following the escape character
00604. 00000 - "error occurred at recursive SQL level %s"
*Cause: An error occurred while processing a recursive SQL statement
(a statement applying to internal dictionary tables).
*Action: If the situation described in the next error on the stack
can be corrected, do so; otherwise contact Oracle Support.
Does anyone have suggestions, questions, or comments for me?
You're missing anything related to regular expressions, which is probably why it isn't working. Your syntax implies you meant to use them and I would agree with that in this situation. Regular expressions have been available since Oracle 10g so you have to ensure that you're using this version or higher.
The syntax would become:
alter table Student
add constraint student_zip_ck
check (regexp_like(Zip,'^[[:digit:]]{5}-[[:digit:]]{4}$'));
This means:
^ - pin to the beginning of the string
[[:digit:]] - accept only numeric values. This is the POSIX compliant variation and is equivalent to \d.
{5} - 5 times exactly
- - match a hyphen
$ - pin to the end of a string
To make the hyphen and the second for digits optional you need to create a group using (). This makes everything within the parenthesis a single unit, whether it's a string or another regular expression, which you can then apply other operators to. A ? means match 0 or 1 times, and needs to be applied to the group. Putting it all together you get:
regexp_like(Zip,'^[[:digit:]]{5}(-[[:digit:]]{4})?$')
Further Reading
Regular expression documentation 11.2
Reading this (for the first time) there's a fairly similar problem, example 3-1, which uses the Perl type regular expression syntax \d instead of the POSIX and may be of interest.
LIKE operator uses wildcards (% and _) - http://docs.oracle.com/cd/F49540_01/DOC/server.815/a67779/operator.htm#997970
for regular expressions, try REGEXP_LIKE() function - http://www.regular-expressions.info/oracle.html

What do the `s around table/field names in SQL mean?

Here's an SQL statement that phpMyAdmin generated for me:
SELECT * FROM `table_name` WHERE 1
You can see that table_name is surrounded by ` characters.
Why?
It's used to enclose identifiers in MySQL.
It lets them have characters that would be ambiguous or otherwise invalid in normal SQL, such as a space:
`foo bar` - is a valid MySQL identifier
foo bar - is not, since foo is the identifier, and bar is ambiguous
Its used as to escape/enclose characters, in case of a space or some other illegal character in the table or field name.
From the docs:
If an identifier contains
special characters or is a reserved
word, you must quote it whenever you
refer to it. The set of alphanumeric
characters from the current character
set, “_”, and “$” are not special.
Reserved words are listed at Section
8.3, “Reserved Words”.
http://dev.mysql.com/doc/refman/5.0/en/identifiers.html

SQL Server query to create database is giving me an error when I include numeric characters in the database name

I am having trouble running a simple sql query in Microsoft SQL Server 2005 to create a database and im not sure why.
When I run this query
CREATE DATABASE 4444-virtual2
I receive this error
Incorrect syntax near '4444'.
Is there anything in particular that I must specify if I am creating a database table with numeric values in the name? Or am I forgetting something?
Database names need to start with a letter, underscore, at sign or number sign:
The first character must be one of the
following:
A letter as defined by the Unicode
Standard 3.2. The Unicode definition
of letters includes Latin characters
from a through z, from A through Z,
and also letter characters from other
languages.
The underscore (_), at sign (#), or
number sign (#).
Certain symbols at the beginning of an
identifier have special meaning in SQL
Server. A regular identifier that
starts with the at sign always denotes
a local variable or parameter and
cannot be used as the name of any
other type of object. An identifier
that starts with a number sign denotes
a temporary table or procedure. An
identifier that starts with double
number signs (##) denotes a global
temporary object. Although the number
sign or double number sign characters
can be used to begin the names of
other types of objects, we do not
recommend this practice.
Some Transact-SQL functions have names
that start with double at signs (##).
To avoid confusion with these
functions, you should not use names
that start with ##.
Unless you want to delimit every use of the name with double quotes "4444-virtual2" or brackets [4444-virtual2].
You can still create a database with that name, but you need to put it in quotes or brackets. e.g. this works:
CREATE DATABASE [4444-virtual2]
or this:
CREATE DATABASE "4444-virtual2"