MySQL数据库与表的设计

create database  xuexiao;
use xuexiao;
create  table   studentInfo(
`name` varchar(10) primary key,    --  primary key 主键
sex char ,
age int
);


show tables;

create  table   studentInfo1(
    id  int auto_increment  primary key ,   -- 自增  auto_increment
`name` varchar(10),
sex char ,
age int
);
 -- 外键
 create  table grade1(
 name varchar(10),
 grade int ,
 dengji char,
 
 constraint fk_gread1Name_studentInfoName foreign key   (`name`) references   studentInfo1  (`name`)   --  外键
 );
 
 desc grade1;
 
 create table student(
    name varchar (10)  not null   unique ,
    sex char default "男",
    province  varchar (10)  default '河南',
    age  int 
    
 
 );
 
 insert into student( `name` ,age) values ("张三" ,18);  -- 没写的的属性字段为默认值
 
  insert into student(`name` ,sex ,province,age) values ("李四",'女', '河北' ,20) ;   -- 默认值会被覆盖掉
 select*from student    
 
 
alter table student change  `name` `name`  varchar(10) not null;  -- 修改表结构

alter table student change  `name` `name`  varchar(10) not null unique;  --  唯一
desc  student;

 

你可能感兴趣的:(MySQL数据库与表的设计)