mysql数据库练习第一章

1.启动数据库

以管理员身份运行CMD

mysql数据库练习第一章

输入  C:\WINDOWS\system32>net start mysql

启动mysql

mysql数据库练习第一章

2.输入用户名密码

C:\WINDOWS\system32>mysql -u root -p

mysql数据库练习第一章

3.创建库,并创建学生详细信息表

需求:

创建库并创建学生详细信息表student:

student表

id

字符串
name 字符串,非空
age 数值型,非空
sex 约束:男 女
birth 出生日期:year型,非空
score 数值型,非空

 

3.1创建student库

mysql> create database student;

mysql数据库练习第一章

查看当前所有数据库

mysql> show databases;

mysql数据库练习第一章

进入student库

mysql> use student;

mysql数据库练习第一章

3.2创建student表

可以这样创建:

mysql> create table student(id char,name char not null,age int not null,sex enum('男','女') ,birth year not null,score int not null) character set=utf8; 

mysql数据库练习第一章

 也可以这样创建:

create table student

(

id char,

name char not null,

age int not null,

sex enum('男','女') ,

birth year not null,

score int not null

) character set=utf8;

mysql数据库练习第一章

3.3修改表student名为teacher

 mysql> alter table student rename teacher;

mysql数据库练习第一章

3.4 修改列名id为sid

mysql> alter table teacher change id sid char;

mysql数据库练习第一章

3.5 查看当前表结构

mysql> desc teacher;

mysql数据库练习第一章

3.6 修改sid属性为数值型,唯一

mysql> alter table teacher modify sid int unique;

mysql数据库练习第一章

3.7 删除name列,再添加name列属性不变

 

alter table teacher drop name;

alter table teacher add name char;