create database 库名 charset utf8;
使用sql语句查看
语法:show databases
;
查看已有数据库的创建信息
语法:show create database 数据库名
;
使用图形化界面查看创建信息
选择要查看的库名右键-“Edit Database”
alter database 数据库名 character set 新编码
;注意:数据库一旦删除则数据库中的表和数据都会被一同删除
使用SQL语句删除
语法:drop database 库名
;
例如:删除test1库
drop database test1
使用图形化界面删除
选中要删除的库名–右键–选中“Delete Database”
数据库和表的关系
每个数据库包含N张表,及表示在库中
表(二维表)
行和列组成:可以将复杂的数据保存在简单的表中
表中的每一行就是一条完整的记录
表中的列用于保存每条记录中特点的信息
create table 表名(
字段名1 字段类型1 [完整性约束],
字段名2 字段类型2 [完整性约束],
......
字段名n 字段类型n [完整性约束]
)
注意:
例如创建一个学生信息表students(id,name,gender,age,height,cls_id)等
create table students(
id int unsigned primary key auto_increment not null,
name varchar(20) default '',
age tinyint unsigned default 0,
height decimal(5,2),
gender enum('男','女','保密') default '保密',
cls_id int unsigned default 0,
is_delete bit default 0
);
b.使用图形化界面
点击选择的库名双击 选择“Table” 右键–“New Table”
查看表的基本结构
语法: desc 表名
例如:查看student表的结构
mysql> desc students;
+-----------+----------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | | |
| age | tinyint(3) unsigned | YES | | 0 | |
| height | decimal(5,2) | YES | | NULL | |
| gender | enum('男','女','保密') | YES | | 保密 | |
| cls_id | int(10) unsigned | YES | | 0 | |
| is_delete | bit(1) | YES | | b'0' | |
+-----------+----------------------------+------+-----+---------+----------------+
查看表的详细结构(还可以查看存储引擎和字符集)
语法:show create table 表名
;
例如:查看student表的详细结构
mysql> show create table students \G;
*************************** 1. row ***************************
Table: students
Create Table: CREATE TABLE `students` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT '',
`age` tinyint(3) unsigned DEFAULT '0',
`height` decimal(5,2) DEFAULT NULL,
`gender` enum('男','女','保密') DEFAULT '保密',
`cls_id` int(10) unsigned DEFAULT '0',
`is_delete` bit(1) DEFAULT b'0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
语法: alter table 旧表名 rename 新表名
例如:将students表名修改为student;
alter table students rename student;
或者 rename table 旧表名 to 新表名
语法:alter table 表名 modify 列名 新字段类型
例如:将student表中id的数据类型 修改为int(10)
mysql> alter table student modify id int(10);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
mysql> desc student;
+-----------+----------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------------------------+------+-----+---------+-------+
| id | int(10) | NO | PRI | NULL | |
| name | varchar(20) | YES | | | |
| age | tinyint(3) unsigned | YES | | 0 | |
| height | decimal(5,2) | YES | | NULL | |
| gender | enum('男','女','保密') | YES | | 保密 | |
| cls_id | int(10) unsigned | YES | | 0 | |
| is_delete | bit(1) | YES | | b'0' | |
+-----------+----------------------------+------+-----+---------+-------+
语法:
alter table 表名 modify 列名1 新字段类型1,
modify 列名2 新字段类型2,
.....
modify 列名n 新字段类型n;
例如:修改student 表中id int(8) , name varchar(30)
mysql> alter table student modify id int(8),modify name varchar(30);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+-----------+----------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------------------------+------+-----+---------+-------+
| id | int(8) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | 0 | |
| height | decimal(5,2) | YES | | NULL | |
| gender | enum('男','女','保密') | YES | | 保密 | |
| cls_id | int(10) unsigned | YES | | 0 | |
| is_delete | bit(1) | YES | | b'0' | |
+-----------+----------------------------+------+-----+---------+-------+
语法:alter table 表名 change 旧字段名 新字段名 旧字段类型
例如:student 表中gender 修改为sex
mysql> alter table student change gender sex enum('男','女','保密');
语法:alter table 表名 change 旧字段名 新字段名 新字段类型
例如:student 表中sex 修改为gender 数据类型 varchar(2)
mysql> alter table student change sex gender varchar(2);
mysql> desc student;
+-----------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+-------+
| id | int(8) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | 0 | |
| height | decimal(5,2) | YES | | NULL | |
| gender | varchar(2) | YES | | NULL | |
| cls_id | int(10) unsigned | YES | | 0 | |
| is_delete | bit(1) | YES | | b'0' | |
+-----------+---------------------+------+-----+---------+-------+
语法:alter table 表名 add 字段名 字段类型 [完整性约束] [first |after 字段名]
例1:在student 表末尾添加score字段
mysql> alter table student add score smallint(3) not null default 60;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from student;
Empty set (0.01 sec)
mysql> desc student;
+-----------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+-------+
| id | int(8) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | 0 | |
| height | decimal(5,2) | YES | | NULL | |
| gender | varchar(2) | YES | | NULL | |
| cls_id | int(10) unsigned | YES | | 0 | |
| is_delete | bit(1) | YES | | b'0' | |
| score | smallint(3) | NO | | 60 | |
+-----------+---------------------+------+-----+---------+-------+
例2:在student 表第一个位置添加phone字段
mysql> alter table student add phone char(11) not null first;
mysql> desc student;
+-----------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+-------+
| phone | char(11) | NO | | NULL | |
| id | int(8) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | 0 | |
例3:在student 表中age的后面添加class字段
mysql> alter table student add class varchar(6) after age;
语法: alter table 表名 drop 列名
例:删除表 student 中phone
mysql> alter table student drop phone;
drop table 表名
;mysql> create table test1(
-> id int(8)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+--------------------+
| Tables_in_students |
+--------------------+
| student |
| test1 |
+--------------------+
mysql> drop table test1;
mysql> show tables;
+--------------------+
| Tables_in_students |
+--------------------+
| student |
+--------------------+
删除表中所有字段,保留表结构语法:truncate table 表名
b.使用图形化界面删除
1.为所有字段插入一条数据
语法:
insert [into] 表名[(字段名1,字段名2,字段名3,....)] values|value(值1,值2,值3....)
;
例如:创建一张表student(sno主键自增 sname非空 age sex 默认 男 email 唯一)
mysql> create table students(
id int unsigned primary key auto_increment not null,
name varchar(20),
age smallint,
sex enum('男','女','保密') default '保密',
email varchar(20) unique );
mysql> desc students;
+-------+----------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| age | smallint(6) | YES | | NULL | |
| sex | enum('男','女','保密') | YES | | 保密 | |
| email | varchar(20) | YES | UNI | NULL | |
+-------+----------------------------+------+-----+---------+----------------+
为student表所有字段插入数据
注意:表后面可以加字段名也可以不加字段名,如果没有添加字段名则插入的值顺序要与表结构字段顺序相同
# 插入单条数据
mysql> insert into students (name,age,sex,email) value ("laowang",18,'男','[email protected]');
mysql> insert into students (name,age,sex,email) value ("laowang",18,'男','[email protected]');
# 一次性插入多条数据
mysql> insert into students (name,age,sex,email) values ("laoyang",19,'女','[email protected]'),
('laoli',20,'女','[email protected]');
mysql> select * from students;
+----+----------+------+------+----------------+
| id | name | age | sex | email |
+----+----------+------+------+----------------+
| 1 | laowang | 18 | 男 | sss@qq.com |
| 4 | laozhang | 19 | 女 | aasss@qq.com |
| 5 | laoyang | 19 | 女 | sfaasss@qq.com |
| 6 | laoli | 20 | 女 | 1dsf@qq.com |
+----+----------+------+------+----------------+
2.为指定字段插入一条数据
语法:insert [into] 表名(字段名1,字段名2,字段名3,....) values|value(值1,值2,值3....)
;
例如:给表student中sname和age插入数据
insert into students (name,age) value ('xiaomie',20)
select * from students
mysql> select * from students;
+----+----------+------+--------+----------------+
| id | name | age | sex | email |
+----+----------+------+--------+----------------+
| 1 | laowang | 18 | 男 | sss@qq.com |
| 4 | laozhang | 19 | 女 | aasss@qq.com |
| 5 | laoyang | 19 | 女 | sfaasss@qq.com |
| 6 | laoli | 20 | 女 | 1dsf@qq.com |
| 7 | xiaomie | 20 | 保密 | NULL |
+----+----------+------+--------+----------------+
3.使用SET方式插入一条数据
语法:
insert [into] 表名 set 字段名1=字段值[,字段名2=字段值2....]
;
例如:使用set方式给student表所有字段插入数据
insert into students set name="狄仁杰",age=18,sex='男',email='[email protected]';
4.同时插入多条数据
语法:
insert [into] 表名[(字段名1,字段名2,字段名3,....)] values|value(值1,值2,值3....),
(值1,值2,值3....),
.......
例如给表student同时插入多条数据
a.给所有字段插入多条数据
mysql> insert into students (name,age,sex,email) values ("laoyang",19,'女','[email protected]'),
('laoli',20,'女','[email protected]');
b.给指定字段插入多条数据
insert into students (name,age) values ('bertwu',18),
('tom',20)
5.插入查询结果
语法:
insert [into] 表名(字段名1,字段名2..) select 字段名1,字段名2... from 表 where 条件
注意:插入表的字段和查询的字段个数和类型要保持一致
例如:将student表中的数据查询结果插入到t_student表
1.创建t_student表
create table t_student (
id int unsigned primary key auto_increment not null,
name varchar(20),
age smallint,
sex enum('男','女','保密') default '保密',
email varchar(20) unique);
# 查询表结构,与student表保持一致
mysql> desc t_student;
+-------+----------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| age | smallint(6) | YES | | NULL | |
| sex | enum('男','女','保密') | YES | | 保密 | |
| email | varchar(20) | YES | UNI | NULL | |
+-------+----------------------------+------+-----+---------+----------------+
2.使用插入查询结果为女插入到t_student表
insert into t_student select * from students where sex ='女';
mysql> select * from t_student;
+----+----------+------+------+----------------+
| id | name | age | sex | email |
+----+----------+------+------+----------------+
| 4 | laozhang | 19 | 女 | aasss@qq.com |
| 5 | laoyang | 19 | 女 | sfaasss@qq.com |
| 6 | laoli | 20 | 女 | 1dsf@qq.com |
+----+----------+------+------+----------------+
3 rows in set (0.00 sec)
1.更新指定数据
语法:
update 表名 set 字段名1=值1 [,字段名2=值2....] where 条件
例如:将students表中姓名为laowang的姓名修改为"小乔"
update students set name="小乔" where name = 'laowang'
2.更新全部数据
语法:
update 表名 set 字段名1=值1 [,字段名2=值2....]
例如:将student表中年龄都修改为18
update students set name="小乔" where name = 'laowang'
mysql> select * from students;
+----+-----------+------+--------+----------------+
| id | name | age | sex | email |
+----+-----------+------+--------+----------------+
| 1 | 小乔 | 18 | 男 | sss@qq.com |
| 4 | laozhang | 18 | 女 | aasss@qq.com |
| 5 | laoyang | 18 | 女 | sfaasss@qq.com |
| 6 | laoli | 18 | 女 | 1dsf@qq.com |
| 7 | xiaomie | 18 | 保密 | NULL |
| 8 | 狄仁杰 | 18 | 男 | fsdfs@qq.com |
| 10 | tom | 18 | 保密 | NULL |
+----+-----------+------+--------+----------------+
1.删除指定数据
语法:
delete from 表名 where 条件
例如:删除性别为保密的学生信息
mysql> delete from students where sex="保密";
2.删除全部数据
语法:delete from 表名
例如:删除学生表的全部数据
delete from students
3.truncate关键字删除数据
语法: truncate 表名
例如:删除t_student表中的全部数据
truncate t_student
delete是数据操纵语言DML 而truncate是数据定义语言DDL
delete删除数据是一条一条删除
truncate删除数据是保留表结构,直接删除数据,删除后表的状态相当于新表
truncate的效率比delete高
delete删除数据,会显示删除行数
truncate不会显示
delete删除记录后,再次向表添加数据,自增的值会在原来最大值+1
truncate删除记录后,会从1开始自增
delete操作可以进行回滚
truncate操作隐式提交,不可以回滚
回滚就类似你电脑的回收站一样,delete删除的数据可以恢复,而truncate删除的数据不能回复。