列名2 数据类型 not null, 列名3 数据类型 unique, 列名4 数据类型 default '值', constraint 索引名 foreign key(外键列) references 主键表(主键列) on delete cascade | on delete set null ) //修改 1.主键约束 添加:alter table table_name add primary key (字段) 删除:alter table table_name drop...
add constraint pk_StuNo primary key(StudentNo) 1. 2. 3. --给身份证添加唯一约束-- 代码如下: alter table Student add constraint uq_StuIdcard unique(IDENTITYcard) 1. 2. ---给地址address添加默认约束-- 代码如下: alter table Student add constraint df_stuaddress default('地址不详') for Addre...
ALTER TABLE 表名 DROP CONSTRAINT [主键名] ALTER TABLE 表名 ADD CONSTRAINT [新主键名] PRIMARY KEY ([列名])ifexists (select*fromdbo.sysobjectswhereid = object_id(N'[dbo].[PK_orders]') and OBJECTPROPERTY(OBJECT_ID(N'dbo.[orders].[pi_no]'), N'IsPrimaryKey') =1) ALTER TABLE orders ...
`gradename` VARCHAR(50) NOT NULL COMMENT '年级名称', PRIMARY KEY(`gradeid`) )ENGINE=INNODB DEFAULT CHARSET=utf8 -- 创建student表 CREATE TABLE IF NOT EXISTS `student`( `id` INT(4) NOT NULL AUTO_INCREMENT COMMENT '学号', `name` VARCHAR(20) NOT NULL DEFAULT '匿名' COMMENT '姓名...
* create database 数据库名称; * 创建数据库,判断不存在,再创建: * create database if not exists 数据库名称; * 创建数据库,并指定字符集 * create database 数据库名称 character set 字符集名; * 练习: 创建db4数据库,判断是否存在,并制定字符集为gbk * create database if not exists db4 characte...
ALTER TABLE 表名ADD CONSTRAINT 外键名称 FOREIGN KEY (外键字段名) REFERENCES 主表(主表字段名) ON UPDATE CASCADE ON DELETE CASCADE; 演示如下: 代码语言:sql AI代码解释 alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id) on update cascade on delete cascade ...
ALTER TABLE 测试表 ADD CONSTRAINT 默认约束1 DEFAULT ('默认设置值') FOR 字段名 --判断是否存在主键约束,primary key简称PK是主键约束的type--- IF EXISTS(SELECT * FROM sysobjects WHERE name='约束条件名称' and xtype='PK') begin print '存...
whereagein(13,15);-- 或者用where age>=13 and age<=15,注意between包含边界;或者用where age>=13 && age<=15select*fromtablewhereagebetween13and15;-- not表示不在13到15范围的所有select*fromtablewhereagebetween13and15;-- 查询为null的select*fromtablewhereageisnull;--where age is not null;不...
SQL> CREATE TABLE tb_constraint_12 (3 empno NUMBER PRIMARY KEY, --主键约束4 ename VARCHAR2(20) NOT NULL, --非空约束5 email VARCHAR2(60) UNIQUE, --唯一约束6 sal NUMBER(5) CHECK(sal>1500), --核查约束7 deptno NUMBER(4) REFERENCES tb_dept(deptno) --外键约束...
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id); 2). 删除外键 ALTER TABLE 表名 DROP FOREIGN KEY 外键名称;例:删除emp表的外键fk_emp_dept_id alter table emp drop foreign key fk_emp_dept_id; 删除/更新行为 添加了外键之后,再删除父表数据时产生的...