1.数据库
创建数据库
create database kb21;
create database if not exists kb21;#有礼貌(推荐使用)
删除数据库
drop database kb21;
drop database if exists kb21;#推荐使用
查看所有数据库名
show databases;
选中指定数据库,使用kb21
mysql> use kb21;
Database changed
查看当前使用的数据库
mysql> select database();
+------------+
| database() |
+------------+
| kb21 |
+------------+
1 row in set (0.00 sec)
2.创建表
创建表
mysql> create table if not exists `stu`(`id` int(10),`name` varchar(32));
Query OK, 0 rows affected, 1 warning (0.05 sec)
#`不是单引号是键盘1旁边的符号
#如果确定student不是mysql中的关键字可以不加`
更完善的新建
create table if not exists student(`id` int(10) primary key auto_increment,`name` varchar(32) not null comment "学生姓名",`age` int(10) not null comment "学生年龄",`email` varchar(255) comment"学生邮箱");
if not exits 是否存在,不存在则按后面的要求创建
primary key 表示主键
auto_increment表示自增长(id不用输入了,会自动增长)
varchar(32)表示字符串最多32位
int(10)表示整数类型最多10位
not null biaoshi 不允许为空,必须设置值,否则会报错,没写则默认为空
举例:
mysql> insert into student(`name`,`age`) value('zs');
ERROR 1136 (21S01): Column count doesn't match value count at row 1
成功插入举例:
mysql> insert into student(`name`,`age`) value('zs',18);
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+----+------+-----+-------+
| id | name | age | email |
+----+------+-----+-------+
| 1 | zs | 18 | NULL |
+----+------+-----+-------+
1 row in set (0.00 sec)
mysql> insert into student(`name`,`age`,`email`) values('ls',19,'ls@qq.com');
Query OK, 1 row affected (0.01 sec)
mysql> select * from student;
+----+------+-----+-----------+
| id | name | age | email |
+----+------+-----+-----------+
| 1 | zs | 18 | NULL |
| 2 | ls | 19 | ls@qq.com |
+----+------+-----+-----------+
2 rows in set (0.00 sec)
添加老师类(举例):
mysql> create table if not exists teacher(`id` int(10) primary key auto_increment,`name` varchar(32) not null comment "老师姓 名",`age` int not null comment "老师年龄",`tel` varchar(32) not null default "021-43579802" comment "练习电话",`email` varchar(255),`school` varchar(100) default "南京大学");
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> insert into teacher(`name`,`age`,`tel`,`email`,school) value('zls',29,'1501231233','lj@qq.com','bdqn');
Query OK, 1 row affected (0.01 sec)
mysql> select * from teacher;
+----+------+-----+------------+-----------+--------+
| id | name | age | tel | email | school |
+----+------+-----+------------+-----------+--------+
| 1 | zls | 29 | 1501231233 | lj@qq.com | bdqn |
+----+------+-----+------------+-----------+--------+
1 row in set (0.00 sec)
查看表结构
mysql> desc stu;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
数据库中表列值类型:
数值类型
字符串类型
日期和时间型数值类型
null值
查看建表语句
mysql> show create table stu;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------+
| stu | CREATE TABLE `stu` (
`id` int DEFAULT NULL,
`name` varchar(32) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
查看当前库中所有的表
mysql> show tables;
+----------------+
| Tables_in_kb21 |
+----------------+
| stu |
+----------------+
1 row in set (0.00 sec)
3.操作表
mysql> select * from stu;
Empty set (0.00 sec)
mysql> desc stu;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
插入一条
mysql> select * from stu;
+------+------+
| id | name |
+------+------+
| 1 | za |
+------+------+
1 row in set (0.00 sec)
插入多条
mysql> insert into stu(id,name) values(2,"ls"),(3,"ww"),(4,"zl");
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from stu;
+------+------+
| id | name |
+------+------+
| 1 | za |
| 2 | ls |
| 3 | ww |
| 4 | zl |
+------+------+
4 rows in set (0.00 sec)
删除表
drop table if exists stu;
mysql> show tables;
+----------------+
| Tables_in_kb21 |
+----------------+
| stu |
| teacher |
+----------------+
2 rows in set (0.01 sec)
mysql> drop table if exists stu;
Query OK, 0 rows affected (0.02 sec)
mysql> drop table if exists teacher;
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
Empty set (0.00 sec)
小练习
mysql> create table if not exists student(`StudentNo` int(4) primary key auto_increment,`LoginPwd` varchar(20),`StudentName` varchar(20) comment"学生姓名",`Sex` tinyint(1) comment"性别,取值0或1",`GradeId` int(11) comment"年级编号",`Phone` varchar(50)not null comment"联系电话,允许为空,即可选输入",`Address` varchar(255) not null comment"地址,允许为空,即可选输入",`BornDate` datetime comment "出生时间",`Email` varchar(50) not null comment"邮箱账号,允许为空,即可输入",`IdentityCard` varchar(18) comment"身份证号");
Query OK, 0 rows affected, 3 warnings (0.02 sec)
mysql> desc student;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| StudentNo | int | NO | PRI | NULL | auto_increment |
| LoginPwd | varchar(20) | YES | | NULL | |
| StudentName | varchar(20) | YES | | NULL | |
| Sex | tinyint(1) | YES | | NULL | |
| GradeId | int | YES | | NULL | |
| Phone | varchar(50) | NO | | NULL | |
| Address | varchar(255) | NO | | NULL | |
| BornDate | datetime | YES | | NULL | |
| Email | varchar(50) | NO | | NULL | |
| IdentityCard | varchar(18) | YES | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
10 rows in set (0.01 sec)
mysql> show create table student;
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
`StudentNo` int NOT NULL AUTO_INCREMENT,
`LoginPwd` varchar(20) DEFAULT NULL,
`StudentName` varchar(20) DEFAULT NULL COMMENT '学生姓名',
`Sex` tinyint(1) DEFAULT NULL COMMENT '性别,取值0或1',
`GradeId` int DEFAULT NULL COMMENT '年级编号',
`Phone` varchar(50) NOT NULL COMMENT '联系电话,允许为空,即可选输入',
`Address` varchar(255) NOT NULL COMMENT '地址,允许为空,即可选输入',
`BornDate` datetime DEFAULT NULL COMMENT '出生时间',
`Email` varchar(50) NOT NULL COMMENT '邮箱账号,允许为空,即可输入',
`IdentityCard` varchar(18) DEFAULT NULL COMMENT '身份证号',
PRIMARY KEY (`StudentNo`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
4.修改表
alter table 原名称 rename as 新名称;
mysql> show tables;
+----------------+
| Tables_in_kb21 |
+----------------+
| student |
| subject |
| teacher1 |
+----------------+
3 rows in set (0.00 sec)
mysql> alter table teacher1 rename as teacher;
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+----------------+
| Tables_in_kb21 |
+----------------+
| student |
| subject |
| teacher |
+----------------+
3 rows in set (0.00 sec)
修改表字段
alter table 表名 change 想改的名称 想改成的名称字段类型 not null default ‘未知’
新增列
alter table 表名 add 添加的字段名称 字段类型 not null default‘默认’
删除列
alter table 表名 drop 想删除的字段名称