如何在没有约束值的情况下警告表的默认值

时间:2018-01-09 03:44:26

标签: sql sql-server

我需要警告表没有约束的列的默认值。

示例:

  **ALTER Table [dbo].[Settings] ALTER column [Explore] bit set Default ((0))**

我用上面的查询来修改默认值为false的列,但我运行查询时显示错误"关键字附近的语法不正确' set'"或"关键字'默认'。"

附近的语法不正确

2 个答案:

答案 0 :(得分:0)

要将默认约束添加到现有列,您需要使用此语法

ALTER TABLE Settings ADD CONSTRAINT DF_name DEFAULT (0) FOR Explore;

答案 1 :(得分:0)

此处是无约束地实现您的要求的方式

如果列

中没有任何数据,请执行以下操作
public class Recycler extends Fragment {

public Recycler() {

}


List<Person> persons;
private String pName;
private String pAge;

RecyclerView rv;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootview = inflater.inflate(R.layout.recycler, container, false);
    super.onCreate(savedInstanceState);

    rv = (RecyclerView)rootview.findViewById(R.id.rv);
    rv.setHasFixedSize(true);
    rv.setLayoutManager(new LinearLayoutManager(getContext()));

    persons = new ArrayList<>();

    persons.add(
            new Person("Vijay", "22", "Male", "RegNo:666", "Doctor:vishnu", "Opno:64446", R.drawable.ic_account_circle_black_18dp));

    persons.add(
            new Person("Vishnu", "23","Male", "RegNo:666", "Doctor:vishnu", "Opno:6666", R.drawable.ic_account_circle_black_18dp));

    persons.add(
            new Person("Vaishnavi sulochana", "24","Female", "RegNo:666", "Doctor:vishnu", "Opno:6666", R.drawable.ic_account_circle_black_18dp));

    persons.add(
            new Person("Ram", "25","Male", "RegNo:666", "Doctor:vishnu", "Opno:6666",R.drawable.ic_account_circle_black_18dp));

    persons.add(
            new Person("Raja", "21","Male","RegNo:666", "Doctor:vishnu", "Opno:6666", R.drawable.ic_account_circle_black_18dp));

    persons.add(
            new Person("Hari", "23", "Male","RegNo:666", "Doctor:vishnu", "Opno:6666",R.drawable.ic_account_circle_black_18dp));

    persons.add(
            new Person("Hari", "23", "Male","RegNo:666", "Doctor:vishnu", "Opno:6666",R.drawable.ic_account_circle_black_18dp));

    //creating recyclerview adapter
    //RVAdapter adapter = new RVAdapter(getContext(), persons);
    RVAdapter adapter = new RVAdapter(getContext(),persons,pName,pAge);
    adapter.setOnItemClickListener(new RVAdapter.OnItemClickListener(){
        public void onItemClick(String textName, String textViewBrief){
            ServiceTable eventFragment = new ServiceTable();
            //replace content frame with your own view.
            FragmentTransaction ft = getFragmentManager().beginTransaction();    ft.replace(R.id.main_container, eventFragment).commit();
        }
    });

    //RVAdapter myAdapter = new RVAdapter(persons, this);

    //setting adapter to recyclerview
    rv.setAdapter(adapter);
    return rootview;
}

}

如果列中有数据,并且您想保留它,那么

1-创建列ALTER TABLE [dbo].[Settings] DROP COLUMN Explore ALTER TABLE [dbo].[Settings] ADD Explore BIT DEFAULT 0

Explore_copy

2-将数据从ALTER TABLE [dbo].[Settings] ADD Explore_copy BIT DEFAULT 0 复制到Explore

Explore_copy

3-删除旧列UPDATE [dbo].[Settings] SET explore_copy = Explore

Explore

4-现在,将ALTER TABLE [dbo].[Settings] DROP COLUMN Explore 重命名为原始列名Explore_copy

Explore