Click here to Skip to main content
15,920,896 members
Home / Discussions / Database
   

Database

 
GeneralRe: How can I use a column alias in a where clause? Pin
Dobromir Dimitrov25-Mar-08 22:13
Dobromir Dimitrov25-Mar-08 22:13 
Questionhow can I get top(N) rows for each group Pin
Dobromir Dimitrov25-Mar-08 21:31
Dobromir Dimitrov25-Mar-08 21:31 
GeneralRe: how can I get top(N) rows for each group Pin
i.j.russell29-Mar-08 9:17
i.j.russell29-Mar-08 9:17 
GeneralGive example : How I can Use index in sql table Pin
Ashish Kumar Vyas25-Mar-08 21:06
Ashish Kumar Vyas25-Mar-08 21:06 
GeneralRe: Give example : How I can Use index in sql table Pin
Ashfield25-Mar-08 22:14
Ashfield25-Mar-08 22:14 
GeneralRe: Give example : How I can Use index in sql table Pin
Ashish Kumar Vyas26-Mar-08 19:06
Ashish Kumar Vyas26-Mar-08 19:06 
GeneralRe: Give example : How I can Use index in sql table Pin
Ashfield26-Mar-08 21:53
Ashfield26-Mar-08 21:53 
GeneralRe: Give example : How I can Use index in sql table Pin
Justin Porteous27-Mar-08 9:03
Justin Porteous27-Mar-08 9:03 
Create a Database

To create a database:

CREATE DATABASE database_name<br />


Create a Table

To create a table in a database:

CREATE TABLE table_name<br />
(<br />
column_name1 data_type,<br />
column_name2 data_type,<br />
.......<br />
)<br />

Example

This example demonstrates how you can create a table named "Person", with four columns. The column names will be "LastName", "FirstName", "Address", and "Age":

CREATE TABLE Person <br />
(<br />
LastName varchar,<br />
FirstName varchar,<br />
Address varchar,<br />
Age int<br />
)


This example demonstrates how you can specify a maximum length for some columns:

CREATE TABLE Person <br />
(<br />
LastName varchar(30),<br />
FirstName varchar,<br />
Address varchar,<br />
Age int(3) <br />
)


The data type specifies what type of data the column can hold. The table below contains the most common data types in SQL:
Data Type 	Description
integer(size)
int(size)
smallint(size)
tinyint(size) 	Hold integers only. The maximum number of digits are specified in parenthesis.
decimal(size,d)
numeric(size,d) 	Hold numbers with fractions. The maximum number of digits are specified in "size". The maximum number of digits to the right of the decimal is specified in "d".
char(size) 	Holds a fixed length string (can contain letters, numbers, and special characters). The fixed size is specified in parenthesis.
varchar(size) 	Holds a variable length string (can contain letters, numbers, and special characters). The maximum size is specified in parenthesis.
date(yyyymmdd) 	Holds a date


Create Index

Indices are created in an existing table to locate rows more quickly and efficiently. It is possible to create an index on one or more columns of a table, and each index is given a name. The users cannot see the indexes, they are just used to speed up queries.
Note: Updating a table containing indexes takes more time than updating a table without, this is because the indexes also need an update. So, it is a good idea to create indexes only on columns that are often used for a search.

A Unique Index

Creates a unique index on a table. A unique index means that two rows cannot have the same index value.

CREATE UNIQUE INDEX index_name
ON table_name (column_name)

The "column_name" specifies the column you want indexed.

A Simple Index

Creates a simple index on a table. When the UNIQUE keyword is omitted, duplicate values are allowed.

CREATE INDEX index_name<br />
ON table_name (column_name)<br />

The "column_name" specifies the column you want indexed.
Example

This example creates a simple index, named "PersonIndex", on the LastName field of the Person table:
<br />
CREATE INDEX PersonIndex<br />
ON Person (LastName)


If you want to index the values in a column in descending order, you can add the reserved word DESC after the column name:
<br />
CREATE INDEX PersonIndex<br />
ON Person (LastName DESC)


If you want to index more than one column you can list the column names within the parentheses, separated by commas:

CREATE INDEX PersonIndex<br />
ON Person (LastName, FirstName)<br />

QuestionSql Query? [modified] Pin
rahul.net1125-Mar-08 19:49
rahul.net1125-Mar-08 19:49 
GeneralRe: Sql Query? Pin
pmarfleet25-Mar-08 20:46
pmarfleet25-Mar-08 20:46 
GeneralSQL Execution Error Failed to Convert from dateTime to TimeSpan Pin
AAGTHosting25-Mar-08 10:37
AAGTHosting25-Mar-08 10:37 
GeneralRe: SQL Execution Error Failed to Convert from dateTime to TimeSpan Pin
Pete O'Hanlon25-Mar-08 11:25
mvePete O'Hanlon25-Mar-08 11:25 
GeneralRe: SQL Execution Error Failed to Convert from dateTime to TimeSpan Pin
AAGTHosting25-Mar-08 11:40
AAGTHosting25-Mar-08 11:40 
Questiondirect link to install SQL Server 2005 Express Pin
KrisnNala25-Mar-08 8:59
KrisnNala25-Mar-08 8:59 
GeneralRe: direct link to install SQL Server 2005 Express Pin
Dave Kreskowiak25-Mar-08 10:00
mveDave Kreskowiak25-Mar-08 10:00 
GeneralRe: direct link to install SQL Server 2005 Express Pin
KrisnNala25-Mar-08 22:10
KrisnNala25-Mar-08 22:10 
GeneralRe: direct link to install SQL Server 2005 Express found Solution Pin
KrisnNala26-Mar-08 2:18
KrisnNala26-Mar-08 2:18 
GeneralRe: direct link to install SQL Server 2005 Express Pin
Dave Kreskowiak26-Mar-08 13:34
mveDave Kreskowiak26-Mar-08 13:34 
GeneralInsert bulk data Pin
zafax_25-Mar-08 7:45
zafax_25-Mar-08 7:45 
QuestionREPLACE function doesn't always work Pin
adi.rusu25-Mar-08 5:56
adi.rusu25-Mar-08 5:56 
GeneralRe: REPLACE function doesn't always work Pin
PIEBALDconsult25-Mar-08 6:29
mvePIEBALDconsult25-Mar-08 6:29 
GeneralRe: REPLACE function doesn't always work Pin
adi.rusu25-Mar-08 6:51
adi.rusu25-Mar-08 6:51 
GeneralRe: REPLACE function doesn't always work Pin
PIEBALDconsult25-Mar-08 13:12
mvePIEBALDconsult25-Mar-08 13:12 
GeneralRe: REPLACE function doesn't always work Pin
adi.rusu25-Mar-08 22:17
adi.rusu25-Mar-08 22:17 
QuestionInsert Quert Creates problem Pin
Syed Shahid Hussain24-Mar-08 21:38
Syed Shahid Hussain24-Mar-08 21:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.