找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
删除主键:
alter table test drop primary key;
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key (直接删除会报错)
1,首先要先删除自增长约束:
alter table test modify sid int(20);
2,然后才可以删除主键:
alter table test drop primary key;
3,增加主键:
alter table test change sid sid int(20) primary key;
注意:删除主键约束时不能通过修改字段属性进行,只能直接使用drop;添加主键约束则可以直接通过修改字段属性的方法进行
删除表:
drop table +表名;   
通过绝对方式的方法删除其他库中表:
alter table ===》改变表的结构
通过绝对方式的方法删除其他库中表:
alter table 库名.表名  add  。。。。。(接绝对路径)
insert into   ===》插入数据
select * from   ==》查询数据
插入数据时,需要注意:
1varcharchar是字符串数据类型,所以插入数据的时候需要加 '' 引号;
2date 日期型也要加引号
3,非空约束中,类型为字符串的字段默认值为'  ',类型为数字的字段默认值是0,与其他没有非空约束的字段对比,其他字段的NULL是不占空间的,但是非空约束字段的值,默认是占一个字节0的空间
插入数据有三种方式
第一方式:插入数据时,完整的说明表中字段,值的位置要和说明字段的位置一一对应
insert into test(sid,name,sex,score,phone,time,class) values(1,'xiaomai','228888,'2021-06-11',2130);
第二种方式:插入数据时,单独对某一个或者某多个字段插入,值的位置和说明字段的位置一一对应
insert into  test (sid) values (2);
insert into test(class,sid) values(88,2130);
第三种方式:插入数据时,不说明表中字段情况,默认的往所有的字段插入数据,而且字段的位置也是和表结构顺序一致,所以这种方式要求对表的结构非常熟悉
insert into test values(100,'damai','boy',90.12345,18999990000,'20210718',2130);
删除表数据:
delete
delete from test1 where id=3;
delete from test1 where phone=17688889999 and id=2;
delete from test1;
===》删除表test1中数据,但是不释放空间,保留表结构。自增长还是按照原来最大的值继续加一
truncate 表名
trunkcate test1===》删除表中数据,且释放空间,保留表结构。自增长会从头开始进行
更新(修改)表数据:
update
update test1 set phone=18899998888;
update test1 set phone=14466667777 where id=1;
删除表数据有三种方式:
第一种:delete from 表名;===》删除表中数据,但是不释放空间,保留表结构。自增长还是按照原来最大的值继续加一
第二种:truncate 表名; ===》删除表中数据,且释放空间,保留表结构。自增长会从头开始进行
第三种:drop table +表名;===》完全删除一张表,释放空间,且清除表结构
查询表数据:
select
select *from test1;
select * from test1 where id=1;
select name from test1;
select name from test1 where id=6;
注意:
select name from test1 where phone=17688889999 and time='20210619';
select name,id,phone from test1 where id=1 or time='20210610';
select name,id,phone from test1 where id=1 or id=6 or id=10;
select id,name from test1 where id<>1;
select id,name from test1 where id != 1;
select * from test1 where score is null;
select *from test1 where score is not NULL;
select * from test1 where name = 'NULL';
select * from test1 where score >80;
select * from test1 where score <80;
select *from test1 where score <=78.65;
select *from test1 where score >=78.65;
注意:
select * from test1 where score between 78.65 and 90;
select *from test1 where id in (1,6,10);
select * from test1 where id not in (1,6,10);
select *from test1 where phone =13188887777 or phone=17688889999 and time='20210619';
*在生活中,排序都是从1开始的,但是在计算机程序里是从0开始
select *from test1 order by score;
select * from test1 order by score asc;
select * from test1 order by score desc;
select *from test1 where score is not null order by score desc limit 0,2
select *from test1 where score is not null order by score desc;
select *from test1 group by time;  
group by 分组之后,其他匹配出来的字段值,是随机匹配出来的,一般是表中匹配到的第一条
select *from test1 where score is not null group by time;
mysql中允许使用where条件过滤数据之后,再进行group by分组
select *from test1 group by time where score is not null;
不允许先使用group by分组再使用where条件过滤,否则会执行报错
select *from test1 group by time having score is not null;

分享至 : QQ空间
收藏

1 个回复

倒序浏览
删除自增长是用change把,modify 是调整字段位置
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册