using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form6 : Form
    {
        public Form6()
        {
            InitializeComponent();
        }

        private void Form6_Load(object sender, EventArgs e)
        {
            //建立第一张表student
            DataTable studt = new DataTable("student");
            DataColumn sno = new DataColumn();
            sno = studt.Columns.Add("sno", typeof(int));
            sno.AllowDBNull = false;
            sno.Unique = true;

            studt.PrimaryKey = new DataColumn[] { studt.Columns["sno"]};

            DataColumn sname = new DataColumn();
            sname = studt.Columns.Add("sname", typeof(string));
            sname.AllowDBNull = false;
            sname.Unique = false;

            DataColumn ssex = new DataColumn();
            ssex = studt.Columns.Add("ssex", typeof(string));
            ssex.AllowDBNull = false;
            ssex.Unique = false;
            ssex.DefaultValue = "男";

            DataColumn sbirthday = new DataColumn();
            sbirthday = studt.Columns.Add("sbirthday", typeof(string));
            sbirthday.DefaultValue = DateTime.Now.ToLongDateString();

            studt.Columns.Add("sclass", typeof(string));

            DataRow dr = studt.NewRow();
            dr["sno"] = 1001;
            dr["sname"] = "张三";
            dr["ssex"] = "男";
            dr["sbirthday"] = "1980-2-1";
            dr["sclass"] = "98002";
            studt.Rows.Add(dr);

            //建立第二张表student
            DataTable coursedt = new DataTable("course");
            DataColumn cno = new DataColumn();
            cno = coursedt.Columns.Add("cno", typeof(int));
            cno.AllowDBNull = false;
            cno.Unique = true;
            
            coursedt.PrimaryKey = new DataColumn[] { coursedt.Columns["cno"] };
            
            DataColumn cname = new DataColumn();
            cname = coursedt.Columns.Add("cname", typeof(string));

            DataColumn grade = new DataColumn();
            grade = coursedt.Columns.Add("grade", typeof(int));

            DataRow dr1 = coursedt.NewRow();
            dr1["cno"] = 1;
            dr1["cname"] = "数学";
            dr1["grade"] = 1;
            coursedt.Rows.Add(dr1);

            //建立第三张表sc
            DataTable scdt = new DataTable("sc");
            DataColumn cno1 = new DataColumn();
            cno1 = scdt.Columns.Add("cno1", typeof(int));
            cno1.AllowDBNull = true;
            cno1.Unique = true;

            DataColumn sno1 = new DataColumn();
            sno1 = scdt.Columns.Add("sno1", typeof(int));
            sno1.AllowDBNull = true;
            sno1.Unique = true;

            scdt.PrimaryKey = new DataColumn[] { scdt.Columns["cno1"], scdt.Columns["sno1"] };

            DataColumn degree = new DataColumn();
            degree = scdt.Columns.Add("degree", typeof(int));
            degree.AllowDBNull = false;
            degree.Unique = false;
            degree.DefaultValue = 0;

            DataRow dr2 = scdt.NewRow();
            dr2["sno1"] = 1001;
            dr2["cno1"] = 1;
            dr2["degree"] = 92;
            scdt.Rows.Add(dr2);

            //如何建立约束
            /*ForeignKeyConstraint custOrderFK = new ForeignKeyConstraint("CustOrderFK",
custDS.Tables["CustTable"].Columns["CustomerID"],
custDS.Tables["OrdersTable"].Columns["CustomerID"]);
            custOrderFK.DeleteRule = Rule.None;
            custDS.Tables["OrdersTable"].Constraints.Add(custOrderFK);*/

            ForeignKeyConstraint scFK = new ForeignKeyConstraint("scorderfK", studt.Columns["sno"], scdt.Columns["sno1"]);
            scdt.Constraints.Add(scFK);
        }
    }
}