Database Operations change primary keys, insert loops, and database primary keys

Source: Internet
Author: User

Database Operations change primary keys, insert loops, and database primary keys
-- Alter table HistoryData drop constraint PK_HistoryData -- delete the primary key
Alter table HistoryData add constraint PK_HistoryData primary key (CollectionTime, Code) -- change the primary key




Delete from HistoryData where CollectionTime between '2017-09-01 00:00:00 'and '2017-11-01 00:00:00' and ID not in (
Select id from (
Select Code, CollectionTime, max (ID) id from HistoryData
Where CollectionTime between '2017-09-01 00:00:00 'and '2017-11-01 00:00:00' group by Code, CollectionTime))
-- Delete duplicate columns.




Loop insert
Declare @ sDate varchar (20)
Declare @ eDate varchar (20)
Declare @ mindate datetime
Declare @ maxdate datetime
Declare @ mindateStr varchar (20)
Declare @ maxdateStr varchar (20)


Set @ sDate = '2017-1-16 06:30:00'
Set @ eDate = '2017-1-16 08:35:00'




Set @ mindate = cast (@ sDate as datetime)
Set @ maxdate = dateadd (mi, 5, @ mindate) -- mi indicates minute, hh hour, day


While @ maxdate <= cast (@ eDate as datetime)
Begin
Set @ mindateStr = convert (varchar (19), @ mindate, 120)
Set @ maxdateStr = convert (varchar (19), @ maxdate, 120)
-- Print (@ mindateStr)
Print (@ maxdateStr)

-- Number of inserts
-- Insert
Insert into HistoryData values (0.1337, '20160301', @ maxdate, 'Iron ', 1, 9001, 0.1337, '20160301', 'n', '', '20160301 ', 'N', 'n', 0, 'n', 'n ')
Set @ mindate = @ maxdate
Set @ maxdate = dateadd (mi, 5, @ maxdate)
End

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.