主要想总结一下今天用到的sql命令
创建表
create table 表名 (字段名1 字段类型1,字段名2 字段类型2...) 注意:这里可以指定主键名,要在主键字段类型后面加 primary key,比如如果要添加字段名1为主键则sql语句为:create table 表名(字段1 字段类型1, primary key,字段名2 字段类型2...); 如果是联合主键则在所有字段添加的后面添加 constraints XXX primary key(主键字段名1,主键字段名2...),如果要将字段名1和字段名2设置为联合主键则sql语句应该为:create table 表名 (字段名1 类型名,字段名2,字段名2....,constraints 联合主键名 primar key(字段名1,字段名2))。
改变表结构
增加字段名sql语句:alter table 表名 add 字段名 字段类型。删除字段名sql语句:alter table 表名 drop column 字段名。 添加主键sql语句: alter table 表名 add primary key (字段名)。删除主键sql语句 : alter table 表名 drop primary key。添加联合主键sql语句 :alter table 表名 add constraints 联合主键名 primary key(主键字段名1,主键字段名2..)。删除联合主键名sql语句:alter table 表名 drop constraints 联合主键名。添加主外键sql语句:alter table 表名 add constraint 约束名 foreign key (字段名) references 表名(字段名)。删除外键sql语句: alter table 表名 drop constraint 约束名。如果没有给出约束名,则需要先用select语句去查询约束名,具体sql语句见下部分。修改字段类型sql语句 :alter table 表名 dodify 字段名 字段类型。 修改字段名sql语句:alter table 表名 rename column 原字段名to新字段名。
查询表结构
首先是查询表的主键sql语句:select a.* from all_cons_columns a,all_constraints b where a.table_name = ‘大写表名’ and a.table_name=b.table_name and a.constarint_name = b.constaint_name and b.constraint_type = 'P'. 要注意的是表名的P都是要大写的。
查询主外键sql语句:select a.constraint_name ,a.table_name,b.constraint_name from user_constraints a,user_constraints b where a.constraint_type ='R' and b.constraint_type = 'P' and a.r_constraint_name = b.constraint_name;
查询一个表的所有约束名sql语句: select table_name, constraint_name,constraint_type from user_constraints where table_name = '大写表名'。
现在对表的约束还不是很了解,只是大概明白数据库是将表的说有约束条件填入到了一个数据库数据字典的表中,我们可以去查询获取需要的内容,只有在对数据字典足够了解了才能灵活的去运用。
数据处理
插入数据sql语句: insert into 表名 (字段1,字段2...)values (值1,值2...) 。 这里字段名可以全部省略,则值是按照顺序添加到表中的。
修改数据的sql语句:update table 表名 set 字段名 = 值。 这里可以用子查询语句选出需要更改的行。
删除数据sql语句: delete from 表名 where 子查询语句。
总结:今天学习的难点主要在于对表结构的修改,还有对于表的约束的查询还不是很懂。剩下的sql难点是select查询语句,这个还需要学习。