mysql数据库表操作

来源:互联网 发布:nginx cache 编辑:程序博客网 时间:2024/05/17 21:43

1、文本文件导入数据库表格式:mysql> load data local infile 'F:\2.txt' into table 表名 FIELDS TERMINATED By ',' ENCLOSED BY "'" LINES TERMINATED By '\r\n';以逗号分割字段,以单引号标明字符,以'\r\n'表示记录行例:mysql> load data local infile 'F:\2.txt' into table tt FIELDS TERMINATED By ',' ENCLOSED BY "'" LINES TERMINATED By '\r\n';可以创建一个文本文件“2.txt”内容为:1,'fnoewi','fneowif'2,'ewfjpew','ewfewf'3,'efjio',\N对于空值,为了在你的文本文件中表示空内容,使用\N表示(反斜线,字母N,要大写N)。2、数据插入表也可以用insert命令INSERT INTO pet VALUES ('Puffball','Diane','hamster','f','1999-03-30',NULL);3、修改表名alter table t1 rename t2; 4、显示指定数据库下所有表show tables; 显示表5、显示表结构describe tablename;show create table 查询建表时的语句 6、select 中加上distinct去除重复字段7、删除表、删除表中所有数据drop table tablename;delete from tablename;8、创建表create table s_position      (          id int not null auto_increment,          name varchar(20) not null default '经理', #设定默认值          description varchar(100),          department_id int not null,          position_id int not null,          primary key PK_positon (id,name),   #设定复合主键          unique (department_id,position_id)   #设定唯一值      );    create table staffer      (          id int not null auto_increment primary key,   #设定主键          name varchar(20) not null default '无名氏',   #设定默认值          department_id int not null,          position_id int not null,          unique (department_id,position_id)   #设定唯一值      );9、修改结构    mysql>    #表position增加列test      alter table 表名称 add(字段名称 char(10));    #表position修改列test默认值      alter table 表名称 alter 字段名称 set default 'system';    #表position去掉test默认值      alter table 表名称 alter test drop default;    #表depart_pos删除主键      alter table 表名称 drop primary key;    #表depart_pos增加主键      alter table 表名称 add primary key PK_depart_pos (department_id,position_id);    修改数据类型      alter table 表名 modify 属性名 新数据类型;    修改字段排列位置      alter table 表名 modify 属性名1 数据类型 first|after 属性名2;    修改字段名      alter table 表名 change 旧属性名 新属性名 新数据类型 [完整性约束条件];    新增字段      alter table 表名 add 属性名1 数据类型 [完整性约束条件] [first|after 属性名2];新增的字段默认    为表的最后一个字段,first表示排在字段最前面,after表示在该字段后面alter table 表名 add 属性名1 数据类型 约束 first|after 属性名,add 属性名2 数据类型 约束 first|after 属性名    删除字段      alter table 表名 drop 属性名1,drop 属性名2;    删除表的外键约束      alter table 表名 drop foreign key 外键别名;    删除表唯一约束      alter table 表名 drop index 唯一约束名;    增加唯一约束    alter table 表名 add unique key 约束名(字段);    更改表的存储引擎      alter table 表名 engine=存储引擎名;注意:修改数据类型前要desc查看原表设置的约束,然后修改时加上约束。外键必须是父表的主键。10、表复制1).复制表CREATE TABLE 新表 SELECT * FROM 旧表2).只复制表结构到新表CREATE TABLE 新表 SELECT * FROM 旧表 WHERE 1=2; 即:让WHERE条件不成立.方法二:(低版本的mysql不支持,mysql4.0.25 不支持,mysql5已经支持了)CREATE TABLE 新表 LIKE 旧表3).复制旧表的数据到新表(假设两个表结构一样)INSERT INTO 新表 SELECT * FROM 旧表4).复制旧表的数据到新表(假设两个表结构不一样)INSERT INTO 新表(字段1,字段2,…….)SELECT 字段1,字段2,…… FROM 旧表11、创建临时表(数据库中不显示该临时表,断开和数据库的连接后该临时表不存在)create temporary table temp_pet(name varchar(10));create temporary table if not exists pet(name varchar(10)); --如果不存在该临时表,则创建该临时表12、查询数据limit的使用1)、limit(选出10到20条)<第一个记录集的编号是0> select * from students order by id limit 9,10;2)、select * from table limit m,n;其中m是指记录开始的index,从0开始,表示第一条记录n是指从第m+1条开始,取n条。select * from tablename limit 2,4即取出第3条至第6条,4条记录13、查询  in在某个集合内 如(3,6,7) between..and...在某范围内14、having是where之后再进行筛选,是对查询结果进行筛选15、查询是否存在,外部字段放入内部  where not exists(select * from xx );16、any关键字表示满足其中任何一个条件。如:>any表示大于其中任何一个,=any表示等于其中任何一个。例:select * from computer_stu where score>=any (select score from scholarship);17、all关键字表示满足所有条件才执行外层查询语句。例:select * from computer_stu where score>=all (select score from scholarship);



0 0
原创粉丝点击