create database school default charset=utf8; -- 创建数据库
create database if not exists xuexiao default charset=utf8;
use school;-- 使用数据库
create table studentInfo(
name varchar(20),
sex char,
age int,
address varchar(50)
);
show tables; -- 显示表格
-- 插入数据
-- 1字段和数据一一对应 2 字段如果和创建时一致,那么可以省略
insert into studentInfo (name ,sex ,age ,address) values ("zhangsan",'n',18,"henan");
insert into studentInfo values ("zhangsanfeng",'n',58,"henansheng");
insert into studentInfo (sex ,address,age ,name ) values ("女","河南省峨眉",28,"灭绝师太");
select *from studentInfo;-- 查询所有数据
create table studentInfo2(
name varchar(20),
sex char not null default'男',
age int not null default 18,
address varchar(50)
);
-- 可以根据需要省略有默认值的字段
insert into studentInfo2 (name , address) values ("周芷若","峨眉")
insert into studentInfo2 (name , sex , age , address) values ("周芷若",'女',20,"峨眉")
insert into studentInfo2 values ("周芷若",'女',20,"峨眉")
-- 一次插入多行数据
insert into studentInfo2 values ("周芷若",'女',20,"峨眉"), ("赵梅",'女',20,"蒙古"), ("赵梅",'女',20,"蒙古"), ("赵梅",'女',20,"蒙古");
select *from studentInfo2;-- 查询所有数据
create table studentInfo3(
id int primary key auto_increment,
name varchar(20),
sex char not null default'男',
age int not null default 18,
address varchar(50)
);
insert into studentInfo3 (id ,name,sex ,age,address)values(1,'铁牛','男',20,"梁山");
insert into studentInfo3 (name,sex ,age,address)values('铁牛','男',20,"梁山"); -- 自动增长的字段,插入数据时可以不写
select *from studentInfo3;