SQL alias for SELECT statement

Not sure exactly what you try to denote with that syntax, but in almost all RDBMS-es you can use a subquery in the FROM clause (sometimes called an “inline-view”): SELECT.. FROM ( SELECT … FROM … ) my_select WHERE … In advanced “enterprise” RDBMS-es (like oracle, SQL Server, postgresql) you can use common table expressions … Read more

how to pass a null value to a foreign key field?

Just allow column university_id of table user to allow NULL value so you can save nulls. CREATE TABLE user ( uid INT NOT NULL, Name VARCHAR(30) NOT NULL, university_ID INT NULL, — <<== this will allow field to accept NULL CONSTRAINT user_fk FOREIGN KEY (university_ID) REFERENCES university(university_ID) ) UPDATE 1 based on your comment, you … Read more

psycopg2 equivalent of mysqldb.escape_string?

Escaping is automatic, you just have to call: cursor.execute(“query with params %s %s”, (“param1”, “pa’ram2”)) (notice that the python % operator is not used) and the values will be correctly escaped. You can escape manually a variable using extensions.adapt(var), but this would be error prone and not keep into account the connection encoding: it is … Read more

Adding a uniqueidentifier column and adding the default to generate new guid

see this sample: create table test (mycol UniqueIdentifier NOT NULL default newid(), name varchar(100)) insert into test (name) values (‘Roger Medeiros’) select * from test for add a not null field on a populated table you need this. alter table test add mycol2 UniqueIdentifier NOT NULL default newid() with values CREATE UNIQUE NONCLUSTERED INDEX IX_test … Read more

“Order By” using a parameter for the column name

You should be able to do something like this: SELECT * FROM TableName WHERE (Forename LIKE ‘%’ + @SearchValue + ‘%’) OR (Surname LIKE ‘%’ + @SearchValue + ‘%’) OR (@SearchValue=”ALL”) ORDER BY CASE @OrderByColumn WHEN 1 THEN Forename WHEN 2 THEN Surname END; Assign 1 to @OrderByColumn to sort on Forename. Assign 2 to … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)