mysql 常用操作

2021-07-16 06:42:40 字數 1305 閱讀 7906

names表

create table if not exists names(

id int primary key not null, //id為主鍵且不為空

name varchar(20),

nick_name varchar(10) not null

);

friendship表
create table if not exists membership(

id int primary key not null auto_increment,

friend varchar(20),

id int,

foreign key (id) references names(id)//將names的主鍵作為friendship的外來鍵關聯

);

新增一行資料

//由於id是auto_increment,可以由系統自動生成

//第乙個括號是所要修改的列名,第二個括號裡是對應列要賦的值

insert into names (nick_name, name) values('測試','王鋼彈');

新增列
alter table add column new_column varchar(20);
刪除一行資料
//where 限定符,指定刪除 id等於2 的一行

delete from friendship where id=2;

更新乙個資料
//將id等於1的那行的friend列改為「小明」

update friendship set friend='小明' where id=1;

修改列屬性
//將names表的id列改為自動遞增,即新增行時,不用指定id值,由系統自動新增,且與前一行有遞增關係

#alter table names modify id int auto_increment;

刪除表
//注意與其他表有約束關係的情況

drop table friendship;

刪除列
alter table friendship drop column id;
修改表名
alter table friendship rename to  friend;
修改列名
alter table friendship change id new_id int;

my sql常用操作

1.grant allprivilegeson tomonty localhost identified by something with grant option monty 可以從任何地方連線伺服器的乙個完全的超級使用者,但是必須使用乙個口令 something 做這個。注意,我們必須對 mo...

mysql 常用操作

1 修改表名在mysql中修改表名的sql語句在使用mysql時,經常遇到表名不符合規範或標準,但是表裡已經有大量的資料了,如何保留資料,只更改表名呢?alter table table name rename to new table name 例如alter table admin user r...

mysql常用操作

mysql常用操作 修改root密碼 用root 進入mysql後 mysql set password password 你的密碼 mysql flush privileges 檢視表結構 show create table 表名 清空表且令自增字段從1開始 truncate table 表名 檢...