mysql表操作

说明:student是要创建的表名

主键:primary key

注释:comment

1,创建表:

create table student(

sno varchar(20) not null primary key comment '学号',

sname varchar(20) not null comment '学生姓名',

ssex varchar(20) not null comment '性别',

sbirthday datatime comment '出生年月‘

)charset=utf8 collate=utf8_general_ci;

2,添加表信息:

a,

   insert into student values

   (108,'曾华','男','1977-09-01',95033),
   (105,'匡明','男','1975-10-02',95031),
   (107,'王丽','女','1976-01-23',95033),
   (101,'李军','男','1976-02-20',95033),
   (109,'王芳','女','1975-02-10',95031),
   (103,'陆君','男','1974-06-03', 95031);

b, 当添加信息时,如果表中存在以相同主键的记录,我们就更新这条记录,否则就加入一条新纪录

如果原表中存在以相同主键的记录,那么就执行duplicate key update 后面的信息

原表:

insert into  student (sno,sname,ssex,sbirthday,class) values

(108,'曾华','男','1978-03-11','95021'),

(110,'丁蛋','男','1992-02-12','95033')

on duplicate key update sno=values(sno),sname=values(sname),ssex=values(ssex),sbirthday='1978-03-11',class='95021';

执行后的表:

3,删除整个表

drop table student;

4,查看表所有内容

select * from student;

5,查看表某个字段内容 son或sname或ssex或sbirthday

select sno from student;

6,查看多个字段内容

select sno,sname from student;

7,修改表里某个字段的内容

update student set sno='200' where sno='101';

8,添加字段

alter table 表名 add 新字段名 字段类型【字段属性列表】

alter table student add sss varchar(20) not null;

9,修改字段(并改字段名)

alter table 表名 change 旧字段名 新字段名 新字段类型 【新字段属性列表】

alter table student change sss www char(10) not null;

10,修改字段(只改属性)

alter table 表名 modify 字段名 新字段类型 【新字段属性列表】

alter table student modify www int not null;

11,删除字段

alter table 表名 drop 字段名;

alter table student drop www;

12,删除主键

alter table 表名 drop primary key;

alter table student drop primary key;

13,修改表名

alter table 表名 rename 新表名;

alter table student rename teacher;

 14,显示所有表

show tables;

15,显示表结构

desc  表名;

desc student;

原文地址:https://www.cnblogs.com/wfc139/p/8901682.html