创建databases
mysql>
CREATE DATABASE IF NOT EXISTS my_db default charset utf8 COLLATE utf8_general_ci;
#注意后面这句话 "COLLATE utf8_general_ci",大致意思是在排序时根据utf8变码格式来排序
#那么在这个数据库下创建的所有数据表的默认字符集都会是utf8了
mysql>show databases(查找databases)
MySQL>use database_name(使用database)
创建 学生表
mysql>create table students(
stu_id int auto_increment primary key,设置主键
name varchar(10) not null,默认不能为空
sex varchar(3) default '女',默认为女,utf-8为三个字节
address varchar(50),
phone int not null unique, null:可为空,unique电话号码唯一
birth date,生日
);
mysql>create table subjects(
sub_id int auto_increment primary key,设置主键
subject varchar(10)not null unique
);
mysql>create table score(
id int auto_increment primary key,设置主键
stu_id int,
sub_id int,
foreign key(stu_id) references students(stu_id),
foreign key(sub_id) references subjects(sub_id)
date DATE;
score float;
);
查询表结构:desc tablename
student表插入数据:
insert into student(name,phone,sex,birth) values('李白',1311111111,'1','1988.2.1');date 日期类型要用引号。
insert into student(name,phone,sex,birth) values('杜甫',1311111112,'1','1988.2.1');date 日期类型要用引号。
insert into student(name,phone,sex,birth) values('苏轼',1311111113,'1','1988.2.1');date 日期类型要用引号。
subjects表插入数据:
insert into subjects(subject) values('英语');
insert into subjects(subject) values('语文');
insert into subjects(subject) values('数学');
score表插入数据:
insert into score(stu_id,sub_id,score,date) values(1,1,75.2,'2017.3.2');
insert into score(stu_id,sub_id,score,date) values(1,2,75.2,'2017.3.2');
insert into score(stu_id,sub_id,score,date) values(2,3,75.2,'2017.3.2');
insert into score(stu_id,sub_id,score,date) values(3,1,75.2,'2017.3.2');