From the 5th century AD, north Britain was divided into a series of petty kingdoms. Of these, the four most important were those of the Picts in the north-east, the Scots of Dál Riata in the west, the Britons of Strathclyde in the south-west and the Anglian kingdom of Bernicia (which united with Deira to form Northumbria in 653) in the south-east, stretching into modern northern England. In AD 793, ferocious Viking raids began on monasteries such as those at Iona and Lindisfarne, creating fear and confusion across the kingdoms of north Britain. Orkney, Shetland and the Western Isles eventually fell to the Norsemen. These threats may have speeded up a long-term process of gaelicisation of the Pictish kingdoms, which adopted Gaelic language and customs. There was also a merger of the Gaelic and Pictish kingdoms, although historians debate whether it was a Pictish takeover of Dál Riata, or the other way round. This culminated in the rise of Cínaed mac Ailpín (Kenneth MacAlpin) as "king of the Picts" in the 840s (traditionally dated to 843), which brought to power the House of Alpin. When he died as king of the combined kingdom in 900 one of his successors, Domnall II (Donald II), was the first man to be called rí Alban (King of Alba). The term Scotia would increasingly be used to describe the heartland of these kings, north of the River Forth, and eventually the entire area controlled by its kings would be referred to as Scotland. The long reign (900–942/3) of Donald's successor Causantín (Constantine II) is often regarded as the key to formation of the Kingdom of Alba/Scotland, and he was later credited with bringing Scottish Christianity into conformity with the Catholic Church.
Máel Coluim I (Malcolm I) (r. c. 943–954) annexed Strathclyde, over which the kings of Alba had probably exercised some authority since the later 9th century. The reign of David I has been characterised as a "Davidian Revolution", in which he introduced a system of feudal land tenure, established the first royal burghs in Scotland and the first recorded Scottish coinage, and continued a process of religious and legal reforms. Until the 13th century, the border with England was very fluid, with Northumbria being annexed to Scotland by David I, but lost under his grandson and successor Malcolm IV in 1157. The Treaty of York (1237) fixed the boundaries with England close to the modern border. By the reign of Alexander III, the Scots had annexed the remainder of the western seaboard after the stalemate of the Battle of Largs and the Treaty of Perth in 1266. The Isle of Man fell under English control in the 14th century, despite several attempts to restore Scottish authority. The English occupied most of Scotland under Edward I and annexed a large slice of the Lowlands under Edward III, but Scotland established its independence under figures including William Wallace in the late 13th century and Robert I and his successors in the 14th century in the Wars of Independence (1296–1357). This was helped by cooperation with the kings of France, under the terms of what became known as the Auld Alliance, which provided for mutual aid against the English. In the 15th and early 16th centuries, under the Stewart Dynasty, despite a turbulent political history, the Crown gained greater political control at the expense of independent lords and regained most of its lost territory to around the modern borders of the country. The dowry of the Orkney and Shetland Islands in 1468 was the last great land acquisition for the kingdom. In 1482, Berwick a border fortress and the largest port in medieval Scotland, fell to the English once again; this was the last time it changed hands. The Auld Alliance with France led to the heavy defeat of a Scottish army at the Battle of Flodden Field in 1513 and the death of the king James IV. A long period of political instability followed.
Consolidation and union: 1513–1707
, whose inheritance of the thrones of England and Ireland created a dynastic union in 1603
In the 16th century, under James V of Scotland and Mary, Queen of Scots, the Crown and court took on many of the attributes of the Renaissance and New Monarchy, despite long royal minorities, civil wars and interventions by the English and French. In the mid-16th century, Scottish Reformation was strongly influenced by Calvinism, leading to widespread iconoclasm and the introduction of a Presbyterian system of organisation and discipline that would have a major impact on Scottish life.
In the late 16th century, James VI emerged as a major intellectual figure with considerable authority over the kingdom. In 1603 he inherited the thrones of England and Ireland, creating a Union of the Crowns that left the three states with their separate identities and institutions. He also moved the centre of royal patronage and power to London.
When James' son Charles I attempted to impose elements of the English religious settlement on Scotland, the result was the Bishops' Wars (1637–40), which ended in defeat for the king and a virtually independent Presbyterian Covenanter state in Scotland. It also helped precipitate the Wars of the Three Kingdoms, during which the Scots carried out major military interventions.
After Charles I's defeat, the Scots backed the king in the Second English Civil War; after his execution, they proclaimed his son Charles II of England king, resulting in the Third English Civil War against the emerging republican regime of Parliamentarians in England led by Oliver Cromwell. The results were a series of defeats and the short-lived incorporation of Scotland into the Commonwealth of England, Scotland and Ireland (1653–60).
After the 1660 restoration of the monarchy, Scotland regained its separate status and institutions, while the centre of political power remained in London. After the Glorious Revolution of 1688–89, in which James VII was deposed by his daughter Mary and her husband William of Orange in England, Scotland accepted them under the Claim of Right Act 1689, but the deposed main hereditary line of the Stuarts became a focus for political discontent known as Jacobitism, leading to a series of invasions and rebellions mainly focused on the Scottish Highlands.
After severe economic dislocation in the 1690s, there were moves that led to political union with England as the Kingdom of Great Britain, which came into force on 1 May 1707. The English and Scottish parliaments were replaced by a combined Parliament of Great Britain, but it sat in Westminster and largely continued English traditions without interruption. Forty-five Scots were added to the 513 members of the House of Commons and 16 Scots to the 190 members of the House of Lords. It was also a full economic union, replacing the Scottish systems of currency, taxation and laws regulating trade.