2

I am trying to use codeigniter and MS-SQL. I have to use ci_sessions table to automatically store the sessions in the mssql server. The codeigniter have provided the ci_sessions table sql in user-guide as below:

CREATE TABLE IF NOT EXISTS  `ci_sessions` (
    session_id varchar(40) DEFAULT '0' NOT NULL,
    ip_address varchar(16) DEFAULT '0' NOT NULL,
    user_agent varchar(120) NOT NULL,
    last_activity int(10) unsigned DEFAULT 0 NOT NULL,
    user_data text NOT NULL,
    PRIMARY KEY (session_id),
    KEY `last_activity_idx` (`last_activity`)
);

. Here, I have to create the same table in MS SQL Server. What can be corresponding sql for MS SQL SERVER and also for below captcha sql table:

CREATE TABLE captcha (
    captcha_id bigint(13) unsigned NOT NULL auto_increment,
    captcha_time int(10) unsigned NOT NULL,
    ip_address varchar(16) default '0' NOT NULL,
    word varchar(20) NOT NULL,
    PRIMARY KEY `captcha_id` (`captcha_id`),
    KEY `word` (`word`)
);

How to create same table of above mysql query in MS SQL SERVER using sql query

1

1 Answer 1

3

This is an example for the first table -

CREATE TABLE dbo.ci_sessions (
    session_id varchar(40) DEFAULT '0' NOT NULL,
    ip_address varchar(16) DEFAULT '0' NOT NULL,
    user_agent varchar(120) NOT NULL,
    last_activity int DEFAULT 0 NOT NULL,
    user_data varchar(max) NOT NULL, -- use varchar(max) instead of MySQL TEXT data type
    PRIMARY KEY (session_id)
)
GO

-- Simalate MySQL 'UNSIGNED' field option
ALTER TABLE dbo.ci_sessions ADD CONSTRAINT last_activity_unsigned
  CHECK (last_activity >= 0)
GO

CREATE INDEX last_activity_idx
  ON dbo.ci_sessions (last_activity)

Change 'dbo' schema with your schema.

Have a look at these references - CHECK Constraints, Data Types.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.