sql基本语句

r囧r小猫 2022-07-16 10:43 307阅读 0赞

1,创建表及约束的标识
create table qj
(
#在列中标识约束
id varchar(10) primary key auto_increment,
name varchar(20) not null,
salary int(10000),
sex varchar(1) default ‘m’ ,
#在表中表示约束,与列中表示主键约束效果一样,但是联合主键则只能在表中标识了
constraint pri_id primary key (id),
constraint pri_mul primary key(id,name),
#还可以标识其他约束如,非空约束
constraint noNull not null (salary)
)
由上可以看出约束的标识的语法为
constraint 约束名 约束类别如primary key ( 表中字段),如下:
(1)唯一性约束
constraint pos_unique unique (postalCode)
(2)非空约束
constraint pos_unique not null (postalCode)
(3)外键约束
constraint fre_off foreign key (officeCode ) references office(officeCode)
(4)默认值约束与自动增加约束都是直接在字段上添加,并且自动增加约束只能用于具有唯一约束的字段,包括primary.默认约束指插入数据时如果该字段没有表示值,则将该值设置为默认值
2,查看数据表结构
desccribe或者desc 表名,比如
desc office;
describe office;其中office是一个表名。

3,修改数据表结构
a,对表的操作
删除表:drop table office;
表重命名:alter table office rename office_info;
修改引擎:alter table employees engine =MyISAM;
b,对表的轻度修改 modify
修改表中字段的类型 : alter table employees modify sex char(1) not null;字段名不变,修改字段类型。
修改表中字段的排序:
alter table office modify mobile varchar(25) after officeCode ;
c, 改变表 change
将表中的某个字段改为另外一个字段
alter table office change birth employee_birth datetime;
d, 增加表 add
增加某个字段: alter table office add colume1 varchar(10) not null;
在某个字段后面添加字段:alter table office add colume1 varchar(10) after sex;
e,删除 delete
删除字段:alter table office drop colume1 ;
删除外表连接:alter table office drop foreign key foreign_key_name;
此外,如果A表依赖B表,则称为B为主表,A为从表,如果要删除A或者B都必须先接触外表连接,也就是必须先删除外表连接。

发表评论

表情:
评论列表 (有 0 条评论,307人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SQL 查询语句基本概念

    数据库中存储的是一张张表;表是行和列的形式; 查询语句的基本形式是:select  字段名  from 表名; 字段名如果是 \,则代表全部字段; 如下图,查询membe