|
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 />
|
|
|
|
|
Hi
I want to create a sql query which meet following criteria but i can't understand how can i write this. I tried a lot but did not successed.
My table structure is as follow.
ATA_Question.
Qid -- Catid -- Subcatid -- SortOrder -- Question --
1 1 1 1 question.
Suppose the table contain 30 question having catid=1 and sort number varies between 1 to 9. Now i want to show the first top 12 question which having catid=1 and sort order in ascending.
If Question 30 having sort order 1 and catid=1 then my query return top 12 question which having sort order in ascending. First it show question having sort order 1 then next sort order 2 and so on and return top 12 question.
How can i write the query for this. Please help me?
I have solved that one
Regards
Rahul
People Laugh on me Because i am Different but i Laugh on them
Because they all are same.
modified on Wednesday, March 26, 2008 2:36 AM
|
|
|
|
|
rahul.net11 wrote: I have solved that one
So, have you solved your problem or not?
If you have, you should modify your post title to indicate the problem is solved and post your solution. Otherwise people will waste their time trying to help you find a solution you have already got.
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
I executed the following query using Visual Basic in the Table Adapter wizard.
SELECT less_time, less_day
FROM tbl_lessons
WHERE (teach_id = 100)
AND (less_status = 1)
AND (less_time = '1/1/1900 3:00:00 PM')
AND (less_day = 2)
In visual basic I use @param1, @param2 and @param3 for the teach_id, less_time and less_day.
When I ran the query in Visual Basic I get the following error, but when I run the identical query using the SQL Server Express I do not get the error. Has anyone seen this error before? Why does it happen? How do I fix it?
Error Source: System.Data
Error Message: Failed to convert paramater value from a dateTime to a timeSpan.
|
|
|
|
|
Take a look at the parameters that you've generated in the wizard. You'll find that one of them is a TimeSpan; and the code is passing in a DateTime. That's what you need to sort out.
|
|
|
|
|
I looked in the wizard and I can't find a place to select a dataType. One of my table columns is a UDT named time that I created from dateTime. I did not create an assembly. Also the dateTime datatype that I pass in is a dateTime that adds a timespan to change the startTime.
Is it possible that the time dataType could cause this? I also increment startTime with timeSpan which is a dateTime dataType in VB.NET. Could this cause the problem?
|
|
|
|
|
Hi please can someone help me.
I've got an application that I've written in VB 2008 Express. To Install the App. I'm using Inno Setup 5.
I would like to have a direct link to the SQL Server 2005 express. So, if the Install checks and finds that the Sql Server Express is not installed then if the user clicks ok it goes direct to installation. I've found the link for .Net Framework 2.0 but no luck for SQL Server Express 2005.
Any help would be greatly appreciated.
Thanks alot
Kris MCP
|
|
|
|
|
|
Hi Dave
Gather you're a bit confused sorry about that. I need a direct link to SQLServer 2005 express.
I've tried going to the web site but when you click to download it shows the error address. They've obviously got problems.
Thanks alot
Kris MCP
|
|
|
|
|
Hi Dave
Just to let you know I've finally found the link it's "http://go.microsoft.com/fwlink/?LinkId=65212">http://go.microsoft.com/fwlink/?LinkId=65212
Took a bit of finding so if anyone ever needs it I've pasted it in.
Thanks for your help.
Kris MCP
|
|
|
|
|
Strange. All the links on those pages worked for me. I clicked on each one before I posted the message.
|
|
|
|
|
Microsoft SQL 2005
---------------------
I created a table name items.
which has to be updated with multiple rows at once, only the ID is Unique.
(more details)
SO when i updated a record there would be six rows with the same items but with different ID.
GIVEN BELOW IS A SAMPLE (6 times)
---------------------------------
ID | Name | Model| Price
---------------------------------
0001 MotorCar Mazda 62000.00
0002 MotorCar Mazda 62000.00
0003 MotorCar Mazda 62000.00
0004 MotorCar Mazda 62000.00
0005 MotorCar Mazda 62000.00
0006 MotorCar Mazda 62000.00
I guess a range(eg.1 to 15) is required to validate the number of time You may required to update a record.
Any suggestion on this would be great indeed.
thanks.
so much of happy ending...
|
|
|
|
|
Hi,
I'm trying to replace the ' character inside a varchar field with another character (such as `). For example, in the database I have a record of the form "Hello, I'm happy to meet you". I apply the REPLACE function to this record: REPLACE(<table_name>.<field_name>, '''', '`'). Theoretically, the result should be "Hello, I`m happy to meet you". But it's not. The record is not affected by the REPLACE. Does anyone know a logical explanation? Or is this voodoo? :P
Thanks
|
|
|
|
|
But you already have the correct character in the field, why replace it with an incorrect one?
Are you setting the field to the new value?
<table_name>.<field_name> = REPLACE(<table_name>.<field_name>, '''', '`')
|
|
|
|
|
Why is ` incorrect? I want to change the ' inside the varchar field because i want to generate an insert command, for example, inside a text file. If i don't change it, the command in the file would be something like this: INSERT INTO some_table VALUES (..., 'Hello, I'm happy to meet you')
And my problem is that the string ends right after "I". I also tried doubling the ' character but it didn't work that way either.
|
|
|
|
|
th3_d3vil wrote: doubling the ' character but it didn't work
It should. What is happening?
Are you storing commands in a file? Why not store the data as CSV and have a parameterized insert command?
What task are you trying to accomplish?
|
|
|
|
|
I found the problem. In the database I found ’ (ASCII code 22) instead of ' (ASCII code 39) and it seems that writing the results to the file using bcp changes the ’ (22) character with ' (39) . It's funny that I don't seem to find ’ (22) on my keyboard . Anyway thanks for your help
|
|
|
|
|
I'm using SQL Server 2000 and performing some insertions. Now I'm facing very slow response and some time it hangs in the middle.
Please take me out from this problem.
Thanks
Syed Shahid Hussain
|
|
|
|
|
I'm always amazed by questions like this. How do you hope for us to help you ? We have no idea what your code looks like, how can we comment on it ?
What does 'hangs in the middle' mean, your app becomes unresponsive ?
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
hi
Are you Sure that your insert statement works?
show us the SQl statement that you use in the insert statement?
Is the Memory of your machine enough to handle SQl server ? and how big it is ?
what is you network Bandwith if you are not using it locally ?
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
|
Hello friends,
I am trying to write a query to bring records from my ACCESS database and the field type of the column included in the where clause is date/time.
my query was:
command = "Select * FROM tblIncomingLetters where InwardDate Between " + dateTimePicker1.Value.Date+ " AND " + dateTimePicker2.Value.Date;
I am sending this query to another fucntion that will take this query as a command txt
but it is constantly giving me the error of missing operator
I was wondering if anyone of you can help me in resolving the issue.
Looking forward for your input.
Sadaf
|
|
|
|
|
TRy using parameterised queries, then you can be sure that your data is going to be presented in a format your source understands.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Thanks for ur co-operation but let me briefly describe what i want
my query is changing based on the filters used .
to accomodate this situation , i have made a function GetData() which is being called after the user clicks on the button (btnFind) and they query is decided based on the filters specified by the user which means the parameters also change .
---Function GetData()----
private void GetData(string selectCommand)
{
// Specify a connection string. Replace the given value with a
// valid connection string for a Northwind SQL Server sample
// database accessible to your system.
string currentDirectory = System.Environment.CurrentDirectory;
OleDbConnection con = new OleDbConnection();
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + currentDirectory + "\\dbLetters.mdb';Persist Security Info=False";
// Create a new data adapter based on the specified query.
dataAdapter= new OleDbDataAdapter(selectCommand, con);
// Create a command builder to generate SQL update, insert, and
// delete commands based on selectCommand. These are used to
// update the database.
OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(dataAdapter);
// Populate a new data table and bind it to the BindingSource.
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource1.DataSource = table;
// Resize the DataGridView columns to fit the newly loaded content.
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
dataGridView1.DataSource = bindingSource1;
}
-------END--------
-----query specified-----
string command = "";
#region Sender of the letter is specified
else if (chkFrom.Checked == true && chkDate.Checked == false && chkSubject.Checked == false)
{
if (cmbFrom.SelectedItem.ToString() == "Others")
{
command = "Select * from tblIncomingLetters where From LIKE ='" + this.txtFrom.Text+ "'";
}
else
{
command = "Select * from tblIncomingLetters where From LIKE '" + cmbFrom.SelectedItem.ToString() + "'";
}
}
#endregion
#region date is specified
else if (chkDate.Checked == true && chkFrom.Checked == false && chkSubject.Checked == false)
{
command = "Select * FROM tblIncomingLetters where InwardDate Between " + dateTimePicker1.Value.Date+ " AND " + dateTimePicker2.Value.Date;
}
#endregion
#region subject is specfied
else if (chkDate.Checked == false && chkFrom.Checked == false && chkSubject.Checked == true)
{
command = "Select * from tblIncomingLetters where Subject LIKE '" + this.txtSubject.Text + "'";
}
#endregion
-------
Now plz tell me how to solve it or is there any other better way to achieve the same result.
I shall be really thankful 2 u
Sadaf
|
|
|
|
|
First of all, what I said stands. Use parameterised queries if you must do SQL in your presentation layer. I think it's inexcusable, but that's another discussion.
Second, read this[^]. Your code is a disaster, and a malicious user can easily comprimise your database, erase it, or gain access to any data they want from it.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
there is one thing I forgot to mention which is that
once the query to retrieve the data from database is decided (string command is formed) , I call the GetData function passing it the string command
GetData(command);
Kidnly correct me if you think this can done in other ways!
Sadaf
|
|
|
|