如何用mysql建立一个数据库,在库中建表,分别给表赋值,修改数据类型等区分主码,外码
-- 创建数据库
create database gradem;
-- 使用(进入)数据库
use gradem;
-- 创建表
create table student(
sno char(10) not null primary key,
sname varchar(8),
ssex char(2),
sbirthday datetime,
saddress varchar(50),
sdept char(16),
speciality varchar(20)
);
create table course(
cno char(5) not null primary key,
cname varchar(20) not null
);
create table sc(
sno char(10) not null,
cno char(5) not null,
degree decimal(4,1),
primary key (sno,cno)
);
-- sc表外码
alter table sc add constraint fk_sno foreign key (sno) references student(sno);
alter table sc add constraint fk_cno foreign key (cno) references course(cno);
create table teacher(
tno char(3) not null primary key,
tname varchar(8),
tsex char(2),
tbirthday datetime,
tdept char(16)
);
create table teaching(
cno char(5) not null,
tno char(3) not null,
cterm tinyint(1)
);
-- 向表中插入数据,values中的值,与表结构中的一一对应
insert into student values('20050101','李勇','男','1987-01-12','山东济南','计算机工程系','计算机应用');
insert into student values('20050201','刘晨','女','1988-06-04','山东青岛','信息工程系','电子商务');
insert into student values('20050301','王敏','女','1989-12-23','江苏苏州','数学系','数学');
insert into student values('20050202','张立','男','1988-08-25','河北唐山','信息工程系','电子商务');
insert into course values('C01','信息系统');
insert into course values('C02','操作系统');
insert into course values('C03','信息系统');
insert into course values('C04','操作系统');
insert into sc values('20050101','C01',92);
insert into sc values('20050101','C02',85);
insert into sc values('20050101','C03',88);
insert into sc values('20050201','C02',90);
insert into sc values('20050201','C03',80);
insert into teacher value('101','李新','男','1977-01-12','计算机工程系');
insert into teacher value('102','钱军','女','1968-06-04','计算机工程系');
insert into teacher value('201','王小花','女','1979-12-23','信息工程');
insert into teacher value('202','张小青','男','1968--08-25','信息工程');
insert into teaching values('C01','101',2);
insert into teaching values('C02','102',1);
insert into teaching values('C03','201',3);
insert into teaching values('C04','202',4);
-- 向student表中增加入学时间
alter table student add enroll_date datetime;
-- 将student中的sdept长度改为20
alter table student modify sdept char(20);
-- 将student中的speciality字段删除
alter table student drop column speciality;
-- 利用drop table删除student,因为sc有外码约束涉及该表,需要先删除外码约束
alter table sc drop foreign key fk_sno;
drop table student;
不知道你这个问题是否已经解决, 如果还没有解决的话:直接po截图和代码
大家也可以参考
这篇帖子建立mysql数据库的步骤:
安装mysql。可以通过yum、apt-get等包管理器安装,也可以从官方网站下载对应版本的安装包进行安装。
启动mysql服务,可以使用systemctl start mysql等命令进行操作。
进入mysql命令行界面,可以通过mysql -u root -p等命令,输入密码后进入。
创建数据库,使用create database databasename;命令,databasename为自定义的数据库名称。
选择数据库,使用use databasename;命令进行选择。
创建表,使用create table tablename (column1 datatype, column2 datatype, ...);命令,tablename为自定义的表名称,column为表的列名,datatype为数据类型。
给表赋值,使用insert into tablename (column1, column2, ...) values (value1, value2, ...);命令,value为对应列的值。
区分主键、外键,可以在创建表的时候,使用primary key和foreign key语句进行区分。如:
create table student ( id int primary key, name varchar(20), age int, class_id int, foreign key (class_id) references class(id) );
其中,id为主键列,class_id为外键列,class为关联的表。
alter table student modify name char(20);
这条命令将student表的name列的数据类型从varchar修改为char,且长度为20。
还可以使用alter table tablename add column datatype;命令添加列,使用alter table tablename drop column columnname;命令删除列等操作。
总结:
以上是建立mysql数据库的基本步骤,包括创建数据库、选择数据库、创建表、表赋值、区分主键、外键以及修改表的数据类型或属性等。重要的是要结合实际情况进行操作,并注意数据类型的选择、表的设计以及索引的使用等问题。
创建表可以参考下这个 http://t.csdn.cn/8yFJC
MYSQL基础之创建数据库表,以及修改表
可以借鉴下
https://blog.csdn.net/u011863822/article/details/122401347