Web Design Forums

Database Systems Help

Discussion and help on database systems such as MySQL, MSSql, SQLite, PostgreSQL

unix to microsoft - same sql query?



Site of the Month Voting - Now Open. CAST YOUR VOTE NOW!

Reply
 
LinkBack Thread Tools
Old March 6 '06, 01:40 PM (#1)
raspberryh is offline
Retired Staff
 
raspberryh's Avatar
 
Join Date: August 2004
Location: Mountain View, CA
Posts: 859
raspberryh has a spectacular aura aboutraspberryh has a spectacular aura about
unix to microsoft - same sql query?

Hi,
I am used to working on a Unix server using SQL... But right now the site I'm working on is on a Microsft Server using Microsoft SQL... I never really knew if there was much of a difference, but my query which was working fine on the Unix server is giving me errors on the SQL server. It's a create table query. Maybe someone can see the problem right away?

Code:
CREATE TABLE jobs (
  id int(11) NOT NULL auto_increment,
  title varchar(255) NOT NULL default '',
  description mediumtext NOT NULL,
  qualifications mediumtext NOT NULL,
  dateadded int(8) NOT NULL default '0',
  acceptingthru int(8) NOT NULL default '0',
  contact varchar(255) NOT NULL default '',
  phone varchar(255) NOT NULL default '',
  email varchar(255) NOT NULL default '',
  address mediumtext NOT NULL,
  PRIMARY KEY  (id)
) TYPE=MyISAM AUTO_INCREMENT=6 ;
And the error I get is this:
Line 2: Incorrect syntax near 'auto_increment'

I have tried looking up stuff on auto_increment for MS SQL but can't find anything.

Thanks,
Heather
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old March 7 '06, 06:27 AM (#2)
ACW is offline
ACW
WDF Member
 
ACW's Avatar
 
Join Date: July 2004
Posts: 82
ACW is on a distinguished road
Try this...
Code:
CREATE TABLE [jobs] (
  [id] [int] IDENTITY (1, 1),
  title varchar(255) NOT NULL default '',
  description text NOT NULL,
  qualifications text NOT NULL,
  dateadded [datetime] NOT NULL default '0',
  acceptingthru [datetime] NOT NULL default '0',
  contact varchar(255) NOT NULL default '',
  phone varchar(255) NOT NULL default '',
  email varchar(255) NOT NULL default '',
  address text NOT NULL,
  CONSTRAINT [PK_jobs] PRIMARY KEY  CLUSTERED([id])
)
Note that I had to change some data types and it will auto increment the id field by one. Also, brackets ([]) around the table and column names are optional but it is good to use so that MS SQL don't confuse it with a keyword.

Let us know how it turns out.
<edit>grammar</edit>

Last edited by ACW; March 7 '06 at 06:29 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old March 7 '06, 10:06 AM (#3)
raspberryh is offline
Retired Staff
 
raspberryh's Avatar
 
Join Date: August 2004
Location: Mountain View, CA
Posts: 859
raspberryh has a spectacular aura aboutraspberryh has a spectacular aura about
Hey ACW,
Thanks for your reply! So, if brackets tell it that the word is not being used as a keyword, I was wondering why there are brackets around [int] and [datetime]?
Also, I wanted dateadded and acceptingthru to be int fields of size 8 - is that not possible in Microsoft SQL? I already have all the code written (from when it was on the old servers) and I'd have to rewrite all the code if I changed that. Anyways, I like manipulating the numbers myself rather than using the premade date-time functions, I don't know why, I just do.
Thanks,
Heather
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old March 7 '06, 11:27 AM (#4)
ACW is offline
ACW
WDF Member
 
ACW's Avatar
 
Join Date: July 2004
Posts: 82
ACW is on a distinguished road
Quote:
Originally Posted by raspberryh
Hey ACW,
Thanks for your reply! So, if brackets tell it that the word is not being used as a keyword, I was wondering why there are brackets around [int] and [datetime]?
You got me on that one. My statement was based on experience with other commands, such as SELECT statements. Complex queries that use keywords as a table or column name will fail without the brackets so it is a good practice to use them. I hardly ever use SQL to create a table and use Enterprise Manager (EM) instead. I decided to let EM generate the SQL to create the table for me and oddly enough it put brackets on both the names and data types...
Code:
CREATE TABLE [dbo].[jobs] (
	[id] [int] IDENTITY (1, 1) NOT NULL ,
	[title] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AI NOT NULL ,
	[description] [text] COLLATE SQL_Latin1_General_CP1_CI_AI NOT NULL ,
	[qualifications] [text] COLLATE SQL_Latin1_General_CP1_CI_AI NOT NULL ,
	[dateadded] [int] NOT NULL ,
	[acceptingthru] [int] NOT NULL ,
	[contact] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AI NOT NULL ,
	[phone] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AI NOT NULL ,
	[email] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AI NOT NULL ,
	[address] [text] COLLATE SQL_Latin1_General_CP1_CI_AI NOT NULL , 
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Quote:
Originally Posted by raspberryh
Hey ACW,
Also, I wanted dateadded and acceptingthru to be int fields of size 8 - is that not possible in Microsoft SQL? I already have all the code written (from when it was on the old servers) and I'd have to rewrite all the code if I changed that. Anyways, I like manipulating the numbers myself rather than using the premade date-time functions, I don't know why, I just do.
Thanks,
Heather
You should be able to use [int] instead of [datetime]. I'm not sure what int(8) in MySQL (8 bytes or 8 digits?) but in MS SQL you just use int to make an 8 byte integer. The only drawback I see in not using the biult in datetime or smalldatetime is loosing the ability to use some of the built in date functions (although you might be able to convert your int to datetime right in the query).

Last edited by ACW; March 7 '06 at 11:29 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old March 8 '06, 02:30 AM (#5)
smoseley is online now
WDF Moderator
 
smoseley's Avatar
 
Join Date: March 2003
Location: Miami, FL
Posts: 8,719
smoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud of
Some answers and suggestions:

MS-SQL and MySQL are variations on ANSI-92 SQL. Each fills in the gaps with some of their own unique functions and datatypes. Microst SQL Server uses bytes as the basis for all datatypes, so everything increases in increments of powers of 2. You can store something as a varchar(255), for example, but it uses 256 bytes to do so. Microsoft SQL int datatype is the same as a Long Int or Long datatype in most programming languages - it's 4 bytes of signed data (something like 32 billion + and -). In MS-SQL, a datetime or an int using Julian Dates (look it up on Google) is most efficient. To store dates as MMDDYYYY, you should probably use a varchar(8), but then you're using 8 bytes alphanumeric vs. 4 bytes numerical. You can see the inefficiency.

Here's my suggestion for the design of your table:
Code:
CREATE TABLE jobs (
	id int NOT NULL IDENTITY (1, 1) PRIMARY KEY,
	title varchar(128) NOT NULL,
	description varchar(4096) NOT NULL,
	qualifications varchar(2048) NOT NULL,
	date_created datetime NOT NULL DEFAULT GETDATE(),
	date_expires datetime NOT NULL DEFAULT GETDATE(),
	contact_name varchar(128) NOT NULL,
	contact_phone varchar(128) NOT NULL,
	contact_email varchar(128) NOT NULL,
	contact_address varchar(1024) NOT NULL
);
GO
Notice I'm using powers of 2 for all the variable data sizes. This is to maximize efficiency of storage. Also, you're limited to just over 8000 bytes per row, so you don't want to use up your space. I changed the text data fields to varchars, because text is stored as binary data and significantly slows you down. You don't want to use it unless you need more than 8000 bytes (characters) of data per row.

The GETDATE() function is like NOW() or CURRENT_TIMESTAMP - it fills the date field with the current date and time values.

GO is a required batch execution command in MS-SQL (MySQL executes after every semicolon). If you're creating tables that have dependencies upon eachother (FOREIGN KEY / REFERENCES for example) you'll need to put a GO between each CREATE statement. It's a good habit to get into.

Finally, I changed some of yoru column names for clarity. Ideally, you should have contacts in a separate table referenced by jobs (to follow 3rd normal form), but I won't get picky there.

That's about that.... have fun!

Last edited by smoseley; March 8 '06 at 02:31 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old March 9 '06, 10:22 PM (#6)
raspberryh is offline
Retired Staff
 
raspberryh's Avatar
 
Join Date: August 2004
Location: Mountain View, CA
Posts: 859
raspberryh has a spectacular aura aboutraspberryh has a spectacular aura about
Hey Transio,
Thanks for your reply. Wow, I have never thought about everything in so much depth when creating a table for a database. I really like the way you analyze everything to maximize efficiency. Thanks so much for all your suggestions!
Heather
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

  Web Design Forums » Programming Help » Database Systems Help

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
sql query flump PHP 6 May 10 '04 02:24 PM
sql query flump PHP 3 March 2 '04 05:09 PM
Practicing SQL Wired PHP 0 November 2 '03 02:59 AM
SQL Server 2000 backups to a client machine that doesn't have SQL Server TheGAME1264 Database Systems Help 21 August 11 '03 01:34 AM
Nifty SQL Server @@ Properties smoseley Other Languages 0 March 28 '03 03:22 PM

 
User Infomation
Your Avatar

Site Of The Month
Nominate Your Site Now!

Advertisement
WolfCMS.org

Latest Articles
- by RickM
- by bfsog

Advertisement

Partner Links



All times are GMT -4. The time now is 02:01 PM.


WebDesignForums.net is Copyright © 2010 RikeMedia.

SEO by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164