表的基本操作
-
使用数据库:
use leo_school; -
在仓库中显示表:
show tables; -
create table student( //回车
-> id int,
-> name varchar(30),
-> age int
-> ); //回车
-
show tables;
-> create table if not exists teacher(
-> id int auto_increment primary key comment '主键id',
-> name varchar(30) not null comment '老师的名字',
-> phone varchar(20) comment '电话号码',
-> address varchar(100) default '暂时未知' comment '家庭住址'
-> )engine=innodb;
id,name,age都叫字段 auto_increment:自动增长 primary key:主键,意味着每个学生的学号都是唯一的,最主要的,靠他来区分这张表,不能重复 not null:姓名可以重复,不设置成主键,不能为空 default:默认值,暂时未知即为默认值,可以不填,此时显示为暂时未知,默认值不设置时,一般为null
engine=innodb:数据库引擎
查看表结构 把表列出来:desc teacher;
会把表的结构列出来,比较美观
删除表几种方法:
1.drop table stu;
2.drop table if exists s;
//如果存在则删除
3.drop table if exists oooo,stu,jjj;
//删除多张表
修改student表:
alter table student add phone varchar(20);
//添加字段
alter table student add gender varchar(1) after neme;
//放在name后面
alter table student add gender varchar(1) first;
//放在所有表的最前端
alter table student drop address;
//删除表
alter table student change phone tel_phone int(11);
//将phone改为tel_phone,并设为int(11)
alter table student modify te_lphone varchar(13);
//只能改字段的类型,不改名字
alter table student rename to students;
//将student这张表改为students(不规范,复数不能成为表的名字)
查询表:
select * from teacher;
| id | name | phone | address |
|---|---|---|---|
| 1 | Leo | 12322343 | Shanghai |
| 2 | Jack | 2423423 | Beijing |
| 3 | Tom | 64655345353 |

浙公网安备 33010602011771号