MySQL 创建数据库 创建表
最近几天开始研究MySQL。那么先来看看最基本的MySQL操作。
登录数据库:
mysql代表客户端命令;
-u 后面跟连接的数据库用户名;
root为用户名,也可以是其他用户名
-p 表示要输入密码
使用SHOW语句找出在服务器上当前存在什么数据库:
-
mysql> SHOW DATABASES;
-
+----------+
-
| Database |
-
+----------+
-
| mysql |
-
| test |
-
+----------+
-
3 rows in set (0.00 sec)
创建一个数据库starive
-
mysql> CREATE DATABASE starive;
注意不同操作系统对大小写的敏感。
选择你所创建的数据库
-
mysql> USE starive
-
Database changed
此时你已经进入你刚才所建立的数据库starive.
显示表
首先看现在你的数据库中存在什么表:
-
mysql> SHOW TABLES;
-
Empty set (0.00 sec)
删除数据库
创建表
说明刚才建立的数据库中还没有数据库表。下面来创建一个数据库表Student, Course, SC :
-
CREATE TABLE Student
-
(Sno CHAR(9) PRIMARY KEY,
-
Sname CHAR(20) NOT NULL,
-
Ssex CHAR(4),
-
Sage SMALLINT,
-
Sdept CHAR(20)) engine= innodb;
粘贴复制版本:
-
CREATE TABLE Student(Sno CHAR(9) PRIMARY KEY,Sname CHAR(20) NOT NULL,Ssex CHAR(4),Sage SMALLINT,Sdept CHAR(20)) engine= innodb;
查看表的定义:
更详细的查看表的定义
-
show create table tablename \G;
-
create table course(
-
cno CHAR(6),
-
cname varchar(20),
-
credit int,
-
primary key(cno)
-
) engine= innodb;
粘贴复制版本:
-
create table course(cno CHAR(6),cname varchar(20),credit int,primary key(cno)) engine= innodb;
-
CREATE TABLE SC
-
(Sno CHAR(9) NOT NULL,
-
Cno CHAR(6) NOT NULL,
-
Grade SMALLINT,
-
PRIMARY KEY (Sno,Cno),
-
constraint f1 FOREIGN KEY (Sno) REFERENCES Student(Sno),
-
FOREIGN KEY (Cno) REFERENCES Course(Cno)
-
) engine= innodb;
粘贴复制版本:
-
CREATE TABLE SC(Sno CHAR(9),cno CHAR(6),Grade SMALLINT,PRIMARY KEY (Sno,cno),constraint f1 FOREIGN KEY (Sno) REFERENCES Student(Sno),FOREIGN KEY (cno) REFERENCES course(cno))engine= innodb;
###### 书《深入浅出MySQL:数据库开发、优化与管理维护(第二版)》 里面的两张表 emp, dept #####
-
CREATE TABLE emp (
-
ename varchar(20),
-
hiredate date,
-
sal decimal(10,2),
-
deptno int(2)
-
) ENGINE=InnoDB
-
mysql> create table dept(deptno int(2),deptname varchar(20));
-
Query OK, 0 rows affected (0.04 sec)
删除表:
创建了表后,我们可以看看刚才做的结果,用SHOW TABLES显示数据库中有哪些表:
-
mysql> show tables;
-
+-------------------+
-
| Tables_in_starive |
-
+-------------------+
-
| course |
-
| sc |
-
| student |
-
+-------------------+
-
3 rows in set (0.00 sec)
5、显示表的结构:
-
mysql> describe student;
-
+-------+-------------+------+-----+---------+-------+
-
| Field | Type | Null | Key | Default | Extra |
-
+-------+-------------+------+-----+---------+-------+
-
| Sno | char(9) | NO | PRI | NULL | |
-
| Sname | char(20) | NO | | NULL | |
-
| Ssex | char(4) | YES | | NULL | |
-
| Sage | smallint(6) | YES | | NULL | |
-
| Sdept | char(20) | YES | | NULL | |
-
+-------+-------------+------+-----+---------+-------+
-
5 rows in set (0.22 sec)
修改表
修改表类型
-
alter table tablename MODIFY [column] column_definition [First | After col_name] ([]可选)
-
-
mysql> alter table emp modify ename varchar(20);
-
Query OK, 0 rows affected (0.24 sec)
-
Records: 0 Duplicates: 0 Warnings: 0
增加表字段
-
alter table tablename ADD [column] column_definition [First | After col_name]
-
-
mysql> alter table emp add column age int(3);
-
Query OK, 0 rows affected (0.18 sec)
-
Records: 0 Duplicates: 0 Warnings: 0
删除表字段
-
alter table tablename drop [column] col_name
-
-
mysql> alter table emp drop column age;
-
Query OK, 0 rows affected (0.14 sec)
-
Records: 0 Duplicates: 0 Warnings: 0
字段改名
-
alter table tablename CHANGE [COLUMN] old_col_name new_col_definition [First | After col_name]
-
-
mysql> alter table emp change age age1 int(4);
-
Query OK, 0 rows affected (0.14 sec)
-
Records: 0 Duplicates: 0 Warnings: 0
修改字段排列顺序
Eg: 将新增加的字段 birth date 加在 ename 之后:
-
mysql> alter table emp add birth date after ename;
-
Query OK, 0 rows affected (0.10 sec)
-
Records: 0 Duplicates: 0 Warnings: 0
-
-
-
mysql> desc emp;
-
+----------+---------------+------+-----+---------+-------+
-
| Field | Type | Null | Key | Default | Extra |
-
+----------+---------------+------+-----+---------+-------+
-
| ename | varchar(20) | YES | | NULL | |
-
| birth | date | YES | | NULL | |
-
| hiredate | date | YES | | NULL | |
-
| sal | decimal(10,2) | YES | | NULL | |
-
| deptno | int(2) | YES | | NULL | |
-
| age1 | int(4) | YES | | NULL | |
-
+----------+---------------+------+-----+---------+-------+
-
6 rows in set (0.00 sec)
修改字段age, 将它放在最前面:
-
alter table emp modify age int(3) first;
-
-
mysql> desc emp;
-
+----------+---------------+------+-----+---------+-------+
-
| Field | Type | Null | Key | Default | Extra |
-
+----------+---------------+------+-----+---------+-------+
-
| age | int(3) | YES | | NULL | |
-
| ename | varchar(20) | YES | | NULL | |
-
| birth | date | YES | | NULL | |
-
| hiredate | date | YES | | NULL | |
-
| sal | decimal(10,2) | YES | | NULL | |
-
| deptno | int(2) | YES | | NULL | |
-
+----------+---------------+------+-----+---------+-------+
-
6 rows in set (0.01 sec)
更改表名
语法如下:
-
alter table tablename rename [To] new_tablename
-
-
mysql> alter table emp rename emp1;
-
Query OK, 0 rows affected (0.08 sec)
往表中加入记录
我们先用SELECT命令来查看表中的数据:
-
mysql> select * from student;
-
Empty set (0.00 sec)
插入数据:
-
INSERT INTO Student VALUES('0201','赵伟','男',18,'cs');
-
INSERT INTO Student VALUES('0202','张力虹','男',19,'is');
-
INSERT INTO Student VALUES('0203','徐秀美','女',21,'is');
-
INSERT INTO Student VALUES('0204','刘平','男,20,'cs');
-
INSERT INTO Student VALUES('0205','姚家全','男',19,'cs');
-
INSERT INTO Student VALUES('0206','上关美云','女',23,'ma');
再用上面的SELECT命令看:
-
mysql> select * from student;
-
+------+----------+------+------+-------+
-
| Sno | sname | Ssex | Sage | Sdept |
-
+------+----------+------+------+-------+
-
| 0201 | 赵伟 | 男 | 18 | cs |
-
| 0202 | 张力虹 | 男 | 19 | is |
-
| 0203 | 徐秀美 | 女 | 21 | is |
-
| 0204 | 刘平 | 男 | 20 | cs |
-
| 0205 | 姚家全 | 男 | 19 | cs |
-
| 0206 | 上关美云 | 女 | 23 | ma |
-
+------+----------+------+------+-------+
-
6 rows in set (0.00 sec)
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26435490/viewspace-1259471/
,如需转载,请注明出处,否则将追究法律责任。