如何在Sql中删除第一行 [英] How to Delete First Row In Sql

查看:363
本文介绍了如何在Sql中删除第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Gno TransactionDate存款Withdra

1003 2010年7月6日24300.0000空
1003 08/06/2010 NULL 22500.0000
1003 2010年8月21日NULL 618.0000
1003 2010年12月12日10000.0000空
1003 02/02/2011 12500.0000 NULL


我想删除第一行,因为在Withdra Transaction之前不需要
如果第一个交易取款为空,我不需要那一行
如何删除plz快速发送rply我非常急需

显然一个gno有很多问题,但是我们只考虑了两次交易之间.当交易后回赠金额大于1时,我们才考虑过,因此在上述情况下我们不需要第一次交易

你能给我解决方案plz尝试了解
感谢

Gno TransactionDate Deposit Withdra

1003 07/06/2010 24300.0000 NULL
1003 08/06/2010 NULL 22500.0000
1003 21/08/2010 NULL 618.0000
1003 12/12/2010 10000.0000 NULL
1003 02/02/2011 12500.0000 NULL


i want to delete first row because before Withdra Transaction no need
if the first transaction withdraw is null i dont need only that row
how to delete plz send rply quickly i neeed very urgent

Auctually one gno have many taransactions but we have only consider Between date Transactions.when the withdra amount is >1 after transaction only we have consider so above case we dont need first transaction

Can u give me solution plz try to understand
Thanks

推荐答案

Mika用主键将其击中头部.但是只是为了扩展为什么需要主键...

...您可以(并且确实)创建没有密钥的交易,但是要每次正确地识别交易,您都需要某种方式来识别它.就像有人要您去购物中心的停车场,并要您找到特定的汽车一样.如果他们告诉您这是本田,您能可靠地得到合适的汽车吗?他们可以为您提供更多信息,例如年份,型号和颜色,但是总会有多辆与该描述相匹配的汽车.但是,只要有一条信息(车牌号),您就可以识别汽车,因为每辆车都有一个唯一的车牌.

主键是数据库,汽车是车牌.您确实(确实)想要分配主键.通过将列定义为主键,可以防止重复.现在,您可以手动分配主键,但是通过使用主键列上的IDENTITY,数据库将自动分配它.实际上,您无需更改INSERT代码即可做到这一点!

现在添加行时如何获取主键?假设您要添加一行

Mika hit it on the head with the primary key. But just to expand on why you need a primary key...

... you can (and did) create transactions without a key, but to identify a transaction correctly, every time, you need some way to identify it. It''s like if someone asked you to go to a parking lot at a mall, and wanted you to find a certain car. Could you reliably get the right car if they told you it was a Honda? They could give you more information, like the year, model, and color, but there could always be multiple cars matching that description. Yet with just 1 piece of information, the license plate number, you could identify the car, because every car has a unique license plate.

The primary key is to the database what a license plate is to a car. You really (really) want to assign primary keys. By defining a column as a primary key, it will prevent duplicates. Now, you can manually assign a primary key, but by using the IDENTITY on the primary key column, the database will automatically assign it. In fact, you won''t need to change your INSERT code to do this!

Now how to you get the primary key when you add a row? Say you''re adding a row

INSERT INTO tablename (GNO, TransactionDate, Deposit, Withdrawal) VALUES (1003, '9/6/2011', NULL, 200.00)



您只需将以下内容添加到该行中:



You just add the following to the line:

SELECT SCOPE_IDENTITY() from tablename



这样您就可以完成INSERT命令,就像这样:



So you''re complete INSERT command would look like:

INSERT INTO tablename (GNO, TransactionDate, Deposit, Withdrawal) VALUES (1003, '9/6/2011', NULL, 200.00)  SELECT SCOPE_IDENTITY() from tablename



这将自动将主键返回给您.



This will return the primary key to you automatically.


第一个问题是您缺少主键.如果可以的话(例如,带有 IDENTITY [
The first problem is that you''re missing a primary key. If you would have it (for example a column with IDENTITY[^] definition).

You can delete the row for example by specifying all the columns in the WHERE condition and checking that the deletion affects only 1 row, no more.

But at this point I think the best advice is that you add an identity column into the table and define it as the priomary key. Something like:
alter table TableName
add TransactionId int identity(1,1) not null

alter table TableName
add constraint pk_Transaction primary key (TransactionId)


之后,无论如何,您都可以可靠地识别每一行.

接下来您可以研究的是外键:
http://msdn. microsoft.com/en-us/library/aa933117(v=sql.80).aspx [


after that you can reliably identify each row, no matter what.

And the next thing you can investigate is foreign keys: http://msdn.microsoft.com/en-us/library/aa933117(v=sql.80).aspx[^]


这篇关于如何在Sql中删除第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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