SQL笔记
1.向表中添加数据:insert into table_name(col1,col2,...) values (data1,data2,...);
2.通过其他数据表向表中添加数据:insert into table_name(col1,col2,...) select col1,col2,... from table_name2;
3.如果目标表不存在(即被导入数据的表不存在):create table table_name as select col1,col2,col3,... from table_source;
4.更新表:update table_name set col1=data1,col2=data2,... [where condition];
5.删除表:delete from table_name [where condition];
6.添加主键约束(唯一且不为空):alter table table_name add constraints constraint_name primary key (col_name);
create table table_name(col_name1 type,col_name2 type,primary key (col_name));
7.添加外键约束(保证数据完整性、一致性): create table table_name(col_name1 type,col_name2 type,primary key (col_name),constraint fk_name foreign key (col_name) references table_name(col_name) on delete cascade); on delete cascade 表示设置级联删除,当主键的字段被删除,外键所对应的字段也同时被删除。

浙公网安备 33010602011771号