SQL表关系和索引

时间:2017-03-14 12:25:18

标签: sql sql-server relationship

我希望有2个表SalesOrderHeader和SalesOrderLine具有一对多的关系,并且还想构建索引。

SalesOrderHeader表:

 SalesOrderNumber 
------------------
|                |
| 1              |
|                |
|----------------|
|                |
|                |
| 2              |
|                |
|                |
------------------

SalesOrderLine表:

 SalesOrderNumber Line
-------------------------
|                | 1    |
| 1              |------|
|                | 2    |
|----------------|------|
|                | 1    |
|                |------|
| 2              | 2    |
|                |------|
|                | 3    |
-------------------------

请告知如何使用这些表结构方法构建关联关系和索引?

1 个答案:

答案 0 :(得分:0)

创建外键

CREATE NONCLUSTERED INDEX IX_SalesOrderNumberLine_SalesOrderNumberID
ON dbo.SalesOrderNumberLine(SalesOrderNumberID);   

然后在该列上创建非聚集索引

<script type="text/javascript">
        // Fire this function when page loads
        $(document).ready(function () {
            $.getJSON(
                    'https://openexchangerates.org/api/latest.json?app_id=2e946db1e3fc4888b61330e505a804e5',
                    function(data){
                            if(typeof fx !== "undefined" && fx.rates){
                                    fx.rates = data.rates;
                                    fx.base = data.base;
                var amount = $("#amt").val();
                                    var from   = $("#from").val();
                                    var to     = $("#to").val();
                                    $("#res").val( fx(amount).from(from).to(to));
                                    $("#result").show();
                            }
                    }
            );
        });

        // Fire this function when value is entered in the field
        $(document).keyup('#amt', function(){
        $.getJSON(
            'https://openexchangerates.org/api/latest.json?app_id=2e946db1e3fc4888b61330e505a804e5',
            function(data){
                if(typeof fx !== "undefined" && fx.rates){
                    fx.rates = data.rates;
                    fx.base = data.base;
                    var amount = $("#amt").val();
                    var from   = $("#from").val();
                    var to     = $("#to").val();
                    $("#res").val( fx(amount).from(from).to(to));
                    $("#result").show();
                }
            }
        );
    });

        // Fire this function on swap button click
        $("#swap").click(function(e) {
        e.preventDefault();
        var fromVal = $("#from option:selected").val();
        var fromText = $("#from option:selected").text();
        var toVal = $("#to option:selected").val();
        var toText = $("#to option:selected").text();

        $("#from option:selected").val(toVal);
        $("#from option:selected").text(toText);
        $("#to option:selected").val(fromVal);
        $("#to option:selected").text(fromText);

            $.getJSON(
                    'https://openexchangerates.org/api/latest.json?app_id=2e946db1e3fc4888b61330e505a804e5',
                    function(data){
                            if(typeof fx !== "undefined" && fx.rates){
                                    fx.rates = data.rates;
                                    fx.base = data.base;
                var amount = $("#amt").val();
                                    var from   = $("#from").val();
                                    var to     = $("#to").val();
                                    $("#res").val( fx(amount).from(from).to(to));
                                    $("#result").show();
                            }
                    }
            );
        });

        // Fire this function on change of "FROM" dropdown selection
        $("#from").change(function () {
            $.getJSON(
                    'https://openexchangerates.org/api/latest.json?app_id=2e946db1e3fc4888b61330e505a804e5',
                    function(data){
                            if(typeof fx !== "undefined" && fx.rates){
                                    fx.rates = data.rates;
                                    fx.base = data.base;
                var amount = $("#amt").val();
                                    var from   = $("#from").val();
                                    var to     = $("#to").val();
                                    $("#res").val( fx(amount).from(from).to(to));
                                    $("#result").show();
                            }
                    }
            );
        });

        // Fire this function on change of "TO" dropdown selection
        $("#to").change(function () {
            $.getJSON(
                    'https://openexchangerates.org/api/latest.json?app_id=2e946db1e3fc4888b61330e505a804e5',
                    function(data){
                            if(typeof fx !== "undefined" && fx.rates){
                                    fx.rates = data.rates;
                                    fx.base = data.base;
                var amount = $("#amt").val();
                                    var from   = $("#from").val();
                                    var to     = $("#to").val();
                                    $("#res").val( fx(amount).from(from).to(to));
                                    $("#result").show();
                            }
                    }
            );
        });
</script>