时间:2021-07-01 10:21:17 帮助过:12人阅读
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sakila | | sys | | world | +--------------------+ 6 rows in set (0.00 sec)
2、创建数据库(即在磁盘上创建一个对应的文件夹)
语法:create database [if not exists] db_name [character set xxx];
命令 可选判断 数据库名 数据库的字符编码
mysql> CREATE DATABASE IF NOT EXISTS student CHARACTER SET utf8; Query OK, 1 row affected (0.01 sec) mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sakila | | student | | sys | | world | +--------------------+ 7 rows in set (0.00 sec)
3、查询一个数据库的创建方式(创建命令)
语法:show create database db_name ;
命令 数据库名
mysql> SHOW CREATE DATABASE student; +----------+------------------------------------------------------------------+ | Database | Create Database | +----------+------------------------------------------------------------------+ | student | CREATE DATABASE `student` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+------------------------------------------------------------------+ 1 row in set (0.04 sec)
4、修改数据库字符编码
语法:alter database db_name character set gbk;
命令 数据库名 修改字符编码
mysql> ALTER DATABASE student CHARACTER SET gbk; Query OK, 1 row affected (0.01 sec) mysql> SHOW CREATE DATABASE student; +----------+-----------------------------------------------------------------+ | Database | Create Database | +----------+-----------------------------------------------------------------+ | student | CREATE DATABASE `student` /*!40100 DEFAULT CHARACTER SET gbk */ | +----------+-----------------------------------------------------------------+ 1 row in set (0.00 sec) #注:修改数据库名字,直接找到该数据库文件夹,修改文件夹名字
5、删除数据库
mysql> DROP DATABASE student; Query OK, 0 rows affected (0.00 sec) mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sakila | | sys | | world | +--------------------+ 6 rows in set (0.00 sec)
6、使用数据库(即切换数据库,进入到数据库内)
mysql> USE mysql; Database changed mysql> SELECT database(); +------------+ | database() | +------------+ | mysql | +------------+ 1 row in set (0.00 sec)
-- 注意:进入到某个数据库后没办法再退回之前状态,但可以通过use进行切换
查看当前使用的数据库 select database();
数据表的操作
表内的列:字段
表内的行:表记录
1、创建一个表(进入库里):最后一个字段不加逗号结束
语法:create table table_name(
字段名 字段数据类型 [约束] ,
字段名 字段数据类型 [约束] ,
字段名 字段数据类型 [约束] ,
....
字段名 字段数据类型 [约束] ) [character set xxx];
约束: (not null + unique就等于主键),约束不止这三个
主键(primary key):非空 且 唯一
not null: 非空约束
unique: 唯一约束
mysql> CREATE DATABASE teachers; Query OK, 1 row affected (0.00 sec) mysql> USE teachers; Database changed mysql> CREATE TABLE teacher1( -> id INT primary key, -> name VARCHAR(20) not null, -> age INT, -> birth DATE); Query OK, 0 rows affected (0.51 sec)
2、查看数据库的表
mysql> SHOW TABLES; +--------------------+ | Tables_in_teachers | +--------------------+ | teacher1 | +--------------------+ 1 row in set (0.00 sec)
3、查看表结构
mysql> DESC teacher1; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(20) | NO | | NULL | | | age | int(11) | YES | | NULL | | | birth | date | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
4、查看创建表命令
mysql> SHOW CREATE TABLE teacher1; +----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | teacher1 | CREATE TABLE `teacher1` ( `id` int(11) NOT NULL, `name` varchar(20) NOT NULL, `age` int(11) DEFAULT NULL, `birth` date DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
5、增加列(字段):可以增加多个列,类似于表创建的格式。
语法:alter table tab_name add [column] 列名 类型 [完整性约束条件] [first|after 字段名];
命令 表名 添加 列 定义列位置,first最前边,after在某个字段之后
mysql> ALTER TABLE teacher1 ADD COLUMN sex INT; Query OK, 0 rows affected (0.58 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC teacher1; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(20) | NO | | NULL | | | age | int(11) | YES | | NULL | | | birth | date | YES | | NULL | | | sex | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+
注:COLUMN可以省略
mysql> ALTER TABLE teacher1 ADD addr VARCHAR(20) first; Query OK, 0 rows affected (0.53 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC teacher1; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | addr | varchar(20) | YES | | NULL | | | id | int(11) | NO | PRI | NULL | | | name | varchar(20) | NO | | NULL | | | age | int(11) | YES | | NULL | | | birth | date | YES | | NULL | | | sex | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 6 rows in set (0.00 sec)
6、修改一列类型
语法:alter table tab_name modify 列名 类型 [完整性约束条件] [first|after 字段名];
mysql> ALTER TABLE teacher1 MODIFY sex CHAR AFTER id; Query OK, 0 rows affected (0.82 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC teacher1; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | addr | varchar(20) | YES | | NULL | | | id | int(11) | NO | PRI | NULL | | | sex | char(1) | YES | | NULL | | | name | varchar(20) | NO | | NULL | | | age | int(11) | YES | | NULL | | | birth | date | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 6 rows in set (0.00 sec)
7、修改列名
语法:alter table tab_name change [column] 列名 新列名 类型 [完整性约束条件] [first|after 字段名];
mysql> ALTER TABLE teacher1 CHANGE birth birthday DATE; Query OK, 0 rows affected (0.12 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC teacher1; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | addr | varchar(20) | YES | | NULL | | | id | int(11) | NO | PRI | NULL | | | sex | char(1) | YES | | NULL | | | name | varchar(20) | NO | | NULL | | | age | int(11) | YES | | NULL | | | birthday | date | YES | | NULL | | +----------+-------------+------+-----+---------+-------+ 6 rows in set (0.00 sec)
8、删除一列
语法:alter table tab_name drop [column] 列名;
mysql> ALTER TABLE teacher1 DROP sex; Query OK, 0 rows affected (0.50 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC teacher1; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | addr | varchar(20) | YES | | NULL | | | id | int(11) | NO | PRI | NULL | | | name | varchar(20) | NO | | NULL | | | age | int(11) | YES | | NULL | | | birthday | date | YES | | NULL | | +----------+-------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
9、修改表名
语法:rename table 表名 to 新表名;
mysql> RENAME TABLE teacher1 to teacher; Query OK, 0 rows affected (0.14 sec) mysql> DESC teacher1; ERROR 1146 (42S02): Table ‘teachers.teacher1‘ doesn‘t exist mysql> DESC teacher; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | addr | varchar(20) | YES | | NULL | | | id | int(11) | NO | PRI | NULL | | | name | varchar(20) | NO | | NULL | | | age | int(11) | YES | | NULL | | | birthday | date | YES | | NULL | | +----------+-------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
10、修该表所用的字符集
语法:alter table 表名 character set 编码;
mysql> ALTER TABLE teacher CHARACTER SET gbk; Query OK, 0 rows affected (0.09 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> SHOW CREATE TABLE teacher; +---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | teacher | CREATE TABLE `teacher` ( `addr` varchar(20) CHARACTER SET utf8 DEFAULT NULL, `id` int(11) NOT NULL, `name` varchar(20) CHARACTER SET utf8 NOT NULL, `age` int(11) DEFAULT NULL, `birthday` date DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk | +---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
11、删除除表
语法:drop table table_name
mysql> DROP TABLE teacher; Query OK, 0 rows affected (0.19 sec) mysql> SHOW TABLES; Empty set (0.00 sec)
补充:MySQL表数据类型
MySQL支持多种数据类型,大致可以分为三类:数值、日期/时间和字符串(字符)类型
数值类型
日期/时间类型
表示时间值的日期和时间类型为DATETIME、DATE、TIMESTAMP、TIME和YEAR。
每个时间类型有一个有效值范围和一个"零"值,当指定不合法的MySQL不能表示的值时使用"零"值。
字符串类型
字符串类型指CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT、ENUM和SET。
CHAR和VARCHAR类型类似,但它们保存和检索的方式不同。它们的最大长度和是否尾部空格被保留等方面也不同。在存储或检索过程中不进行大小写转换。
CHAR:指定字符个数
VARCHAR:最大长度
BINARY和VARBINARY类类似于CHAR和VARCHAR,不同的是它们包含二进制字符串而不要非二进制字符串。也就是说,它们包含字节字符串而不是字符字符串。
BLOB是一个二进制大对象,可以容纳可变数量的数据。有4种BLOB类型:TINYBLOB、BLOB、MEDIUMBLOB和LONGBLOB。它们只是可容纳值的最大长度不同。
有4种TEXT类型:TINYTEXT、TEXT、MEDIUMTEXT和LONGTEXT。这些对应4种BLOB类型,有相同的最大长度和存储需求。
表记录的操作:下一天
Linux-数据库1
标签:创建表 时间类 结构化 语言 关系 toolbar set 数据库的操作 自动