How to specify input format in SQL create table? - sql

I'm new in SQL and I need to create table with specified field format. How to add CHECK condition that will assure that input will be formatted e.g.
[LLLDD]
where L is a letter and D is a digit?

Try this if you are adding the constraint on a new table
CONSTRAINT ck_data_checker CHECK ([columnName] LIKE ('[A-Z][A-Z][A-Z][0-9][0-9]'))
Try this if you are adding the constraint on existing table
ALTER TABLE tableName
ADD CONSTRAINT ck_data_checker CHECK ([columnName] LIKE ('[A-Z][A-Z][A-Z][0-9][0-9]'))

Try this: http://sqlfiddle.com/#!6/3974b
create table test (
field1 char(5),
check (field1 like '[a-z][a-z][a-z][0-9][0-9]')
);
insert into test values ('ttt09'); --this will succeed
If you were to change the insert to:
insert into test values ('testi'); -- this will fail
insert into test values ('12345'); -- this will fail

I'm no sql server expert, but I think you can add a LIKE with a regular expression. Have a look at these websites
https://msdn.microsoft.com/en-us/library/ms179859.aspx
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/dc127433-2982-4065-b290-f411a075a694/use-regular-expressions-to-check-sql-server-2012-table-fields?forum=databasedesign

Related

How can I change column comments in existing Hive table without including new column name and type?

I want to change the column comments on an existing hive table using hive 0.13. This works:
create table test (mycolumn int);
alter table test change mycolumn mycolumn int comment 'hello';
But I can't find a way to do this without repeating the name of the column and the type, both of which are irrelevant to the change. For example:
alter table test change mycolumn comment 'hello'; leads to an error.
If this was for one column it would not be a big deal but I want to do this for large numbers of columns in tables that were not commented. I know this could be done with a script that simply copies the column name and its type but would be nice to know if there were something simpler. Thanks
You can do this using ALTER command.
CREATE TABLE my_table
(id INT COMMENT 'id comment',
name STRING comment 'name comment');
-- change column comment as below.
ALTER TABLE my_table CHANGE id id INT COMMENT 'another comment';
-- see changed column
DESC EXTENDED my_table;
ALTER TABLE test_change CHANGE a1 a1 INT COMMENT 'this is column a1';
Directly its not supported to edit column property for comment the other way around is
alter table dev.travel change num2 clm_num1 int comment 'a new column added';
Now I want to change above one lets do ;
alter table dev.tkt change clm_num1 num2 int comment 'a new column added';
alter table dev.tkt change num2 clm_num1 int comment 'a new column added with new comment';

can't insert a date in my tablle in Oracle sql

I'm new in the SQL programming, I've created a table where I want to insert a a date like
create table person (
PANr integer not null,
name Varchar(10),
HNr integer not null,
stuff_date date,
constraint P_NR primary key (PANr)
);
insert into Personen values ('4711','Andreas''15','31.10.1958');
the creating of the table works but the insert command gives this error:
SQL Error: ORA-01843: not a valid month
01843. 00000 - "not a valid month"
any Idea what's wrong here ??
thanks in advance
Use TO_DATE('31.10.1958', 'DD.MM.YYYY').
You have another error.
insert into person
values
('4711','Andreas''15','31.10.1958');
First, you are quoting your integer values. That may or may not cause a problem, but it's a bad idea. Second, you need a comma between Andreas and 15.
If you don't want to use the TO_DATE function you can use the following:
ALTER SESSION SET nls_date_format='yyyy-mm-dd';
Now, your insert statement would look like this:
INSERT INTO PERSONEN VALUES('4711','Andreas','15','1958-10-31');
If you want to be more specific you can define the date format like this:
ALTER SESSION SET nls_date_format='yyyy-mm-dd hh24:mi:ss';
P.S. Please, pay attention on #Dan Bracuk answer.

Insert empty string into INT column for SQL Server

A SAMPLE table has only one column ID of type int, default null.
In Oracle when I do:
insert into SAMPLE (ID) values ('');
the new record is added with blank value. But in SQL Server 2008, when I run the same insert statement, the new record has the value of 0.
Is there a way to force SQL Server 2008 to default blank string to NULL instead of 0 (for numerical type of columns)?
Assuming that your INSERT statement is part of a stored procedure re-used in many places of your application (or, perhaps, is a batch always constructed by the same part of the client code) and that the inserted value is a number passed as a string argument, you could modify the INSERT like this:
INSERT INTO SAMPLE (ID) VALUES (NULLIF(#argument, ''));
Use NULL instead.
insert into SAMPLE (ID) values (NULL);
How about another idea - define an INSTEAD OF INSERT Trigger.
Despite the fact that you're trying to insert a string, with this the operation is "intercepted", empty string is replaced by NULL, and the insert succeeds.
If you define this trigger on your table, then you can continue to insert empty string as before, with no other changes.
Edit: As Martin Smith points out, this effectively is a comparison to 0 (the equivalent of empty string as an int) meaning you won't be able to store 0 in this table. I leave this answer here in case that's acceptable to your situation - either that or re-do all your queries!
CREATE TRIGGER EmptyStringTrigger
ON [SAMPLE]
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO [SAMPLE](ID)
SELECT CASE
WHEN ID = '' THEN NULL
ELSE ID
END
FROM inserted
END
SQL Fiddle example
You can't insert a 'string' into a int column. Oracle must be just handling that for you.
Just try inserting NULL if that's what you need.
insert into SAMPLE (ID) values (NULL);
One more option
insert into SAMPLE (ID) values (DEFAULT)

SQL CHECK CONSTRAINT VARCHAR

create table table1
(
column1 varchar2(8)
check constraint column1_ch check ........
);
How do I do a check for a data that the first 4 char is a specific set of alphabets while the last 4 is numbers? and as well as a range of values.
examples, data can be ABCD2121, ABCD1111.
range - ABCD0001 to ABCD9999
So 'ABCD' is fixed while the numbers are changing.
I've foudn online about using '[]" to define the numbers but i'm not able to integrate it into my constraint.
Thanks
The easiest way is to do this using a regular expression:
alter table table1
add constraint chck_code check (regexp_like(column1, '(ABCD)[0-9]{4}') );
If you've got a fixed set of prefixes, use regexp_like and enumerate the prefix list:
alter table test_1
add constraint chk_col1 check(regexp_like(column1, '(ABCD|EFGH)[0-9]{4}'));
This will allow ABCD and EFGH as prefixes, followed by exactly 4 digits.
Your check condition should be as follows:
(column1 LIKE 'ABCD[0-9][0-9][0-9][1-9]')
Edit: Modified to use a set prefix vs. a range for the alpha characters.
Here's a solution using Microsoft SQL Server that illistrates this:
DECLARE #MyTable TABLE
(column1 varchar(8) check (column1 LIKE 'ABCD[0-9][0-9][0-9][1-9]'))
INSERT INTO #MyTable (column1)
SELECT 'ABCD0000'
UNION SELECT 'ABCD2121'
UNION SELECT 'ABCD1111';
SELECT *
FROM #MyTable;
INSERT INTO #MyTable (column1)
SELECT 'ABCD000A'; --<== Fails!
INSERT INTO #MyTable (column1)
SELECT 'ABCD221'; --<== Fails!

sql: insert value

I have a problem about inserting values into the table with sqlite.
supposing the table:
create table test
{
KeyName varchar(50) primary key,
KeyValue varchar (255)
};
I want to insert data like ('john', 'friend'), but I don't know whether the 'john' existed.
Currently I solve it:
using select * where KeyName = "john"
according the result from list 1, I use insert or update;
I'd like to know whether there is better solution?
thanks
you can use insert or replace which replaces the record if it already exists.
so you query be INSERT OR REPLACE INTO
check this link : http://www.sqlite.org/lang_conflict.html