blog.x-e.ro / database: page 1 of 6

dbcli

sql :: command line database clients

dbcli as a curious code monkey, i find myself poking around a lot of different types of databases. and as a terminal junkie, i've used a bunch of different commandline tools to connect and interact with them.

in this post i wanna hightlight the dbcli project. it's a suite of db clients each written in python with a similar interface. clients support autocompletion, syntax highlighting, history, the project is opensource, and cross-platform.

i find myself perpetually using them for postgres, mysql, and sqlite but these have clients for ms-sql, redis, and more....

Read: dbcli »

SQL select into statement

sql :: copy and swap database tables in MSSQL

have you ever had a database table that you needed to clone? you can use any number of convoluted methods such as: manually coping and pasting rows in the database manager, running lots of queries/stored procedures, or writing a script to cycle though each row copying data from one table to the next. all of these methods are inferior and far more complex then the method i'm going to show you.

introducing the select into query...
this method should be used if the table you want to clone exists, but the new table douse not. the select into query will create the new table for you, copying all the column names, properties, and data. if you want to make an exact clone of the table you can select * into the new table. but if you only want a few specific columns, you can declare only the ones you want. the syntax is elegantly simple...

SELECT *
INTO [new-table]
FROM [old-table]

Read: SQL select into statement »

inline SQL statements

sql :: nested queries in MSSQL

the world of database programming can be a dark and treacherous journey. when you first set out, your quest seems manageable. but as time progresses your start to realize you need more queries, more tables, etc, etc, to achieve your goal. well today i’m going teach you a little trick that might help you need one less stored procedure then you thought...

sometimes it’s true, you actually do need two queries to get the job done. but other times you can circumvent this by using something like a "join" or "inner join" query to merge two tables together and pull your results from both.

but the optimizer in you says that still isn’t enough...

Read: inline SQL statements »

asp.net pure code database connection

c-sharp :: three different ways

visual studio 2005 has a tool for connecting to a database called a "sqlDataSource". while this tool works, i find myself wanting to create the connection and build or execute stored procedures within my own C# code. this tutorial will show you how to connect to a sql database 3 different ways in asp.net

the first thing is creating a connection with your database. if you understand how connection strings are built, write your own other wise will can use the visual studio database tool to do that for us.

configure database

Read: asp.net pure code database connection »

SQL get last ID

sql :: mssql and mysql

here's a simple SQL script that returns the last row created. this is great for when you add a new record that has an autogenerated id and you need that number.

mssql

CREATE PROCEDURE [dbo].[getLastLogin]
AS
SELECT TOP 1 *
FROM dbo.login
ORDER BY id DESC
GO

mysql

SELECT * FROM `login` 
ORDER BY `id` DESC
LIMIT 1,1;

Read: SQL get last ID »

loading...

loading...