1.创建数据库
create database test2;
2.删除数据库
drop database test2;
3.创建表
create table class
(
code varchar(20) primary key,
name varchar(20) not null
);
create table ceshi
(
ids int auto_increment primary key,
uid varchar(20),
name varchar(20),
class varchar(20),
foreign key (class) references class(code)
);
**自增长 auto_increment
**主键 primary key
**外键 foreign key (列名) references 主表名(列名)
**非空 not null
4.删除表
drop table ceshi;
注意:
1.类型包含长度的在类型后面加括号,括号里面写长度
2.上一列写完加逗号
3.最后一列不要写逗号
4.在每一条SQL语句写完之后要加分号
5.如果有外键关系,先创建主表
创建表(例子):
create table class
(
code varchar(20) primary key,
name varchar(20)
);
create table student
(
code varchar(20) primary key,
name varchar(20),
sex bit,
age int,
class varchar(20),
foreign key (class) references class(code)
);
create table kecheng
(
code varchar(20) primary key,
name varchar(20)
);
create table teacher
(
code varchar(20) primary key,
name varchar(20)
);
create table chengji
(
ids int auto_increment primary key,
scode varchar(20),
kcode varchar(20),
degree float,
foreign key (scode) references student(code),
foreign key (kcode) references kecheng(code)
);
create table tkecheng
(
ids int auto_increment primary key,
tcode varchar(20),
kcode varchar(20),
foreign key (kcode) references kecheng(code),
foreign key (tcode) references teacher(code)
);