如何在SQL Server中像MySQL中那样使用空值插入标识列 [英] How to insert identity column in SQL Server with null values like in MySQL

查看:73
本文介绍了如何在SQL Server中像MySQL中那样使用空值插入标识列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在SQL Server中像MySQL中那样使用空值插入标识列?

How to insert identity column in SQL Server with null values like in MySQL?

在MySQL中,我们可以放置null并为自动递增的列插入值.如何在SQL Server中执行此操作?

In MySQL we can place null and insert values for auto incremented columns. How to do this in SQL Server?

示例:

在MySql中:

CREATE TABLE `employee` (
  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NOT NULL,
  `phone` VARCHAR(45),
  `salary` DOUBLE NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`)
)
ENGINE = InnoDB;

上表中的id是auto inc,这表示SQL Server中的身份,以便在上表中插入值MySQL支持以下查询

In the above table id is auto inc which means identity in SQL Server, to insert values into the above table MySQL supports the below query

INSERT INTO `employee` (`id`,`name`,`phone`,`salary`) 
VALUES (null,'Harry','9985099850',60000);

MySQL会自动在身份列中插入自动公司ID,但这是SQL Server怎么做?

MySQL automatically insert auto inc id to the identity column, but how to do this is SQL Server?

请指导我如何在SQL Server中执行此操作.我几天来一直在努力使用JDBC在我的应用程序中解决此问题.

Please guide me how to do this in SQL Server. I am struggling here from days to solve this in my application using JDBC.

推荐答案

这应该可以解决问题:

CREATE TABLE employee (
id INTEGER IDENTITY(1,1) PRIMARY KEY,
name VARCHAR(45) NOT NULL,
phone VARCHAR(45),
salary FLOAT DEFAULT 0 NOT NULL
);

当您插入此表时,从插入/选择列表中省略"id",SQL Server将为您处理.

When you insert into this table, omit 'id' from the insert/select list, SQL Server will handle it for you.

例如:

INSERT INTO employee (name,phone,salary) 
VALUES ('Harry','9985099850',60000);

这篇关于如何在SQL Server中像MySQL中那样使用空值插入标识列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆