mysql恶补2_表级约束与列级约束

本文介绍了SQL中列级约束与表级约束的区别,并详细列举了多种SQL命令用于修改表结构,包括添加、删除列,修改约束等。此外还提供了查询数据库表名及字段名的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对一个数据列建立的约束,称为列级约束。

对多个数据列建立的约束,称为表级约束.

列级约束既可以在列定义时声明,也可以在列定义后声明.

表级约束只能在列定以后声明.


sql命令补充:

alter table `biao` add lieming varchar(20) after liedehoumian;   //修改列

alter table `biao` drop lieming,drop lieming        //删除列

alter table `biao` add constraint yueshuming_id primary key(id);   //修改约束

alter talbe `biao` add unique(lieming);

show create table `biao`; //显示创建表的sql语句

show indexes from biao\G;  //显示表的索引

alter table biao2 add foregin key(pid) references biao1(id);//添加外键
alter talbe biao alter age set default 15; //修改列的信息

alter table 表名 character set utf8 collate utf8_unicode_ci; //修改表的字符集

alter database 数据库名 character set utf8 collate utf8_unicode_ci;  //修改表的字符集

alter table 表名 auto_increment = 1;//把表的自增长id清零

alter table table_name rename table_new_name; //改表的名字
show columns from 表名;//显示表的结构
ALTER TABLE `b` CHANGE `sex` `sex` ENUM( '男', '女' ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL;
//修改字段

查询数据库中所有表名
select table_name from information_schema.tables where table_schema='csdb' and table_type='base table';


查询指定数据库中指定表的所有字段名column_name
select column_name from information_schema.columns where table_schema='csdb' and table_name='users'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值