1. 启动服务
mysql -uroot -p
2. 显示所有数据库
show databases;
3. 创建数据库
create databases 数据库名称;
4. 切换数据库
use j2ee;
5. 显示所有表
show tables;
6. 显示表格信息
mysql> desc User;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| username | varchar(30) | YES | | NULL | |
| password | varchar(10) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
7. 增删查改表格
创建表
create table 表名(
列名1:列的类型
列名2:列的类型
列名3:列的类型
)
CREATE TABLE IF NOT EXISTS `runoob_tbl`(
`runoob_id` INT UNSIGNED AUTO_INCREMENT,
`runoob_title` VARCHAR(100) NOT NULL,
`runoob_author` VARCHAR(40) NOT NULL,
`submission_date` DATE,
PRIMARY KEY ( `runoob_id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
mysql> create table Role(
-> `role_id` int(3),
-> `role_name` varchar(30),
-> primary key(`role_id`));
Query OK, 0 rows affected (0.06 sec)
mysql> desc Role;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| role_id | int(3) | NO | PRI | NULL | |
| role_name | varchar(30) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
增加一个属性
alter table 表名 add column 列名 类型;
补充说明——多对多关系
多对多连接插入一条记录
insert into tablename(列名1,列名2.....列名n)
,(值1,值2.......值n),
,(值1,值2.......值n),
,(值1,值2.......值n);
insert into Role(role_id,role_name) values(001,'Teacher');
mysql> insert into Role(role_id,role_name) values(002,'Student'),(003,'Manager');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from role;
+---------+-----------+
| role_id | role_name |
+---------+-----------+
| 1 | Teacher |
| 2 | Student |
| 3 | Manager |
+---------+-----------+
3 rows in set (0.00 sec)
删除一条记录
delete from attend_table where duty_day='2019-05-27';
清空表格
TRUNCATE TABLE table1
网友评论