I want to disallow the use of spaces in some text/varchar fields.
Even more, it would be best to have only a set of characters that are allowed to use there, like:
[a-zA-Z0-9_\-]
And I want to make it as a rule to all VARCHAR fields that are members of primary key in their tables.
This should be done on the database level and could throw an exception when trying to insert a wrong record or update one with a change of a key field to invalid value.
Can this be done within the database level? Should I use Pl/Perl for that, or is there any simpler method?
You don't even need stored procedures:
alter table xxx add constraint check_valid_chars check ( your_column ~ '^[a-zA-Z0-9_\-]+$' );
should work.
You can define a domain, look at http://www.postgresql.org/docs/current/interactive/sql-createdomain.html at the bottom, there is an example about US postal code.
Seeing your latest comment you could perhaps use CHECK constraints and regex search?
But you will have to modify the schema (tables) and insert it for each field.
Related
Can we create hidden column in the table. It should be listed only when I specify column name explicitly in select statement.
No, there is no supported and safe way to create a hidden column that's listed only when you specify the column name explicitly. PostgreSQL doesn't provide any user-accessible way to hide user-defined columns from the * wildcard.
You could use any user interface layer / query builder of your choice to do this, though.
(PostgreSQL actually does have hidden columns, as you'll see if you select ctid,xmin,xmax from some_table, but it doesn't allow users to add to the set of hidden columns. It is possible to modify the system catalogs directly to trick PostgreSQl into thinking that a user defined column is a hidden system column, but it's a really bad idea to mess directly with the catalogs, so I won't explain how in detail. If you insist on doing this, read the documentation on pg_attribute ... but understand that you're creating a giant foot gun.).
You can set column permissions so a user can only select some columns, though again you can't say "all except this one", you have to say "I want them to be able to see these ones".
Update: #maybeWeCouldStealAVan has the most sensible suggestion: you probably want a view. Mark that answer as correct, not mine.
You can effectively do this by creating a view and selecting only the columns you wish to show.
Here it is said, that no special effort is need to get a dynamic column family. But I always get an exception, when I try to set a value for an undefined column.
I created a column family like this:
CREATE TABLE places (
latitude double,
longitude double,
name text,
tags text,
PRIMARY KEY (latitude, longitude, name)
)
BTW: I had to define the tags column. Can somebody explain me why? Maybe because all other columns are part of the Index?
Now when inserting data like this:
INSERT INTO places ("latitude","longitude","name","tags") VALUES (49.797888,9.934771,'Test','foo,bar')
it works just fine! But when I try:
INSERT INTO places ("latitude","longitude","name","tags","website") VALUES (49.797888,9.934771,'Test','foo,bar','test.de')
I get following error:
Bad Request: Unknown identifier website
text could not be lexed at line 1, char 21
Which changes are needed so I can dynamically add columns?
I am using Cassandra 1.1.9 with CQL3 with the cqlsh directly on a server.
CQL3 supports dynamic column family but you have to alter the table schema first
ALTER TABLE places ADD website varchar;
Check out the 1.2 documentation and CQL in depth slides
CQL3 requires column metadata to exist. CQL3 is actually an abstraction over the underlying storage rows, so it's not a one-to-one. If you want to use dynamic column names (and there are lots of excellent use cases for them), use the traditional Thrift interface (via the client library of your choice). This will give you full control over what gets stored.
I have database with a column name "State/Province". All the queries and data transfers work properly. But in the "SelectedValue" property of the dropdownlist control, bind expressions throws an error.
When I edit the column name by removing the slash sign, it works well.
So using slash in the column name is not a proper way of naming?
Basically using anything different than:
Alphabets
Numbers (not at start of the column name)
Underscore (_)
is not recommended as it is not a good way to name fields and some datasources might throw errors on other characters.
Some good points about Column Naming convention:
Avoid underscores, they look unnatural and slow the reader down.
Never use a column name that requires [ ]. Shame on Microsoft for
excessive use of ID which requires the use of a table qualifier.
Use Proper Case, descriptive names and don't abbreviate.
Name primary keys with a suffix that denotes it data type.
TableNameID for integer (the preferred choice for all primary keys).
TableNameCode for varchar.
TableNameKey (other data types).
Do not change the spelling of the primary key from a parent table
when it's used in a child table.
Don't use acronyms unless they are well know by programmers or all
employees of your company.
I know it's an old threat, but if you're not the designer of the table and fields but just want to use the data, I would suggest you use:
SELECT * FROM <YOUR TABLE NAME>
You probably notice that SQL Management studio returns a field name for your column like 'State_Province'.
This is the SQL fieldname that you can use in your queries
I have a Database DB with a table name population but with no Primary Key. Means it can have duplication of data. For example: I have 5 families (f1,f2,f3,f4,f5) with different members inside it (and members may have same name). So I can have exactly same type of record in more than 1 row. Now I want to edit just 1 member of the family, but it is editing all the duplicate records. What I want to do is, I just want to Update my Database once and only once. In other words, I want to use UPDATE command to execute just once. How to do it?
I am using Sql Server Express, VS2010, C# 4.0 (if this info matters).
I am pretty sure my problem may sound stupid to some people (probably many). But it is just a dummy of my problem. Any help or suggestion will be greatly appreciated. Thanks
I know it's not exactly what you're asking but seriously, the easiest option is to alter the database to have a primary key and use that. Perhaps an Identity key....
Without that, you could update just one record, but you have no guarantee of which record. This is why primary keys are such as fundamental concept. I suppose this doesn't really matter if they are all the same, so....
If you really want to proceed without a primary key, you need to use the TOP keyword as shown here: How do I update n rows in a table?
Set it to
UPDATE TOP 1 ....
Add an identity column, ID int with auto increment on. Then update using the id's.
CREATE TABLE dbo.Family
(
Id int NOT NULL IDENTITY (1, 1),
FamilyName varchar(50) NULL
)
update dbo.Family set FamilyName = 'xxx' where Id = y
In case you can't add an identity column for some reason:
UPDATE TOP ( 1 ) Families
SET Whatever = 'Your Value', ...
WHERE <Your where clause>
The real answer is: Fix your database design. Every record should have some unique identifier. Add an auto-increment field or something.
To directly answer your question, you can say "update top (1) ..." to only update one record. But without some unique identifier, how do you know which record to update? The program will essentially update a random record. Which takes me back to point 1.
Edit: Whoops, my original answer was for a different engine. Corrected above.
I am doing some homework. The users of my database uses some other attributes, not just the ones that ASP 2.0 automatically created for me when i implemented the login and registration mechanism. But when i try to save the modification displays me an error. Can someone give me a hand?
This is the error:
The error says:
'aspnet_Users' table
- Unable to modify table. ALTER TABLE only allows columns to be added
that can contain nulls, or have a
DEFAULT definition specified, or the
column being added is an identity or
timestamp column, or alternatively if
none of the previous conditions are
satisfied the table must be empty to
allow addition of this column. Column
'kjoptekvoten' cannot be added to
non-empty table 'aspnet_Users' because
it does not satisfy these conditions.
That database was automatically created when i implemented Forms based authentification and registration. The problem now is that that users needs some more attributes. How can i give to it more attributes? What is the easiest way to do it?Does not mind if it is not theorically correct(It is just for a homework).
I would appreciate a lot your help.
Apart form the technicalities on the database side, there is a deeper issue here.
You should not alter the aspnet_Users table because you are bypassing the way the membership 'system' in asp.net is working. Instead, have a look into the Profile mechanism: https://web.archive.org/web/20211020111657/https://www.4guysfromrolla.com/articles/101106-1.aspx
You need to make the new attributes nullable or provide a default value. But you also need to consider how to obtain the values from db. The sql membership provider utilizes an auto generated stored procedure to put data into the membership user instance returned,so just adding the attributes in the table will not be sufficient to get the attribute values to your application. I would use a user attribute table instead.
The error message says it all:
You are adding a new column that can't be Null (checkbox "Allow Nulls" not checked), but as you didn't provide a default value, it will be Null.
So SQL Server can't create the new column.
You can do two things:
a) Create the new column with Nulls allowed.
THEN put a default value in all existing rows:
update aspnet_Users set kjoptekvoten = 0)
...and THEN uncheck "Allow Nulls"
b) Create the new column directly with default values.
I don't know if you can do this in Management Studio, but it's easy in T-SQL:
alter table aspnet_Users
add kjoptekvoten int not null
constraint Name_For_Constraint default(0) with values
This will add the new not nullable column, AND create a constraint with a default value, AND fill the default value in all existing rows (SQL Server will not do this without the "with values" clause).
Normally I just set the column as allow nulls
then do an SQL UPDATE TABLE SET VALUE = whateva
then update the table definition to not allow nulls.