--Use the test database!
use test
--Cleanup!
drop table orders
drop partition scheme ps_orders
drop partition function pf_orders

--Partition Function
CREATE PARTITION FUNCTION pf_orders (int)
AS RANGE LEFT FOR VALUES(0,10000,20000,30000,40000,50000,60000)

--Partition Scheme
/* Multiple file groups (not used)
CREATE PARTITION SCHEME ps_orders
AS PARTITION pf_orders
TO ([FG1],[FG2],[FG3],[FG4],[FG5],[FG6],[FG7],[FG8],[PRIMARY])
*/

CREATE PARTITION SCHEME ps_orders
AS PARTITION pf_orders
ALL TO ([PRIMARY])

--Partitioned table
CREATE TABLE orders
(order_id int not null,
 order_date datetime not null,
 order_amount money default 0,
 order_details char(7000),
 CONSTRAINT pk_orderid PRIMARY KEY CLUSTERED (order_id)) --PK on order_id
 ON ps_orders(order_id) --Replaces ON <<FILEGROUP>>

--Look at partition functions
select
	*
from
	sys.partition_functions

--Look at partition schemes
select
	*
from
	sys.partition_schemes

--Look at partitions
select * from sys.partitions
where
	OBJECT_NAME(object_id)  = 'orders'


--Let's put some data in!
exec usp_loadpartitiondata

--How do our partitions look now?
select 
	OBJECT_NAME(object_id) object_name,
	index_id,
	partition_number,
	rows
from
	sys.partitions
where
	OBJECT_NAME(object_id)  = 'orders'
order by
	index_id,
	partition_id
