創(chuàng)建databases
mysql>
CREATE DATABASE IF NOT EXISTS my_db default charset utf8 COLLATE utf8_general_ci;
#注意后面這句話 "COLLATE utf8_general_ci",大致意思是在排序時(shí)根據(jù)utf8變碼格式來排序
#那么在這個(gè)數(shù)據(jù)庫下創(chuàng)建的所有數(shù)據(jù)表的默認(rèn)字符集都會(huì)是utf8了
mysql>show databases(查找databases)
MySQL>use database_name(使用database)
創(chuàng)建 學(xué)生表
mysql>create table students(
? ? ? ? ? ? stu_id int auto_increment primary key环凿,設(shè)置主鍵
????????????name varchar(10) not null,默認(rèn)不能為空
????????????sex varchar(3) default '女',默認(rèn)為女岖妄,utf-8為三個(gè)字節(jié)
????????????address varchar(50),
????????????phone int not null unique, null:可為空,unique電話號(hào)碼唯一
? ? ? ? ? ? birth date,生日
????????);
mysql>create table subjects(
? ? ? ? sub_id int auto_increment primary key,設(shè)置主鍵
? ? ? ? subject varchar(10)not null unique
????????);
mysql>create table score(
? ? ? ? id int auto_increment primary key状飞,設(shè)置主鍵
????????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;
????????);
查詢表結(jié)構(gòu):desc tablename
student表插入數(shù)據(jù):
insert into student(name,phone,sex,birth) values('李白',1311111111,'1','1988.2.1');date 日期類型要用引號(hào)。
insert into student(name,phone,sex,birth) values('杜甫',1311111112,'1','1988.2.1');date 日期類型要用引號(hào)。
insert into student(name,phone,sex,birth) values('蘇軾',1311111113,'1','1988.2.1');date 日期類型要用引號(hào)诬辈。
subjects表插入數(shù)據(jù):
insert into subjects(subject) values('英語');
insert into subjects(subject) values('語文');
insert into subjects(subject) values('數(shù)學(xué)');
score表插入數(shù)據(jù):
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');