Xiao Bian found that this thing is completely forgotten when it is not used for a long time, so it is necessary to open it often or write a blog summary to develop this habit!
Create and database: create database database name;
View databases: show databases;
Select database: use database;
Delete database: drop database database name;
Common command steps for querying data in the database in the dos window:
a:show databases;
b: use test; (test library name)
c:show tables;
d: desc student; (student is the table name)
e: Add, delete, modify and check operations
For example query:
select * from student;
---------------------------------------------------------------------
Create the table:
create table [if not exists] 表名(
field1 datatype [field properties | constraints] [index] [comment]
field2 datatype [field properties | constraints] [index] [comment]
.......................
) [table type] [table character set] [comment]
1: Single field primary key
create table [if not exists] 表名 (
Field 1 data type primary key;
............................
);
E.g:
create table student(
student int(4) primary key,
.........................
);
2: Multi-field union primary key
create table [if not exists] 表名(
primary key [field1,field2..........]
);
E.g:
create table tb_temp(
'id' int(4),
'name' varchar(11),
.....................
primary key('id','name')
);
View table: show table;
Delete table: drop table [if not exists] table name;
For example: drop table student;
---------------------------------Modify table--------------- -------------------
Modify the table name:
alter table old table name rename new table name
E.g:
ALTER TABLE easybuy_comment RENAME TO easybuy_com
Add fields:
alter table table name add field name data type [attribute]
例如:ALTER TABLE easybuy_comment ADD easybuy_num INT(10) NOT NULL
Modify fields:
alter table table name change original field name new field name data type [attribute]
ALTER TABLE easybuy_comment CHANGE easybuy_num easybuy_sss INT(10) NOT NULL
Delete fields:
alter table table name drop field name
alter table easybuy_comment drop easybuy_sss
Add a primary key constraint:
alter table table name add constraint primary key name primary key table name (primary key field)
Add a foreign key constraint:
alter table table name add constraint foreign key name foreign key (foreign key field) references associated table name (associated field)
----------------------------------------Insert data using DML---------------- -------------
1: Insert a single row of data
insert into table name (list of field names) values (list of values)
E.g:
insert into student (id,pass,usename) values (1,‘aa’,‘哈哈1’)
2: Insert multiple rows of data
insert into new table (field list) values (list value 1), (list value 2), (list value 3), ..... (list value n)
E.g:
insert into student (id, pass, usename) values (1, 'aa', 'haha 1'), (2, 'bb', 'haha 2'), (3, 'cc', 'haha 3')
3: Insert the query results into a new table
create table 新表(select studentName,phone, from student)
-----------------------------Update data using DML ----------------- ------------------------------
update table name set column name = update value where update condition
E.g:
update student set sex=‘女’
update student set address='Beijing' where address='Gansu'
-----------------------Deleting data using DML ----------------------- ----------------------
delete from table name where delete condition
E.g:
delete from student where studentName='Wang Baobao'
delete all records in the table
truncate table student
-------------------------- Query data using DML -------------------- -------------------------------
select column name|expression|function|constant from table name where query condition order by asc|desc