一、数据库管理
1 创建数据库: CREATE DATABASE dataname;
删除数据库: DROP DATABASE dataname;
不能重名数据库名
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (0.00 sec) mysql> CREATE DATABASE xxj; Query OK, 1 row affected (0.00 sec) mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | | xxj | +--------------------+ 4 rows in set (0.00 sec) mysql> DROP DATABASE xxj; Query OK, 0 rows affected (0.00 sec) mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (0.00 sec) mysql>
二、数据类型
数据是以表格的形式保存在数据库中
每个列(属性)以特定数据类型保存,常见数据类型如下:
数据类型 用途
integer(size),int(size),smalint(size),tinyint(size) #能存储的最大的整数从大到小 存储整数数据
decima(size,d),rumeric(size,d) 存储浮点数数据
char(size) 存储固定长度字符串
archar(size) 存储可变长度字符串
date(yyyymmdd) 存储日期
三、创建表格
查看当前数据库下的所有表:SHOW TABLES;
通过一下语法创建一个表格:
CREATE TABLE tablename
(
列名称1 数据类型,
列名称2 数据类型,
列名称3 数据类型,
、、、
);
查看表的结构:DESC tablename;
删除表:DROP TABLE tablename;
例: 我们希望创建一个表格lc_course保存课程信息,需要保存课程名称、课程长度、课程讲师、和课 程分类。 #一般会创建一个ID列,来唯一标识一条记录
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (0.00 sec) mysql> USE test; Database changed mysql> show tables; Empty set (0.00 sec) mysql> CREATE TABLE lc_course; ERROR 1113 (42000): A table must have at least 1 column mysql> CREATE TABLE lc_course( -> id int(10), -> course_name varchar(50), -> course_length int, #不指定默认是11 -> teacher varchar(20), -> category varchar(50) -> ); Query OK, 0 rows affected (0.12 sec) mysql> SHOW TABLES; #查看当前数据库下的所有表 +----------------+ | Tables_in_test | +----------------+ | lc_course | +----------------+ 1 row in set (0.00 sec) mysql> DES lc_course; #查看表结构 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘DES lc_course‘ at line 1 mysql> DESd lc_course; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘DESd lc_course‘ at line 1 mysql> DESc lc_course; +---------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+---------+-------+ | id | int(10) | YES | | NULL | | | course_name | varchar(50) | YES | | NULL | | | course_length | int(11) | YES | | NULL | | | teacher | varchar(20) | YES | | NULL | | | category | varchar(50) | YES | | NULL | | +---------------+-------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
重命名表名:ALTER TABLE tablename RENAME newtablename;
向表中添加一列:ALTER TABLE tablename ADD 列名 字符类型;
删除表中一列:ALTER TABLE tablename DROP COLUMN 列名; #好像不写COLUMN也是可以的
修改一个列的数据类型:ALTER TABLES tablename MODIFY 列名(已存在) 数据类型;
重命名一个列:ALTER TABLE course CHANGE COLUMN 旧列名 新列名 数据类型; #COLUMN不写也可以
mysql> ALTER TABLE lc_course RENAME course; Query OK, 0 rows affected (0.00 sec) mysql> SHOW TABLES; +----------------+ | Tables_in_test | +----------------+ | course | +----------------+ 1 row in set (0.00 sec) mysql> ALTER TABLE course ADD link varchar(100); Query OK, 0 rows affected (0.07 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC course_ -> ; ERROR 1146 (42S02): Table ‘test.course_‘ doesn‘t exist mysql> DESC course; +---------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+--------------+------+-----+---------+-------+ | id | int(10) | YES | | NULL | | | course_name | varchar(50) | YES | | NULL | | | course_length | int(11) | YES | | NULL | | | teacher | varchar(20) | YES | | NULL | | | category | varchar(50) | YES | | NULL | | | link | varchar(100) | YES | | NULL | | +---------------+--------------+------+-----+---------+-------+ 6 rows in set (0.00 sec) mysql> ALTER TABLE course DROP link; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC course; +---------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+---------+-------+ | id | int(10) | YES | | NULL | | | course_name | varchar(50) | YES | | NULL | | | course_length | int(11) | YES | | NULL | | | teacher | varchar(20) | YES | | NULL | | | category | varchar(50) | YES | | NULL | | +---------------+-------------+------+-----+---------+-------+ 5 rows in set (0.00 sec) mysql> ALTER TABLE cours MODIFY teacher varchar(100); ERROR 1146 (42S02): Table ‘test.cours‘ doesn‘t exist mysql> ALTER TABLE course MODIFY teacher varchar(100); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC course; +---------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+--------------+------+-----+---------+-------+ | id | int(10) | YES | | NULL | | | course_name | varchar(50) | YES | | NULL | | | course_length | int(11) | YES | | NULL | | | teacher | varchar(100) | YES | | NULL | | | category | varchar(50) | YES | | NULL | | +---------------+--------------+------+-----+---------+-------+ 5 rows in set (0.00 sec) mysql> ALTER TABLE cours MODIFY teacher varchar(100); ERROR 1146 (42S02): Table ‘test.cours‘ doesn‘t exist mysql> ALTER TABLE course MODIFY teacher varchar(100); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC course; +---------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+--------------+------+-----+---------+-------+ | id | int(10) | YES | | NULL | | | course_name | varchar(50) | YES | | NULL | | | course_length | int(11) | YES | | NULL | | | teacher | varchar(100) | YES | | NULL | | | category | varchar(50) | YES | | NULL | | +---------------+--------------+------+-----+---------+-------+ 5 rows in set (0.00 sec) mysql> ALTER TABLE course RENAME teacher lecture varchar(200); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘lecture varchar(200)‘ at line 1 mysql> ALTER TABLE course CHANGE COLUMN teacher lecture varchar(200); Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC course; +---------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+--------------+------+-----+---------+-------+ | id | int(10) | YES | | NULL | | | course_name | varchar(50) | YES | | NULL | | | course_length | int(11) | YES | | NULL | | | lecture | varchar(200) | YES | | NULL | | | category | varchar(50) | YES | | NULL | | +---------------+--------------+------+-----+---------+-------+ 5 rows in set (0.00 sec) mysql> ALTER TABLE course DROP lecture; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC course; +---------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+---------+-------+ | id | int(10) | YES | | NULL | | | course_name | varchar(50) | YES | | NULL | | | course_length | int(11) | YES | | NULL | | | category | varchar(50) | YES | | NULL | | +---------------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql> ALTER TABLE course CHANGE id idid int(12); Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc course; +---------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+---------+-------+ | idid | int(12) | YES | | NULL | | | course_name | varchar(50) | YES | | NULL | | | course_length | int(11) | YES | | NULL | | | category | varchar(50) | YES | | NULL | | +---------------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)