Autoincrement syntax error - syntax

The Sybase manual and plenty of examples claim I can write:
create table run_log (
run_id integer not null default autoincrement
);
But Sybase 15 says there is a syntax error on the default

Sybase documentation isn't great, and it turns out there's Sybase ASA, some other product they bought, and Sybase ASE (Server Enterprise) which is what most people would think of as Sybase.
For ASE15, it's not autoincrement, it's identity, which must be on numeric, not integer
create table run_log (
runid numeric(12,0) identity
)

Related

how to create check constraint in maria DB for checking char type multiple values?

i have working for my college project of QuestionAnswer site like StackOverflow but i have face one problem in creating tables in Maria DB
i use latest version of XAMPP which by default use maria DB instead of MYSQL
so i want to create post table which contain post type (p_type) like (question, answer, comment),
i use following code
create table post
(
p_type char(1) check(p_type = 'q' OR p_type = 'a' OR p_type = 'c')
);
and i use InnoDB storage engine and Maria DB version is 10.1.30
but when i insert other character like (s,z,x) is store in database
which means Check constraint is not applied,
i also visited the Maria DB manual for Check Constraint but there is no any example related to Char type.
so any answer would be appreciated
thanks in advance
Your syntax is fine, the version is wrong. MariaDB versions before 10.2 (and all available versions of MySQL) parse the CHECK clause, but ignore it completely. MariaDB 10.2 performs the actual check:
MariaDB [test]> create table post ( p_type char(1) check(p_type = 'q' OR p_type = 'a' OR p_type = 'c') );
Query OK, 0 rows affected (0.18 sec)
MariaDB [test]> insert into post values ('s');
ERROR 4025 (23000): CONSTRAINT `p_type` failed for `test`.`post`
MariaDB [test]> insert into post values ('q');
Query OK, 1 row affected (0.04 sec)

JdbcSQLException executing PostgreSQL query with 'MATCH simple' in H2

I'm trying to run in development mode (with H2) a setup currently used in production with a PostgreSQL database and I get an error. It would be best if I could reuse the production SQL without any change to it.
Using this setup:
# H2 Database
spring.datasource.datasource-class-name=org.h2.jdbcx.JdbcDataSource
spring.datasource.url=jdbc:h2:mem:userregistry;DB_CLOSE_DELAY=-1;MODE=PostgreSQL
This query:
CREATE TABLE IF NOT EXISTS users.user_userrole (
user_signum VARCHAR(20) NOT NULL,
role VARCHAR(255) NOT NULL,
CONSTRAINT user_userrole_pk PRIMARY KEY (user_signum, role),
CONSTRAINT user_fk FOREIGN KEY (user_signum) REFERENCES users.user (signum) MATCH SIMPLE,
CONSTRAINT role_fk FOREIGN KEY (role) REFERENCES users.userrole
(role_name) MATCH SIMPLE
);
Raises this exception:
org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "<<SQL OMITTED FOR BREVITY>>";
expected "INDEX, ON, NOT, DEFERRABLE, NOCHECK, CHECK, ,, )"; [42001-185]
Note that I am already using Mode=PostgreSQL. Any ideas?
Thanks
The H2 database does not support MATCH SIMPLE (same as Oracle, MS SQL Server, Apache Derby).

set locale on Oracle connection

In my company's product, we retrieve results a page at a time from the database. Because of this all filtering and sorting must be done on the database. One of the problems is coded values. For filtering and sorting to work properly, the code values need to be translated to locale specific labels in the query.
My plan is to use a table similar to the following:
t_code_to_label (
type varchar2(10),
locale varchar2(10),
code varchar2(10),
label varchar2(50)
)
The first three columns are the primary (unique) key.
When using this table, you would see something like this
select ent.name, ent.ent_type, entlabel.label as ent_type_label
from t_entitlements ent
join t_code_to_label entlabel on entlabel.type='entlabel' and entlabel.locale=currentLocale() and entlabel.code=ent.ent_type
The problem is that currentLocale() is something that I made up. How can I on the Java side of a JDBC connection do something to set the locale for the Connection object that I have that I can read on the Oracle side in a simple function. Ideally this is true locale support by Oracle but I can't find such a thing.
I am using Oracle 10g and 11g.
Are you talking about the NLS_LANGUAGE setting of the Oracle database? Would you (from the client side) like to dictate the usage of a particular NLS_LANGUAGE by Oracle database?
Then maybe this would work: Set Oracle NLS_LANGUAGE from java in a webapp
If you want an "all american" session you could do you could do something like:
ALTER SESSION SET NLS_LANGUAGE= 'AMERICAN' NLS_TERRITORY= 'AMERICA'
NLS_CURRENCY= '$' NLS_ISO_CURRENCY= 'AMERICA'
NLS_NUMERIC_CHARACTERS= '.,' NLS_CALENDAR= 'GREGORIAN'
NLS_DATE_FORMAT= 'DD-MON-RR' NLS_DATE_LANGUAGE= 'AMERICAN'
NLS_SORT= 'BINARY'

ORA-00907 while trying to create a table with automatic column

I'm attempting to create a table with an automatic column, the value of which is computed using a function I've defined. However, when I try to create the table I keep getting ora-00907: Missing right parenthesis. Can anyone help?
Here is the CREATE code:
CREATE TABLE NEW_EMP2 (
SSN CHAR(9),
EMP_NUM2 CHAR(5) automatic as newemp2id(SSN),
Fname VARCHAR2(15),
Lname VARCHAR2(15),
Bdate DATE
)
Here is the code for the function newemp2id:
CREATE OR REPLACE FUNCTION newemp2id (i_ssn NCHAR) RETURN NCHAR
IS
BEGIN
RETURN 'E'||(1000+SUBSTR(i_ssn,6,4));
END
Any help on this would be greatly appreciated, thanks!
UPDATE: I'm using Oracle Express Edition on a Windows Vista machine, in case that makes any difference.
I hadn't heard of the syntax prior to this, but all I could find is this PDF for Oracle RDB. RDB was/is a separate product for Oracle databases... Confirmed - not supported on 10g
Use a BEFORE INSERT trigger instead, because I don't believe the syntax you're using is valid for Oracle Express (10g effectively) - there's no mention in the CREATE TABLE or ALTER TABLE documentation.
I'm not fond of using triggers, I'd prefer to have a single stored procedure for inserting into given table(s) & only allow anyone to use the procedure rather than direct table access...
CREATE OR REPLACE TRIGGER newemp2_before_insert
BEFORE INSERT
ON new_mep2
FOR EACH ROW
BEGIN
-- Update created_by field to the username of the person performing the INSERT
:new.emp_num2 := newemp2id(new.ssn)
END;
Though frankly, this is overcomplicated when it could be handled in a view:
CREATE VIEW vw_emp AS
SELECT t.ssn,
'E'||(1000+SUBSTR(i_ssn,6,4)) AS emp_num2
FROM NEW_EMP2 t
What's an automatic column supposed to be? Did you mean a purely computed i.e. virtual column? Then your statement should look like this:
CREATE TABLE NEW_EMP2 (
SSN CHAR(9),
EMP_NUM2 CHAR(5) GENERATED ALWAYS AS ( newemp2id(SSN) ) VIRTUAL,
Fname VARCHAR2(15),
Lname VARCHAR2(15),
Bdate DATE
)
And your functions need to declared deterministic:
CREATE OR REPLACE FUNCTION newemp2id (i_ssn NCHAR) RETURN NCHAR DETERMINISTIC
IS
BEGIN
RETURN 'E'||(1000+SUBSTR(i_ssn,6,4));
END
If I'm not mistaken, virtual columns were introduced with Oracle 11g.
Oracle Express is Oracle 10g.
According to the manual (http://download-uk.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_7002.htm#i2095331) there is no "automatic" keyword and Oracle 10 has never supported "computed columns"
Oracle 11g supports virtual columns, but they are created using GENERATED ALWAYS, not even Oracle 11g has an automatic keyword
Why do you think this should work in Oracle?

Why is oracle spewing bad table metadata?

I'm using DBVisualizer to extract DDL from an Oracle 10.2 DB. I'm getting odd instances of repeated columns in constraints, or repeated constraints in the generated DDL. At first I chalked it up to a bug in DBVisualizer, but I tried using Apache DDLUtils against the DB and it started throwing errors which investigation revealed to be caused by the same problem. The table metadata being returned by Oracle appears to have multiple entries for some FK constraints.
I can find no reference to this sort of thing from my google searches and I was wondering if anyone else had seen the same thing. Is this a bug in the Oracle driver, or does the metadata contain extra information which is being dropped when my tools access it, resulting in confusion on the part of the tools...
Here is an example (truncated) DDL output from
CREATE TABLE ARTIST
(
ID INTEGER NOT NULL,
FIRST_NAME VARCHAR2( 128 ),
LAST_NAME VARCHAR2( 128 ),
CONSTRAINT ARTIST_ID_PK PRIMARY KEY( ID ),
CONSTRAINT ARTIST_CONTENT_ID_FK FOREIGN KEY( ID, ID, ID ) REFERENCES CMS_CONTENT( CONTENT_ID, CONTENT_ID, CONTENT_ID )
-- note the multiple instances of ID and CONTENT_ID in the above line
-- rest assured there is nothing bizarre about the foreign table CMS_CONTENT
)
I'm attempting to find a Java example which can show the behaviour, and will update the question when I have a concrete example.
You can try the built-in Oracle DBMS_METADATA.GET_DDL('TABLE','ARTIST') and see if that resolves the issue (ie whether it is a bug in the tools or the DB).
You can look at the data_dictionary tables too. In this case, ALL_CONSTRAINTS and ALL_CONS_COLUMNS.
select ac.owner, ac.constraint_name, ac.table_name, ac.r_owner, ac.r_constraint_name,
acc.column_name, acc.position
from all_constraints ac join all_cons_columns acc on
(ac.owner = acc.owner and ac.constraint_name = acc.constraint_name)
where ac.table_name = 'ARTIST'
and ac.constraint_type = 'R'
I'd suspect that it is a bug in the tools, and they've missed a join on the owning schema and you are picking up the same table/constraint but in another user's schema.
As far as I can see, dbvis (6.5.7) uses own code when you use the 'DDL' tab and it uses dbms_metadata when using the tab 'DDL with Storage'.
Does this make a difference for you ?
Ronald

Resources