数据库:存放数据的仓库;
硬盘 运行内存
mysql 现在被oracle收购了
oracle oracle公司
sqlServer 微软的
一般都是关系型数据库管理软件
mysql(数据库管理软件)--数据库--表---行
sql语言:structured query language 结构化查询语言
我们学的是标准sql ,sql的方言被各个数据库软件单独使用;
1.登录mysql数据库:
方式1:mysql -uroot -proot
方式2:
C:\Users\Administrator>mysql -uroot -p
Enter password: ****
退出数据库:quit; 或者exit;
2.查看所有数据库:mysql> show databases;
3.选择要使用的数据库:mysql> use 数据库名;
4.查看所有表:mysql> show tables;
5.创建数据库:mysql> create database; 新数据库名;
6.删除数据库:mysql> drop database; 被删除的数据库名;
7.新建一个表:mysql> create table student(sid int,sname varchar(10));
8.查看表结构:mysql> desc student;
数据类型:
文本:
char(10):固定长度的字符串; char(10);开辟10个字符的内存空间;
varchar(n):可变长度的字符串,最多可以存n个字符,若实际存的不到n个那么就只开辟实际存储的个数的大小;
n的最大值为255;
要用引号引起来
数字:
int:整数;
float(m,n):单精度小数,m表示总共有多少位,n表示小数有几位;
double(m,n):双精度小数,m表示总共有多少位,n表示小数有几位;
decimal(m,n):比double更精确,m表示总共有多少位,n表示小数有几位;
日期:
date:日期。格式:YYYY-MM-DD 注释:支持的范围是从 '1000-01-01' 到 '9999-12-31'
所有的日期都用引号引起来
DATETIME:*日期和时间的组合。格式:YYYY-MM-DD HH:MM:SS
注释:支持的范围是从 '1000-01-01 00:00:00' 到 '9999-12-31 23:59:59'
创建表格:
mysql> create table student2(sid int ,sname varchar(10),birthday date,
-> score double(5,2));
Query OK, 0 rows affected (0.01 sec)
插入数据:mysql> insert into student2 (sid,sname,birthday,score) values(1001,'tom','2000-01-11',99.99);
查看表中的所有数据:mysql> select * from student2;
+------+-------+------------+-------+
| sid | sname | birthday | score |
+------+-------+------------+-------+
| 1001 | tom | 2000-01-11 | 99.99 |
| 1001 | bob | 2000-01-11 | 99.99 |
| 1001 | lucy | 2000-01-11 | 99.99 |
+------+-------+------------+-------+
3 rows in set (0.00 sec)
mysql Day2
设置命令行显示时的编码格式:
命令行窗口是windows自带的,它的编码是gbk;
执行set names gbk;告诉mysql把数据显示到命令行的时候用gbk显示;
这个语句在命令行关闭后失效,下次重新设置;
一、对表结构的操作:
1.增加一个字段
语法:alter table 表名 add column 新字段名 字段类型;
mysql> alter table student2 add column sex varchar(10);
2.修改一个字段
语法: alter table 表名 modify 列名 新数据类型;
mysql> alter table student2 modify sex int;
3.更改字段名
语法:alter table 表名 change 原列名 新列名 数据类型;
mysql> alter table student2 change sex gender varchar(10) ;
4. 删除表的字段
语法: alter table 表名 drop 列名;
mysql> alter table student2 drop gender;
5.重命名表
语法:alter table 表名 rename to 新表名;
mysql> alter table student2 rename to stu;
6.复制表
6.1复制表结构及数据
语法:create table 新表 select * from 旧表;
mysql> update student set sal=120 where sal is null;
mysql> select * from student;
6.2 只复制表结构
语法:create table 新表 select * from 旧表 where 1=0;
mysql> create table stu3 select * from stu where 1>9;
7.删除表
语法: drop table 表名;
mysql> drop table student;
8.索引:查询快,增删慢;不可滥用;
创建索引:
语法:create index 索引名 on 表名(字段名);
mysql> create index my_index1 on stu2(sname);
查看索引:
mysql> show index from stu2;
删除索引:
mysql> drop index my_index1 on stu2;
二、mysql约束:
1.非空约束 not null:限制某一列的值不能为null;
方式1:在建表语句中字段类型的后面加上 not null;
mysql> create table emp2(empno int ,ename varchar(10) not null );
方式2:
mysql> alter table emp2 modify empno int not null;
删除非空约束:
mysql> alter table emp2 modify empno int null;
2.默认值 default:给某一列设置默认值;
默认情况下的默认值为null;
方式1:
mysql> alter table emp modify empno int default 0;
方式2:在建表语句中字段类型的后面加上 default 默认值;
mysql> create table emp2(empno int ,ename varchar(10) default '张三');
mysql> alter table emp2 modify ename varchar(10) default null;
3.唯一约束 unique:限制列里面的值不能重复;
方式1:
mysql> alter table emp2 modify empno int unique;
方式2:在建表语句中字段类型的后面加上 unique
mysql> create table emp3(empno int unique);
方式3:自定义名字的
mysql> alter table emp3 add constraint my_unique unique(empno);
删除唯一约束:
语法:alter table 表名 drop index 唯一约束的索引名(默认为所在的字段名);
mysql> alter table emp3 drop index empno;
4.主键约束 primary key(非空+唯一)
方式1:
mysql> alter table emp modify empno int primary key;
方式2:在建表语句中字段类型的后面加上 primary key;
mysql> create table emp10(empno int primary key,ename varchar(10));
方式3:自定义名字的
mysql> create table emp11(empno int ,ename char(10),constraint my_key primary key(empno));
删除主键约束:
方式1:mysql> alter table emp11 drop primary key;
5.主键自增长auto_increment
方式1:在建表语句中字段类型的后面加上 primary key auto_increment;
方式2:mysql> alter table emp12 modify empno int primary key auto_increment;
删除自增长: alter table emp modify empno int;
6.外键约束 foreign key 不可滥用
方式1:
alter table 表名 add constraint 自定义外键名 foreign key(本表字段) references 被参考的表名(被参考的字段);
mysql> alter table emp add constraint fk_deptno foreign key(deptno) references dept(deptno);
方式2:
在建表语句最后一个字段的后面 constraint 自定义外键名 foreign key(本表字段) references 被参考的表名(被参考的字段)
删除外键:
语法:alter table emp drop foreign 外键名;
mysql> alter table emp drop foreign key fk_deptno;
mysql Day2 作业:
1.创建学生表student:学生编号sid 主键自增长,姓名sname 非空,性别sex 非空,生日birthday ,年龄age ,班级编号cid ;
mysql> create table student(sid int primary key auto_increment,sname varchar(10)
not null,sex varchar(10) not null,birthday date,age int,cid int);
Query OK, 0 rows affected (0.03 sec)
这是学生表student:
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| sid | int(11) | NO | PRI | NULL | auto_increment |
| sname | varchar(10) | NO | | | |
| sex | varchar(10) | NO | | | |
| birthday | date | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| cid | int(11) | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
2.创建班级表class: 班级编号 cid 主键 ,班级名称cname 非空;
mysql> create table class(cid int primary key,cname varchar(10) not null);
Query OK, 0 rows affected (0.01 sec)
mysql> desc class;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| cid | int(11) | NO | PRI | | |
| cname | varchar(10) | NO | | | |
+-------+-------------+------+-----+---------+-------+
3.给学生表添加一个字段:住址address;
mysql> alter table student add column address varchar(10);
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
这是增加的字段address:
mysql> desc student;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| sid | int(11) | NO | PRI | NULL | auto_increment |
| sname | varchar(10) | NO | | | |
| sex | varchar(10) | NO | | | |
| birthday | date | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| cid | int(11) | YES | | NULL | |
| address | varchar(10) | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
7 rows in set (0.01 sec)
4.修改学生表性别字段为gender;
mysql> alter table student change sex gender varchar(10);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
修改后的student表:
mysql> desc student;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| sid | int(11) | NO | PRI | NULL | auto_increment |
| sname | varchar(10) | NO | | | |
| gender | varchar(10) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| cid | int(11) | YES | | NULL | |
| address | varchar(10) | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
7 rows in set (0.01 sec)
5.给birthday添加默认值为2000-01-01;
mysql> alter table student modify birthday date default '2000-01-01';
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
改变birthday的默认值:
mysql> desc student;
+----------+-------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+------------+----------------+
| sid | int(11) | NO | PRI | NULL | auto_increment |
| sname | varchar(10) | NO | | | |
| gender | varchar(10) | YES | | NULL | |
| birthday | date | YES | | 2000-01-01 | |
| age | int(11) | YES | | NULL | |
| cid | int(11) | YES | | NULL | |
| address | varchar(10) | YES | | NULL | |
+----------+-------------+------+-----+------------+----------------+
6.给性别字段添加默认值为男;
mysql> set names gbk;
Query OK, 0 rows affected (0.02 sec)
mysql> alter table student modify gender varchar(10) default '男';
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+----------+-------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+------------+----------------+
| sid | int(11) | NO | PRI | NULL | auto_increment |
| sname | varchar(10) | NO | | | |
| gender | varchar(10) | YES | | 男 | |
| birthday | date | YES | | 2000-01-01 | |
| age | int(11) | YES | | NULL | |
| cid | int(11) | YES | | NULL | |
| address | varchar(10) | YES | | NULL | |
+----------+-------------+------+-----+------------+----------------+
7.删除age字段;
mysql> alter table student drop age;
Query OK, 0 rows affected (0.01 sec)
mysql> desc student;
+----------+-------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+------------+----------------+
| sid | int(11) | NO | PRI | NULL | auto_increment |
| sname | varchar(10) | NO | | | |
| gender | varchar(10) | YES | | 男 | |
| birthday | date | YES | | 2000-01-01 | |
| cid | int(11) | YES | | NULL | |
| address | varchar(10) | YES | | NULL | |
+----------+-------------+------+-----+------------+----------------+
9.给班级表插入3个班级;
mysql> insert into class values(1001,'一班');
Query OK, 1 row affected (0.03 sec)
mysql> insert into class values(1002,'二班');
Query OK, 1 row affected (0.00 sec)
mysql> insert into class values(1003,'三班');
Query OK, 1 row affected (0.00 sec)
mysql> select * from class;
+------+-------+
| cid | cname |
+------+-------+
| 1001 | 一班 |
| 1002 | 二班 |
| 1003 | 三班 |
+------+-------+
10.在学生表中给每个班级添加3名学生信息;
mysql> insert into student values(1,'张三','男','2012-01-02',1001,'cq');
Query OK, 1 row affected (0.00 sec)
mysql> insert into student values(null,'李四','女','2009-03-22',1002,'cq');
Query OK, 1 row affected (0.00 sec)
mysql> insert into student values(null,'王五','男','2009-03-25',1003,'cq');
Query OK, 1 row affected (0.00 sec)
mysql> select *from student;
+-----+-------+--------+------------+------+---------+
| sid | sname | gender | birthday | cid | address |
+-----+-------+--------+------------+------+---------+
| 1 | 张三 | 男 | 2012-01-02 | 1001 | cq |
| 2 | 李四 | 女 | 2009-03-22 | 1002 | cq |
| 3 | 王五 | 男 | 2009-03-25 | 1003 | cq |
+-----+-------+--------+------------+------+---------+
8.给学生表的cid上添加外键约束,以参考class表中的cid字段;
mysql> alter table student add constraint fk_cid foreign key(cid) references class(cid);
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
受到约束报错:
mysql> insert into student values(null,'王五','男','2009-03-25',1004,'cq');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f
ails (`c17/student`, CONSTRAINT `fk_cid` FOREIGN KEY (`cid`) REFERENCES `class`
(`cid`))
11.删除外键;
mysql> alter table student drop foreign key fk_cid;
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
删除后可以往student继续添加学生信息:
mysql> insert into student values(null,'王五','男','2009-03-25',1004,'cq');
Query OK, 1 row affected (0.00 sec)
mysql> select *from student;
+-----+-------+--------+------------+------+---------+
| sid | sname | gender | birthday | cid | address |
+-----+-------+--------+------------+------+---------+
| 1 | 张三 | 男 | 2012-01-02 | 1001 | cq |
| 2 | 李四 | 女 | 2009-03-22 | 1002 | cq |
| 3 | 王五 | 男 | 2009-03-25 | 1003 | cq |
| 4 | 王五 | 男 | 2009-03-25 | 1004 | cq |
+-----+-------+--------+------------+------+---------+
4 rows in set (0.00 sec)
12.复制student表到student2;
mysql> create table student2 select * from student;
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select *from student2;
+-----+-------+--------+------------+------+---------+
| sid | sname | gender | birthday | cid | address |
+-----+-------+--------+------------+------+---------+
| 1 | 张三 | 男 | 2012-01-02 | 1001 | cq |
| 2 | 李四 | 女 | 2009-03-22 | 1002 | cq |
| 3 | 王五 | 男 | 2009-03-25 | 1003 | cq |
| 4 | 王五 | 男 | 2009-03-25 | 1004 | cq |
+-----+-------+--------+------------+------+---------+
13.将表student2的名字改为stu;
改前所有的表:
mysql> show tables;
+---------------+
| Tables_in_c17 |
+---------------+
| class |
| student |
| student2 |
+---------------+
改后所有的表:
mysql> show tables;
+---------------+
| Tables_in_c17 |
+---------------+
| class |
| stu |
| student |
+---------------+
14.删除stu表中sid的自增长;
mysql> desc stu;
+----------+-------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+------------+----------------+
| sid | int(11) | NO | PRI | NULL | auto_increment |
| sname | varchar(10) | NO | | | |
| gender | varchar(10) | YES | | 男 | |
| birthday | date | YES | | 2000-01-01 | |
| cid | int(11) | YES | | NULL | |
| address | varchar(10) | YES | | NULL | |
+----------+-------------+------+-----+------------+----------------+
mysql> alter table stu modify sid int;
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> desc stu;
+----------+-------------+------+-----+------------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+------------+-------+
| sid | int(11) | NO | PRI | 0 | |
| sname | varchar(10) | NO | | | |
| gender | varchar(10) | YES | | 男 | |
| birthday | date | YES | | 2000-01-01 | |
| cid | int(11) | YES | | NULL | |
| address | varchar(10) | YES | | NULL | |
+----------+-------------+------+-----+------------+-------+
15.删除stu表;
删除前:
mysql> show tables;
+---------------+
| Tables_in_c17 |
+---------------+
| class |
| stu |
| student |
+---------------+
mysql> drop table stu;
Query OK, 0 rows affected (0.01 sec)
删除后:
mysql> show tables;
+---------------+
| Tables_in_c17 |
+---------------+
| class |
| student |
+---------------+
2 rows in set (0.00 sec)